Changing the order of categories shown in conditional effects plots brms

Short summary of the problem

I am running a simple logistic regression with an interaction between between two categorical variables, one with five categories and the other binary. The conditional_effects() command displays the bars for the reference category first, but I want to change the order that the bars appear. Is there a way to do that?


fit <- brms(y ~ x1*x2, family=‘bernoulli’, data=dat)

x1 has five levels, (0, 1, 2, 3, 4), and the reference category is 2. Currently, the bars are plotted in the order of 2 0 1 3 4. I would like to have them shown from 0 to 4. Is this possible without having to change the reference category to 0?

Thank you

Please also provide the following information in addition to your question:

  • Operating System: Windows 10
  • brms Version: 2.12.0

Looking forward to your topic!

One relatively easy way to do this is to save the plot as an object and change the order of the levels directly in the plot object:

# save plot as p
p <- conditional_effects(fit,“x1:x2”)

#change order of factor levels in plot
p$`x1:x2`$effect1__ <- factor(p$`x1:x2`$effect1__, levels(p$`x1:x2`$effect1__)[c(2,3,1,4,5)])

#plot again

should do what you want. I hope this answers your question.

If you wanted to do the same thing for the main-effect plot of x1 the 2nd line would become

p$x1$effect1__ <- factor(p$x1$effect1__, levels(p$x1$effect1__)[c(2,3,1,4,5)])

That worked! Thanks a lot!