2

The axis labels vary for a ggplot that I create within a function. Some of the labels have super/subscripts, while others don't. Example:

  m.data <- data.frame(x = runif(10), y = runif(10))
  x.labs <- c("rain, mm", "light*','~W~m^-2")  
  for (i in 1:2) {
     ggplot(m.data, aes(x = x, y = y)) +
        labs(title = bquote(.(x.labs[i])))
  }

The title for the graph when i=2 is literally

light*','~W~m^-2

rather than the formatted version of same. With the same result, I also tried moving bquote inside each string.

 x.labs <- c("bquote(rain*','~mm)", "bquote(light*','~W~m^-2)")

and

 title = x.labs[i]  

Of the many questions about ggplot and bquote, none seem to address passing in a symbol like the superscript indicator.

InColorado
  • 308
  • 2
  • 12

1 Answers1

1

One alternative is to use expression() in your vector of titles instead of bquote().

For example

x.labs <- c("rain, mm", expression("light,"~W~m^-2))

ggplot(m.data, aes(x = x, y = y)) +
    labs(title = x.labs[2])

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118