How does color_scheme in bayesplot work?

Hi!
I have a rather technical and R-ish question about bayesplot. How does “color_scheme_set()” work? I want to have the same scheme of colors in my ggplots than in my bayesplots, and I would like to force ggplot to use the same colors by default than bayesplot.
I looked at the bayesplot-colors.R code, but I don’t understand how .bayesplot_aesthetics is used. I’ve found this post about changing the color scheme in R but it doesn’t seem to be the right way.
Sorry that this is more R-related than stan, but I guess @jonah or some of the other devs can help me out.

1 Like

By default bayesplot color schemes work a bit differently than standard ggplot2 color schemes. color_scheme_set() sets a 6 color palette, the elements of which it uses for different kinds of plot elements (e.g., if something is filled using light then it’s outlined using light_highlight):

> str(color_scheme_get("blue"))
List of 6
 $ light          : chr "#d1e1ec"
 $ light_highlight: chr "#b3cde0"
 $ mid            : chr "#6497b1"
 $ mid_highlight  : chr "#005b96"
 $ dark           : chr "#03396c"
 $ dark_highlight : chr "#011f4b"
 - attr(*, "mixed")= logi FALSE
 - attr(*, "scheme_name")= chr "blue"
 - attr(*, "class")= chr [1:2] "bayesplot_scheme" "list"

The plotting functions then access the colors by referring to e.g. light_highlight (or lh for short), which would use the 2nd color in the scheme.

However, most of the plotting functions use ggplot2::scale_color_manual()
and/or ggplot2::scale_fil_manual() internally and so you can use that afterwards to change colors in the standard ggplot2 way. For example:

color_scheme_set("brightblue")
y <- example_y_data()
yrep <- example_yrep_draws()
dim(yrep)
p <- ppc_dens_overlay(y, yrep[1:25, ]) 
plot(p) # uses brightblue color scheme set above

p2 <- p + ggplot2::scale_color_manual(values = c("red", "green"))
plot(p2)  # uses red and green

# changing fill too
ppc_stat(y, yrep) + 
  ggplot2::scale_color_manual(values = "black") +
  ggplot2::scale_fill_manual(values = "green")

There are still some functions that this won’t work for (some of the mcmc_ ones I think) but expanding capability is on the to-do list.