Getting ggthemes to work with marginal effects plots

I’m trying to plot the marginal_effects using a different theme than the default. In the example below I use theme_stata (my preferred theme) but I’ve also tried it with other themes. According to the help for the function marginal_effects I should be able to use a ggthemes theme. Anyone know what might be going on? I’ve included a reproducible example below.

data = read.csv("https://tinyurl.com/y9uct2c5",header = TRUE)
fit = brm(Neuroticism ~ Age + Gender, data=data)

#This doesn’t change the theme.
marginal_effects(fit, theme=ggthemes::theme_stata())

#This doesn’t work either
marginal_effects(fit, theme=theme_stata())
marginal_effects(fit, theme=ggthemes::theme_stata)
marginal_effects(fit, theme=theme_stata)

#This throws an error.
theme_set(ggthemes::theme_stata())

Warning message:
New theme missing the following elements: axis.title.x.top, axis.title.y.right, axis.text.x.top, axis.text.y.right, axis.line.x, axis.line.y, legend.margin, legend.spacing.x, legend.spacing.y, legend.box.margin, legend.box.background, legend.box.spacing, panel.spacing.x, panel.spacing.y, panel.ontop, plot.subtitle, plot.caption, strip.placement, strip.switch.pad.grid, strip.switch.pad.wrap

  • Operating System: Windows 10
  • brms Version: 2.3.0
  • ggthemes Version: 3.5.0

You could try the package ggeffects, which also supports brms-models, e.g.:

p <- purrr::map(
       c("Age", "Gender"),
       ~  ggeffects::ggpredict(fit, .x) %>% plot() + theme_stata()
     )
p

p1

p2

1 Like

theme is an argument of the plot method of marginal_effects not of marginal_effects itself (see ?marginal_effects for details). In your case, go for

plot(marginal_effects(fit), theme=theme_stata())
2 Likes

Thanks, that’s perfect! This toy data set actually has a lot of heteroscedasticity, when you see the plot. Looks like a good opportunity to toy around with the asymmetric laplace family

Paul,

I’m in a similar situation: I’m using plot(marginal_effects(fit), theme_theme_tufte()), but then I’d also like to change the color of the line which defaults to blue. I’ve tried lots of combinations by now (using different ggplot2 calls, or even using par() directly), but alas, it’s still blue. Not that I don’t like blue, it’s just that all my plots are in bayesplot::color_scheme_set(“darkgray”) :(

plot(marginal_effects(...)) returns a list of ggplot objects. You can extract and amend each element to your needs using ggplot2.

1 Like

Much appreciated - and as always you reply quickly. Thanks a lot!

Which version of brms do I need for this?

plot(marginal_effects(…))
Error in plot(marginal_effects(…)) : ‘…’ used in an incorrect context`

The ... was just a placeholder for all arguments you might want to use ;-)

doh sorry for spamming…

Oh, and the reason for why it didn’t work originally was actually me sitting in RStudio. When several plots get printed by a function then RStudio says: Hit <Return> to see next plot: so if I save the object I also need to be explicit about which plot I want to manipulate, e.g., p$one_of_the_plots + ...

See @paul.buerkner what I did there with the dots ;)

Sorry to revive this. I cannot get the blue lines to play along with the theme when I use spaghetti = TRUE. I can change the color by doing:

me_plot[[1]]$layers[[2]]$aes_params$colour <- nicer_color

But I feel like there should be a better way…

For such fine tuning it may be easier to just write the plots yourself from scratch using either the list of data.frames returned by marginal_effects itself (the plotting is just done by the “print” or “plot” methods) or by using the tidybayes package.