ggplot2 facet strip text that begins with a superscript

Viewed 721

This probably sounds redundant, but I've searched to no avail to resolve this specific issue. I want to label a facet strip with a symbol for an isotope: 222Rn, where the "222" is in superscript format and there is no space before "Rn". Below is a simple example using the mpg data set:

require(ggplot2)
test <- transform(mpg, drv = factor(drv, levels=c("4", "f", "r"),
        labels=c("Nitrate~as~NO[3]", "sum(x[i], i==1, n)", "x^222~Rn")))
ggplot(test, aes(displ, hwy)) +
geom_point() +
facet_wrap(~drv, labeller=label_parsed)

In the first two facets, can achieve most types of character/symbol variations (even somewhat complex ones), but in the third seemingly simple case - if the hat ("^") is not preceded by a character & not followed by a blank space designator "~"... the script fails every time.

1 Answers

Try this instead:

test <- transform(mpg, drv = factor(drv, levels=c("4", "f", "r"),
                                    labels=c("Nitrate~as~NO[3]", 
                                             "sum(x[i], i==1, n)", 
                                             "phantom(x)^222*Rn")))

I usually solve these things by poring over the info at ?plotmath and then lots of trial and error.

Related