# Getting ggthemes to work with marginal effects plots

**URL:** <https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230>\
**Category:** brms\
**Tags:** bayesplot\
**Created:** [May 17, 2018, 5:56pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230 "2018-05-17T17:56:30Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![BKV](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bkv/32/4532_2.png) [@BKV](https://discourse.mc-stan.org/u/BKV)\
**Post date:** [May 17, 2018, 5:56pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/1 "2018-05-17T17:56:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![strengejacke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/strengejacke/32/559_2.png) [@strengejacke](https://discourse.mc-stan.org/u/strengejacke)\
**Post date:** [May 17, 2018, 6:28pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/2 "2018-05-17T18:28:09Z")

</div>

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](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/1/15e74a7d0ce076905b98a6115fe4e43258d47f0c.png)

![p2](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/7/7daeb2183b1c69f5874e8efec30795ddd6e0718a.png)

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [May 17, 2018, 7:01pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/3 "2018-05-17T19:01:05Z")

</div>

`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())

```

---

<div class="post-metadata">

**Author:** ![BKV](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bkv/32/4532_2.png) [@BKV](https://discourse.mc-stan.org/u/BKV)\
**Post date:** [May 17, 2018, 7:19pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/4 "2018-05-17T19:19:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [June 3, 2018, 5:09pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/5 "2018-06-03T17:09:48Z")

</div>

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”) :(

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [June 3, 2018, 5:11pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/6 "2018-06-03T17:11:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [June 3, 2018, 5:19pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/7 "2018-06-03T17:19:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [June 3, 2018, 5:20pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/8 "2018-06-03T17:20:47Z")

</div>

> [@paul.buerkner](#):
>
> plot(marginal\_effects(…))

Which version of `brms` do I need for this?

> plot(marginal\_effects(…))  
> Error in plot(marginal\_effects(…)) : ‘…’ used in an incorrect context`

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [June 3, 2018, 5:37pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/9 "2018-06-03T17:37:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [June 3, 2018, 5:38pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/10 "2018-06-03T17:38:11Z")

</div>

**doh** sorry for spamming…

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [June 3, 2018, 5:45pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/11 "2018-06-03T17:45:45Z")

</div>

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 ;)

---

<div class="post-metadata">

**Author:** ![Matias\_Guzman\_Naranjo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/matias_guzman_naranjo/32/4977_2.png) [@Matias\_Guzman\_Naranjo](https://discourse.mc-stan.org/u/Matias_Guzman_Naranjo)\
**Post date:** [March 3, 2020, 9:48pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/12 "2020-03-03T21:48:46Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [March 5, 2020, 8:35pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/13 "2020-03-05T20:35:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GGLuca](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ggluca/32/12431_2.png) [@GGLuca](https://discourse.mc-stan.org/u/GGLuca)\
**Post date:** [August 10, 2021, 9:07pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/14 "2021-08-10T21:07:24Z")

</div>

Hi there,

the problem seems to be individual, due to active packages that mask functions and so on. However that may be, here it what works for me. I try to call the functions directly from their package so it is pretty explicit.

We have a formula of:

```
fit1 = brm(data = data,
         Y ~ A + B)

```

To modify color transparency the ggplot2 usage has to be: alpha(colour, alpha = NA), but that is not readily apparent from the documentation. Also, alpha tends to be loaded from a variety of packages like psych and that throws everything out the window.

So i call it directly like this:

```
p <- conditional_effects(fit1, 
                    effects = "A",
                    spaghetti = T,
                    nsamples = 500) %>% 
 plot(mean=F,
       spaghetti_args = list(colour = ggplot2::alpha("#21908D", .2)))

p[[1]]+see::theme_lucid()

```

Say if it helped :)  
George

---

<div class="post-metadata">

**Author:** ![Matias\_Guzman\_Naranjo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/matias_guzman_naranjo/32/4977_2.png) [@Matias\_Guzman\_Naranjo](https://discourse.mc-stan.org/u/Matias_Guzman_Naranjo)\
**Post date:** [August 10, 2021, 9:25pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/15 "2021-08-10T21:25:03Z")

</div>

Hey, thanks for the idea. I now usually just build the plots myself, but that’s a neat trick.

---

<div class="post-metadata">

**Author:** ![GGLuca](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ggluca/32/12431_2.png) [@GGLuca](https://discourse.mc-stan.org/u/GGLuca)\
**Post date:** [August 10, 2021, 9:48pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/16 "2021-08-10T21:48:04Z")

</div>

How do you do that? Could you share some sample code for a multiple regression like that one here above?

---

<div class="post-metadata">

**Author:** ![Matias\_Guzman\_Naranjo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/matias_guzman_naranjo/32/4977_2.png) [@Matias\_Guzman\_Naranjo](https://discourse.mc-stan.org/u/Matias_Guzman_Naranjo)\
**Post date:** [August 10, 2021, 10:17pm UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/17 "2021-08-10T22:17:42Z")

</div>

Sure, here are two alternatives.

```
library(tidyverse)
library(brms)
library(gridExtra)

set.seed(1234)
a <- rnorm(50, 30, 3)
b <- rnorm(50, 0, 5)
y <- 1.2*a + 2*b + rnorm(50, 0, 4)

fit1 = brm(y ~ a + b
         , data = data.frame(a = a, b = b, y = y)
         , prior = c(prior(normal(50, 10), class = Intercept)
                   , prior(normal(0, 1), class = b))
         , cores = 4
         , chains = 4
         , iter = 1000
         , warmup = 500
         , seed = 1234
         )

set.seed(1234)
ce_plot_1 <- conditional_effects(fit1, 
                                effects = "a",
                                spaghetti = T,
                                nsamples = 500) %>% 
    plot(mean=F,
         spaghetti_args = list(colour = ggplot2::alpha("#21908D", .2)))

ce_plot_1

## there are two ways:
## the easy way:

set.seed(1234)
ce_plot_2_data <- conditional_effects(fit1, effects = "a", spaghetti = T, nsamples = 500)

ce_plot_2_data <- attr(ce_plot_2_data[[1]], "spaghetti")

ce_plot_2 <- ggplot(ce_plot_2_data, aes(x = effect1 __, y = estimate__ , group = sample__))+
    geom_line(color = alpha("#21908D", .2))

grid.arrange(ce_plot_1[[1]], ce_plot_2)

## the slightly harder way:
ce_plot_2_data %>% head

a_ce <- ce_plot_2_data[1:100,"a"] # we need to tell the model what to predict on

set.seed(1234)
ce_plot_3_data <- fitted(fit1, newdata = data.frame(b = 0.6976476, a = a_ce)
                      , summary = FALSE
                      , nsamples = 500) %>%
    as.data.frame()%>% 
    mutate(sample = 1:500) %>%
    pivot_longer(-c(sample)) %>%
    mutate(a = rep(a_ce, 500))

ce_plot_3_data %>% head

ce_plot_3 <- ggplot(ce_plot_3_data, aes(x = a, y = value, group = sample))+
    geom_line(color = alpha("#21908D", .2))

grid.arrange(ce_plot_1[[1]], ce_plot_2, ce_plot_3)

```

---

<div class="post-metadata">

**Author:** ![GGLuca](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ggluca/32/12431_2.png) [@GGLuca](https://discourse.mc-stan.org/u/GGLuca)\
**Post date:** [August 11, 2021, 5:43am UTC](https://discourse.mc-stan.org/t/getting-ggthemes-to-work-with-marginal-effects-plots/4230/18 "2021-08-11T05:43:19Z")

</div>

That is basically genius. Thanks! God, I love the internet :)
