Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add alpha-numeric AND greek characters to geom_text() in ggplot?

Tags:

r

ggplot2

latex

I am trying to create a plot that is annotated with text that contains both alpha-numeric and greek characters. If I want to add just greek characters I can use:

qplot(x, y) + geom_text(aes(2, 2, label="rho"), parse=TRUE)

How can I annotate a plot with "ρ and some other text"? I would like to do something like this:

qplot(x, y) + geom_text(aes(2, 2, label="rho and some other text"), parse=TRUE)

When I try the above code, I get this error:

Error in parse(text = lab) : <text>:1:5: unexpected symbol
1: rho and
       ^

I would also appreciate any solution that would allow me to use LaTeX in geom_text() for more complex use cases in the future.

like image 590
drbunsen Avatar asked Feb 12 '12 12:02

drbunsen


1 Answers

One solution: substitute tildes ~ for spaces.

d <- data.frame(x=1:3,y=1:3)
qplot(x, y, data=d) + geom_text(aes(2, 2,
              label="rho~and~some~other~text"), parse=TRUE)

Substituting * for ~ works if you want to juxtapose without spaces.

For the complete LaTeX solution I think you want to have a look at the tikzDevice package, possibly used in conjunction with knitr (the new coolest package on the block); e.g. see http://yihui.name/knitr/demo/graphics/

like image 170
Ben Bolker Avatar answered Oct 06 '22 18:10

Ben Bolker