Search code examples
rggplot2plotmath

How to escape predefined symbols e.g. % with plotmath?


I'm using the plotmath support while adding annotations to my plot i.e. using the parse=TRUE argument. Reviewing the plotmath documentation here it is not clear how to escape predefined symbols e.g. %

label <- 'atop(This~goes~on~top,of~this~with~11.1%)' # how to escape the % sign?
geom_text(...,label=label,parse=TRUE)

which leads to the following error:

Error in parse(text = as.character(lab)) : <text>:1:40: unexpected input
1: atop(This~goes~on~top,of~this~with~11.1%)
                                          ^

Solution

  • Just put it in quotes

    label <- 'atop(This~goes~on~top,of~this~with~"11.1%")'
    ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
        annotate("text", x = 4, y = 25, label = label, parse=TRUE)
    

    Just put all test in quotes

    label <- 'atop("This goes on top of this with 11.1%")'