ggplot format italic annotation

Right now this page is the top search result on google for ggplot annotate italic. For the benefit of those who simply want to italicize an entire annotation, I'm writing this post. Use annotate's fontface option. Example:

seq(0,3.14,0.01)
qplot(x, sin(x)) +   # works the same for qplot or ggplot
annotate(geom = 'text', 
         x = 1.5, 
         y = 0.5, 
         hjust = 0.5,
         label = 'Hello, World', 
         fontface = 'italic')

enter image description here


Use parse=TRUE and supply a string formatted according to ?plotmath.

p <- ggplot(d, aes(i, value, linetype=variable)) +
    geom_hline(yintercept=700^2) +
    geom_line() +
    scale_linetype_manual(values=c(2,1)) +
    scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
    scale_y_sqrt() +
    annotate('text', 8*365/7, 1e3,
             label="P(italic(i))==8~italic(i)", parse=TRUE,
             hjust=1, size=3) +
    annotate('text', 8*365/7, 2.5e5,
             label="A(italic(i))==1+4~italic(i)(italic(i)-1)", parse=TRUE, 
             hjust=1, size=3)

enter image description here

Tags:

R

Ggplot2