0

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?

  • 1
    I don't quite understand. If we add independent variables ("hp" for fit1 and "am" for fit2) it absolutely makes sense that the estimates for the dependent variable get recalculated for each model and thus get plotted independently, since they differ across the models. – Gnueghoidune Apr 13 '22 at 17:34
  • Thanks! I do want the coefficients to be plotted separately for each dependent variable, you are right. What I do not want is to have different labels for each dependent variable (mpg.1 being coded with green, mpg.2 being coded with blue etc.). What I would want is simply to have as a legend "mpg" being coded with e.g. blue, and all the estimates for the DVs that refer to "mpg" to be blue. And similarly, if I were to add another 3 models that use a different DV (say "hp"), I would want that to be e.g. yellow and all the estimates for hp to be yellow for all models that include it. – Codrin Mironiuc Apr 13 '22 at 17:59
  • I have now updated the question to represent more clearly what I need to have, hopefully this helps! – Codrin Mironiuc Apr 13 '22 at 18:35

1 Answers1

0

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

Gnueghoidune
  • 1,237
  • 4
  • 13
  • Thanks! If I select for all "mpg" variables the colour "blue", for all "hp" colors "yellow" and for all "cyl" variables green, I have exactly what I want! Is there any easy way to then only include in the legend that "mpg" is blue, "hp" yellow and "cyl" green? Without adding each individual dependent variable separately? – Codrin Mironiuc Apr 14 '22 at 12:21
  • 1
    add `breaks=c("mpg.1", "hp.4", "cyl.7"), labels = c("mpg", "hp", "cyl")` inside your `scale_color_manual`. However, there's loss of information if doing so. – Gnueghoidune Apr 14 '22 at 12:28