How to modify axis labels in rstan::plot.stanfit()?

I am using rstan to estimate a model. After the sampler runs, I use plot() to produce a plot of point estimates and uncertainty intervals for the estimated parameters. However, it uses the “ugly” names for the parameters (e.g. sigma_individual), and I would like to report “pretty” names (e.g. Individual-level SD) on the axis labels.

I have figured out I can use scale_y_continuous(breaks=1:2, labels=c(“a”,“b”), but it seems to change the order of things, which makes it hard to know exactly what I’m doing.

I posted this on stackoverflow before realizing there’s a stan specific forum, apologizes for cross-posting.

This should be easier if you use bayesplot::mcmc_intervals(). The plot method for stanfit objects is still using the older version of the function at the moment. So something like this:

library(bayesplot)
library(ggplot2)

x <- as.matrix(stanfit, pars = ...)  
mcmc_intervals(x) + scale_y_discrete(labels = my_labels) # my_labels is character vector 

Alternatively you can change the column names of x and that should do the trick too.

1 Like

You can use named vectors as labels in ggplot2, which automatically correctly assigns the labels to the axis ticks, no matter which order you have. However, this does not work for continuous scales:

library(ggplot2)
data(iris)

ggplot(iris, aes(x = Species, y = Sepal.Width)) + 
  geom_point()

Rplot

ggplot(iris, aes(x = Species, y = Sepal.Width)) + 
  geom_point() +
  scale_x_discrete(
    labels = c(versicolor = "new 2nd", setosa = "new first", virginica = "last")
  ) +
  scale_y_continuous(
    labels = c(
      `2.0` = "test2", `4.0` = "test4", `4.5` = "four half", 
      `2.5` = "two half", `3.0` = "test3", `3.5` = "three half"
    )
  )

Rplot01

In the above plot, labels for continuous scales are taken in the order as provided by the labels-argument.

Thanks, that works well. I didn’t know about bayesplot. Thanks!

It’s relatively new (at least compared to RStan). Check out

http://mc-stan.org/bayesplot/articles

for some tutorials.