Search code examples
rtextplotggplot2latex

How to place +/- plus minus operator in text annotation of plot (ggplot2)?


I have tried the following, but it doesn't work for me:

a <- ggplot()
a <- a + geom_point(aes(x=seq(0,1,0.1), y=seq(0,1,0.1)))
a <- a + annotate("text", x=0.5, y=0.3, label="myplot")
a <- a + annotate("text", x=0.5,y=0.2,label=expression(%+-%))

I have also tried the following as pointed out by How to annotate() ggplot with latex with no luck:

a <- a + annotate("text", x=0.5, y=0.1, label="%+-%", parse=TRUE)

And this doesn't work either:

a <- a + annotate("text", x=0.5, y=0.1, label="\pm", parse=TRUE)

Solution

  • It is possible to use the unicode representation (\u00B1):

    a <- ggplot()
    a <- a + geom_point(aes(x=seq(0,1,0.1), y=seq(0,1,0.1)))
    a <- a + annotate("text", x=0.5, y=0.3, label="myplot")
    a + annotate("text", x=0.5, y=0.2, label="\u00B1")
    

    Or you can use the ± symbol directly, by copying and pasting it from somewhere.

    a + annotate("text", x=0.5, y=0.2, label="±")