Search code examples
rggplot2axis-labelssubscriptsuperscript

Superscript and subscript in one axis label


I'm trying to code for a label that says "13CO2%" in ggplot. My sample code as guessed from other posts here is

xlab("Treatment") + ylab(expression(paste("^13, CO[2], %"))) +

This results in verbatim pasting including the arrows and brackets. Can anyone spot the issue, I'm sure it's nothing major but I think I'm following the basic rules of superscript=^ and subscript = [].

`xlab("Treatment") + ylab(expression(paste("^13, CO[2], %"))) +'


Solution

  • No need to use paste. You could go on with expression. However, note the use of * to glue the parts of your expression which are needed to make it a valid expression. Also, you have to quote the % symbol and use an invisible phantom() to which you could add your superscript.

    library(ggplot2)
    
    ggplot(mtcars, aes(hp, mpg)) +
      geom_point() +
      ylab(expression(phantom()^{13}*CO[2]*"%"))