Search code examples
rgraph-visualizationsjplot

sjPlot: how to specify dependent variables?


I have a short question about the sjPlot, plot_models function.

I have this code:

fit1 <- lm(mpg ~ wt + cyl + disp + gear, data = mtcars)
fit2 <- update(fit1, . ~ . + hp)
fit3 <- update(fit2, . ~ . + am)


fit4 <- lm(hp ~ wt + cyl + disp + gear, data = mtcars)
fit5 <- update(fit4, . ~ . + hp)
fit6 <- update(fit5, . ~ . + am)


fit7 <- lm(cyl ~ wt + cyl + disp + gear, data = mtcars)
fit8 <- update(fit7, . ~ . + hp)
fit9 <- update(fit8, . ~ . + am)



plot_models(fit1, fit2, fit3, fit4, fit5, fit6, fit7, fit8, fit9,std.est = "std2")

Which generates this:

enter image description here

How can I code all dependent variables that refer to MPG as blue, all dependent variables that refer to hp as green, and all dependent variables that refer to cyl as yellow in the estimates plot? And then the label should update correspondingly?


Solution

  • You can use ggplot commands to adjust the colors:

    plot_models(fit1, fit2, fit3, fit4, fit5, fit6, fit7, fit8, fit9, std.est = "std2") +
      scale_color_manual(values = c("mpg.1" = "dodgerblue", 
                                    "mpg.2" = "dodgerblue3", 
                                    "mpg.3" = "blue",
                                    "hp.4" = "yellow",
                                    "hp.5" = "yellow2",
                                    "hp.6" = "gold2",
                                    "cyl.7" = "chartreuse", 
                                    "cyl.8" = "springgreen4", 
                                    "cyl.9" = "forestgreen"))
    

    As you can see, the distinction between the subtypes is not very good.

    enter image description here