HJT
HJT

Reputation: 327

Reorder groups of factor for sjPlot::plot_model in R

I am plotting the interaction effects of a complex model from lme4::glmer using sjPlots::plot_model. I'd like to change the ordering of the panels of a categorical factor. E.g. below I would like the order to be 'switch' and then 'maintain' rather than their current order.

sample image of current figure

My current code to produce this figure is:

plot_model(glmer_build, type = "int")

The argument order.terms provided by sjPlots only allows me to reorder the sequence of terms when looking at the coefficients - not levels within a factor.

Suggestions for doing this without just changing the names of the levels in my factor to be in the desired order (e.g. making them A. Switch and B. Maintain - I actually already did this with another variable....).

Upvotes: 1

Views: 397

Answers (1)

Pablo Bernabeu
Pablo Bernabeu

Reputation: 262

This can be achieved by adding a guides aesthetic, as exemplified at https://github.com/pablobernabeu/language-sensorimotor-simulation-PhD-thesis/blob/main/semanticpriming/frequentist_analysis/semanticpriming-interactions-with-gender.R#L46-L55

plot +
  guides(color = 'none',
         fill = guide_legend(title = 'Gender', 
                             # In each key of the legend, replace the 
                             # default line with a full square.
                             override.aes = list(linetype = c(0, 0), 
                                                 alpha = 1), 
                             # Reverse default order of categories 
                             # in legend (else, Male would appear 
                             # first as it was coded as -0.5).
                             reverse = TRUE))

Upvotes: 0

Related Questions