Search code examples
rsjplot

Change variable order in plot_models from sjPlot


I want to change the order of which the variables are displayed on the y-axis. Like in this code:

mod1 <- lm(mpg ~ cyl + disp + hp, data = mtcars)
mod2 <- lm(drat ~ cyl + disp + hp, data = mtcars)

library(sjPlot)
plot_models(mod1, mod2)

How can I for example put hp in the middle between cyl and disp (without changing the formula)? In plot_model, without the s, one can use "order.terms" but this doesn't exist for plot_models.


Solution

  • You can do this by hacking the data within the plot object.

    pp <- plot_models(mod1, mod2)
    pp$data <- transform(pp$data, 
        term = factor(term, levels = c("disp", "hp", "cyl")))
    print(pp)