How to plot interaction effects in R using brms

Hi there,

I am looking to plot an interaction effect from a multilevel model using brms in R. I successfully have used the conditional_effects function (Display Conditional Effects of Predictors — conditional_effects.brmsfit • brms) to plot and change almost everything I need. I feel like this is a silly question, but I have spent hours trying to change the linetypes and it just will not work. I am supplying my model below, as well as the code and images I currently have.

Model: brm(rumination ~ time * gender * corum_cw + corum_cb * gender +
(time + corum_cw + (time*corum_cw) | id), data = d, iter = 6000)

corum_cw is the effect of within-person changes in co-rumination. I want to plot the difference for girls vs. boys (this was a small but significant effect).

I used the following plotting code:
int_conditions ← list(
gender = setNames(c(-.5, .5), c(“boys”, “girls”))

p ← conditional_effects(mod, effects = “corum_cw:gender”,
int_conditions = int_conditions)

p ← plot(p, plot = FALSE)[[1]] +
xlab(“Change in Within-Person Co-rumination”) +

p + scale_colour_manual(values = c(“red”, “dodgerblue”)) + theme_bw()

And got this image:

The visual is nearly perfect, I just literally need to change one line to be dashed and one to be solid (for publication purposes). I have tried a million things and nothing has worked for me!

Thanks so much!

howdy. brms is using ggplot2, so I think what you are asking is a ggplot2 question. Have you tried adding geom_line(aes(linetype=gender)) ?

Edit - maybe you have tried that already. Hmm, not sure. Some data and reproducible example would help, but to be honest, at this point it might be easiest just to create your own data frames with the conditions you want and use fitted() to get the model output and then ggplot2 to plot it yourself.

Hi! Yes, I get the error “A continuous variable can not be mapped to linetype” – even though gender is effect coded as -.5 or .5. Changing it to linetype = as.factor(gender) doesn’t work either

Regarding changing the line types: the lines that are already in the plot were probably generated with geom_smooth(). Another call to geom_smooth will plot dotted lines on top of the original lines, rather than replacing them. However, calling aes() alone, without wrapping it inside geom_smooth, will replace the default linetype aesthetic in the pre-existing geom_smooth call that generated the original plot. So I think the code should be:

p + aes(linetype=factor(gender))

I’m not sure why factor(gender) didn’t resolve the error you were getting. Maybe try:

p + aes(linetype=factor(gender), group=gender)

Definitely not a Stan or brms question but a ggplot2 question as already pointed out.

Nevertheless, perhaps this works (you need to match linetype and colour) …

## ? = whatever is relevant from looking at str(p) ...

p1 <- ggplot(p$`?`,aes(x = ?, y = ?, colour = gender, linetype = gender))

p1 + theme_bw() + 
    scale_linetype_manual(values = c(1,3),    ## or the linetypes you prefer ...
                       labels = c("girls", "boys")) + 
    scale_colour_manual(values = c(“red”, “dodgerblue”),
                       labels = c("girls", "boys")) + 

This second part worked!! Thanks so much :)