Drew Steen Drew Steen - 1 year ago 223
R Question

How to annotate() ggplot with latex

I'd like to add latex text to a ggplot2 plot using

. Using
, as described here for adding latex to axis labels, does not seem to work. To wit:

# Use expression() to create subscripted text
p <- ggplot(mpg, aes(x=cty, y=hwy)) + geom_point() +

# But expression() in annotate adds nothing to the plot
p + annotate("text", x=10, y=40, label=expression(text[subscript]))

# Passing regular text to annotate works fine
p + annotate("text", x=10, y=40, label="foo")

Why are
s treated differently by
than by other ggplot functions? And how can I annotate with latex?

Answer Source

You can use the parse argument, without expression:

p + annotate("text", x=10, y=40, label="text[subscript]", parse=TRUE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download