Search code examples
rggplot2superscript

Paste string with superscript in ggplot


I am trying to pass a variable toPaste which is a string that I want part of it to be superscripeted. The string: "this^2/that^+" where 2 and + are desired to be superscripted.

I browsed around, and it seems, I need to reformat my string, and then I can paste() in a bquote() for example, but since the desired superscript is embedded in the middle of the string, I am not sure how to approach it. Sample script below:

library(ggplot2)
toPaste <- "this^2/that^+"
ggplot() + ylab(toPaste)

Any assistance is appreciated.


Solution

  • You can use the following:

    toPaste <- "this^2/that^'+'"
    ggplot() + ylab(parse(text = toPaste))
    

    Note that the "+" sign needs to be surrounded by single quotes.