Bayesplot mcmc_areas line and fill colour

Hi there,

I’m trying to plot posterior predictions from a brms model in R.

I’ve been able to get them to look nice using mcmc_areas, but id really like to edit the colours more. I know they can be set with e.g., color_scheme_set(“brewer-Reds”), e.g.,:

However, id like the colours to be the other way around, e.g., a lighter outer line, and a dark fill of the area?

Ive looked in ggdist and other examples, but none seem to have a similar example.

Here’s my code so far:

 off <- mcmc_areas(
   posterior, 
   pars = c("a", "b", 
            "c", "d"),
   prob = 0.89, 
   prob_outer = 0.95, 
   point_est = "mean"
 )

off2 <- off + 
  xlim(-1, 1.5) +
  scale_y_discrete(labels=c(
   "a" = "Housing",
   "b" = "Monkeys",
   "c" = "Moomins",
   "d" = "Roses"
 ))

To reverse the colors in an existing color scheme, you can use the rev (“reverse”) function to reverse the color order in bayesplot’s color scheme.

color_scheme_get() gives the current color scheme. If we look at the structure of the color scheme object, we can see that creating a scheme with a reversed color order should invert the colors in the plot.

library(bayesplot)

> str(color_scheme_get())
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"

Here’s an example:

# some parameter draws to use for demonstration
set.seed(3)
x <- example_mcmc_draws(params = 4)
color_scheme_set("red")

mcmc_areas(x, regex_pars="beta")

Now reverse the colors in the color scheme:

cols = rev(unname(unlist(color_scheme_get())))
color_scheme_set(cols)

mcmc_areas(x, regex_pars="beta")

You can also set your own custom color scheme by creating a vector of six colors. Below I’ve just reversed the custom color vector from the help for bayesplot-colors.

orange_scheme <- rev(c("#ffebcc", "#ffcc80",
                       "#ffad33", "#e68a00",
                       "#995c00", "#663d00"))

color_scheme_set(orange_scheme)

mcmc_areas(x, regex_pars="beta")

Created on 2022-03-24 by the reprex package (v2.0.1)

1 Like

Excellent - worked a charm, thanks so much.
I had tried the _scheme code before, but didn’t realise it had to have 6 colours added.