I'm plotting a data set with chemical formulae as categories, and values associated with each:
data <- data.frame(compound = factor(c("SiO[2]", "Al[2]O[3]", "CaO")),
value = rnorm(3, mean = 1, sd = 0.25))
I want to get the subscripts in the chemical formulae to display correctly in the axis labels. I've tried various solutions involving bquote()
, label_parsed()
, scales::parse_format()
and ggplot2:::parse_safe
(as per this thread), but all of those give me either no category labels at all or a mess. For example:
ggplot(data = data, aes(x = compound, y = value)) +
geom_col() +
scale_x_discrete(labels = scales::parse_format())
Gives this error message:
Error in parse(text = x, srcfile = NULL) : 1:6: unexpected symbol
1: Al[2]O
^
Can anyone help? I've done this successfully before with the x axis and x-axis labels (via labs()
and then bquote()
or similar), and there are various threads I can see for that problem, but the same solutions don't seem to work for category labels.