Manipulating bayesplot plots

Not sure this is the right category for bayesplot related questions, but here it goes…

I want to change the subtitles(?) in an mcmc_pairs plot:

mcmc_p <- mcmc_pairs(posterior,
pars = c(“b_intercept”, “b_technique”, “b_category”),
off_diag_args = list(size = 0.5),
diag_fun = “dens”)

I can get the first density plot in my 3x3 plot by writing mcmc_p[1,1], and then I naively thought I could manipulate the text by adding + labs(title=expression(beta[i])) or, perhaps, using subtitle. I’ve checked ?bayesplot-helper, and it seems one can manipulate most things, but not the title/subtitle (it points to ?labs, but there the examples are like the one I suggest above). I’m sure this is an easy one to answer for @jonah… I have a feeling my problem is due to multiple plots?

mcmc_pairs() is a bit different than other bayesplot functions in that it returns a grid of plots rather than a single ggplot object. So it makes what you’re trying to do a bit difficult. It’s easier to do this using the development version of bayesplot on github, which saves the individual ggplot objects in the object returned by mcmc_pairs() so you can access them after creating the plot. For example:


x <- example_mcmc_draws() 

[1] "alpha"   "sigma"   "beta[1]" "beta[2]"

pairs_plot <- mcmc_pairs(x, diag_fun = "dens")  # 4 x 4 grid
plots <- pairs_plot$bayesplots # list of 16 ggplot objects (diagonal plots are in slots 1,6,11,16)

# change beta[1] so it renders as math
plots[[11]] <- plots[[11]] + labs(subtitle = expression(beta[1]))

# recreate the grid
bayesplot_grid(plots = plots)


1 Like

And yeah, fine to ask this question here. Thanks for using the “bayesplot” tag

1 Like

Thanks for this answer, I need to be able to do this too. Any chance of getting this into the package in a more useable way? Basically anyone wanting to make publication plots from this will need this since usually the variable names in the stan model are not math-ready.