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?
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:
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.
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.
If then +scale_y_discrete(labels = newlabels) is used, then y-axis get reversed with correct labels.
When used +scale_y_discrete(labels = newlabels) is used, y-axis gets again reversed, but the labels get missplaced.
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: