I have the following kind of interaction plot made with sjPlot, showing an interaction between two categorical variables. The levels seem to appear alphabetically in the plot - however, I would like them ordered low -> average -> high.
The code used to generate this plot is
plot_model(model, type = "pred", terms = c("education", "gender"))
Is there a way to specify/change the order in which levels of a factor are supposed to show up in the plot? Seems related to this question which unfortunately received no answers: Reorder groups of factor for sjPlot::plot_model in R and to this one as well, but I would have to access something else than the facets Adjusting facet order and legend labels when using plot_model function of sjplot
Edit: Another example with the starwars dataset from dplyr:
library(dplyr)
library(sjPlot)
model = lm(height ~ gender*species, data = starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek",])
plot = plot_model(model, type = "pred", terms = c("species", "gender"))
Say I would prefer to have the humans in the middle of the plot to be better able to see how they differ from droids and Twi'lek on each side. scale_x_discrete(limits = c("low", "average", "high")) unfortunately only changes the labels of the x-axis, but I would like to move not only the labels but also the corresponding data points around on the x-axis.
While at a first glance the x
scale looks like a discrete scale it is actually a continuous scale, in which case the limits
trick I mentioned in my comment does not work.
One option to fix that would be to convert the x
axis variable to a factor
with the order of the levels
set in the desired order.
library(dplyr, warn=FALSE)
library(sjPlot)
dat <- starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek", ]
dat$species <- factor(dat$species, levels = c("Droid", "Human", "Twi'lek"))
model = lm(height ~ gender*species, data = dat)
plot_model(model, type = "pred", terms = c("species", "gender"))