Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 787
PopcornKing Avatar asked Dec 19 '15 00:12

PopcornKing


1 Answers

This works:

a0 <- ggplot()
a0 <- a0 + geom_point(aes(x=seq(0,1,0.1), y=seq(0,1,0.1)))
a0 + annotate("text", x=0.5, y=0.1, label="'' %+-% '' ", parse=TRUE)

The key idea is that %+-% is an operator, so it has to operate on something, i.e. it has to be in the form x %+-% y; in this case I've made x and y be blank strings.

like image 152
Ben Bolker Avatar answered Sep 25 '22 18:09

Ben Bolker