How can I adjust the brms plot like ggplot2?

Short summary of the problem

I had a question when I plotting brms fit

code_to_run_your_model(if_applicable)
fit_openness<-brm(formula=openness~canopy*date+(1|plot),

  •                     family = gaussian(link="identity"),
    
  •                     data=a,
    
  •                     seed=1,
    
  •                     prior=c(set_prior("",class="Intercept"),
    
  •                             set_prior("",class="sigma")),
    
  •                     chains=4,
    
  •                     iter=5000,
    
  •                     warmup=2000,
    
  •                     thin=1,
    
  •                     control = list(adapt_delta=0.97,max_treedepth = 15,stepsize=0.001)
    

fit<-conditional_effects(fit_openness,effect=“date:canopy”,re_formula = NA)

plot(fit,points=T)

then I got a figure like this
question.pdf (19.8 KB)

I want to adjust the interval between the red points and blue points, and maybe do other adjustments just like theme, font, etc. How can I do this?

thank you very much!

A reproducible example:

library(brms)

mtcars2 <- mtcars; mtcars2$cyl <- factor(mtcars$cyl); mtcars2$am <- factor(mtcars2$am)
model <- brm(qsec ~ cyl*am, data = mtcars2, chains = 1, backend = 'cmdstanr')
cond <- conditional_effects(model, effect = 'cyl:am')
(p <- plot(cond, points=T))

These plots that brms produces are returned in a list:

class(p)
# [1] "list"

But each list entry is just a ggplot object:

class(p[[1]])
# [1] "gg"     "ggplot"

Theme adjustments etc. are easy enough, you can add things to these plots:

p[[1]] + theme_minimal()

To make more involved changes, it’s probably easier to make your own plot instead. You can still use the conditional_effects output, e.g. like so:


est <- as.data.frame(cond[[1]])
ggplot() +
  geom_point(
    aes(cyl, qsec, color = am),
    mtcars2
  ) +
  geom_pointrange(
    aes(cyl, estimate__, ymin = lower__, ymax = upper__, color = am), 
    est,
    position = position_dodge(width = 1)
  ) +
  theme_minimal()

Rplot04

2 Likes

Mr.Ax3man

My problem was solved perfectly by your methods,

now I can adjust the brms data just like I adjust a normal ggplot2 data.

Thank you very much!

1 Like