18

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)
0

2 Answers 2

32

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="±")
Sign up to request clarification or add additional context in comments.

Comments

12

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.

You could also use phantom():

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

If you want to use full LaTeX-style markup like \pm you need to go down the rabbit hole of tikzDevice. However, latex2exp::TeX can often provide a useful hint, e.g.:

latex2exp::TeX("\\pm")
##    LaTeX: \pm 
## plotmath: phantom() %+-% phantom() 

2 Comments

phantom also works, although it is almost completely undocumented
Reading and re-reading the plotmath page is necessary. And actually it is phantom() that is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.