0

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.

Alex
  • 1,997
  • 1
  • 15
  • 32

1 Answers1

1

You only need a slight adjustment of this question: Customize axis labels

We can use a named vector instead of supplying breaks and labels separately. Then it should be clearer what is going on.

library(ggplot2)
df <- data.frame(x = 1:5, y = sample(1:10, 5, TRUE))

qplot(factor(x),y, data = df) + 
  scale_x_discrete(labels=c("1" = "foo", "2" = "bar", "3" = "baz",
                            "4" = "phi", "5" = "fun")) +
  xlab(NULL)

For your case, that would be something like:

scale_y_continuous(labels = c("sigma_individual" = "Individual-level SD", etc.)
Thomas K
  • 3,242
  • 15
  • 29
  • 1
    Thanks, I didn't know it was possible to use a named vector for the labels! – Alex May 23 '18 at 15:10
  • Huh, this worked for a simple graph, but when used on a more complicated one it complains that `breaks` and `labels` are of different lengths. Do you know how to check what the `breaks` are of an existing `ggplot2` plot? – Alex May 23 '18 at 15:30
  • See this question: https://stackoverflow.com/questions/27374409/get-tick-break-positions-in-ggplot For my version (current dev = 2.2.1.9000) the following would work: `ggplot_build(p)$layout$panel_scales_x[[1]]$range`, p being the plot as an object. With RStudio, you can easily inspect the content via `View(ggplot_build(p))`. – Thomas K May 23 '18 at 16:15