R `parse()` for subscripts mixed numbers and letters

Viewed 681

I am trying to display subscripts in ggplot facet labels where there are mixed letters and numbers in the subscript using parse().

This works:

>parse(text="cI[933]")

And so does this:

>parse(text="cI[W]")

But not these:

>parse(text="cI[933W]")
Error in parse(text = "cI[933W]") : <text>:1:7: unexpected symbol
1: cI[933W
          ^

> parse(text="cI[9W33]")
Error in parse(text = "cI[9W33]") : <text>:1:5: unexpected symbol
1: cI[9W33
        ^

For an example in ggplot:

data(mtcars)
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("cI[123]", "cI[ABC]", "cI[456]"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2, labeller = label_parsed)

That works fine, but:

data(mtcars)
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("cI[AB3]", "cI[2CD]", "cI[EF1]"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2, labeller = label_parsed)

...does not.

1 Answers
Related