Changing color of each posterior distribution through bayesplot

Hello,

I’m trying to change colors of posterior parameter distribution, for which estimation was done through brms. I use mcmc_areas() function of bayesplot package https://mc-stan.org/bayesplot/reference/MCMC-intervals.html. This function shows distributions of each distribution vertically, with the same color (the default is light blue). I would like to change this color manually with variation by distributions. For example, the top distribution is red, the second is blue, and so on. I added scale_color_manual(values = c(“red”, "blue”)). But it did not work.

mcmc_areas(
   as.array(model1), 
   pars = c("b_A", "b_B"),
   prob = 0.8,
   prob_outer = 0.95,
   point_est = "median") +
  ggplot2::labs(title = "Posterior parameter distributions") + 
  scale_color_manual(values = c("red", "blue"))

I find this way to change color https://discourse.mc-stan.org/t/bayesplot-mcmc-areas-line-and-fill-colour/26919/2. But it changes colors of all distributions consistently.

Do someone know the way to hack the color of distributions?

Thank you.

Not exactly a hack since it will take you a bit of work but you can generate plots one parameter at a time with different color scheme (& store them in a list) and then assemble the plots into one graphic. I use patchwork and patchwork::wrap_plots for this. I imagine it could work especially well if the parameters are grouped by category (location params, scale params, …) and you use different colors for each category.

2 Likes

Thank you very much. It’s a very great idea. I tried such way. But I have a problem yet. The patchwork function generates each distribution in each panel. So I modified your idea like the following. It fixes the range of x-axis across panels and deletes x-axes of all panels other than the bottom panel. I use cowplot package instead of patchwork package, just for my familiarity. Thank you!!!

p <- list()
parameter_vec <- c("b_a", "b_b", "b_c")
color_vec <- c("gray", "blue", "red")

for (i in 1:length(parameter_vec)){
  color_scheme_set(color_vec[i])

  p[[i]] <- mcmc_areas(
     as.array(model1), 
     pars = c(parameter_vec[i]),
     prob = 0.8,
     prob_outer = 0.95,
     point_est = "median") +
    scale_y_discrete(labels = c(name_levels[i])) +
    scale_x_continuous(limits = c(-0.5, 1.5))
  
  if (i != 3){
    p[[i]] <-  p[[i]] + 
      theme(axis.ticks.x = element_blank(),
            axis.text.x = element_blank(),
            axis.title.x = element_blank(),
            axis.line.x = element_blank())
  }
}

cowplot::plot_grid(p[[1]], p[[2]], p[[3]],
                   align = "hv",
                   ncol = 1)
1 Like