Manually changing y-axis (tick) labels in brms mcmc_plot()

I’m trying to figure out how to change y-axis labels (tick texts) on graphs produced by mcmc_plot(). I have tried using ggplot2 functions directly, but with varying success. For example, using “scale_y_discrete(labels = newlabels)” on a mcmc_plot works otherwise fine, but it reverses the order of the y-axis. I feel like I’m missing something completely obvious here. :)

In general, what is the simplest way of customizing mcmc_plots? Do bayesplot -helper functions work or should one turn directly to ggplot2?

  • Operating System: Windows 10
  • brms Version: 2.14.4

Hello!

I’m not sure what package is producing mcmc_plot or if you mean any of the mcmc_* functions from bayesplot, but if you post the code maybe we could figure it out? Here’s an example that works as I’d expect:

library(bayesplot)
library(ggplot2)

my_label_function <- function(x) {
  sprintf("%0.3f", as.numeric(x))
}

mcmc_trace(example_mcmc_draws()) +
  scale_y_continuous(labels = my_label_function)
1 Like

I see now that mcmc_plot comes from brms, but calls bayesplot functions and returns a ggplot object. If the labels are in reverse order you can use rev:

library(bayesplot)
library(ggplot2)
library(brms)

model <- brm(count ~ zAge + zBase * Trt + (1|patient),
             data = epilepsy, family = "poisson")

new_labels <- c(sprintf("beta[%d]", 0 : 4), "sigma[beta]")
mcmc_plot(model) + 
  scale_y_discrete(labels = rev(new_labels))

If you want to be fancy you can change the last line to labels = parse(text = rev(new_labels)) for nice plotmath expressions.

1 Like

Ah, yes it from brms. The issue isn’t that the labels are in reversed order, but the axis is reversed. So if labels = rev(new_labels) is used, then they are mislabelled completely.

This is the original output of mcmc_plot(). The goal is just to modify the labels and keep everything else as it is.
example1

If then +scale_y_discrete(labels = newlabels) is used, then y-axis get reversed with correct labels.
example2

When used +scale_y_discrete(labels = newlabels) is used, y-axis gets again reversed, but the labels get missplaced.
example3

The goal would be to have same plot as in the first picture, but with new labels for y-axis ticks. There seems to be bayesplot helper -functions to do this with bayesplot functions, but can they be implemented with the brms mcmc_plot?

xaxis_text() and yaxis_text() return a ggplot2 theme object that can be added to an existing plot (ggplot object) in order to toggle or format the text displayed on the x or y axis (e.g. tick labels).

Looking at the bayesplot source, they appear to be wrappers for ggplot2::theme, so we should be able to change them with ggplot functions alone.

I see this now, the issue is that bayesplot constructs the y-axis in reverse, so when we overwrite that with our own scale_y_discrete the reversing is removed. With a bit of recent ggplot magic we can write:

mcmc_plot(model) + 
  scale_y_discrete(labels = rev(new_labels), limits = rev) 

and get the same plot as the original mcmc_plot with new labels :).

3 Likes

It works (the code) and now I understand how to bayesplotting works! Thank you!

2 Likes