Text annotation ggplot

Can markup be used in ggplot annotation?

Let's say I have this graph:

p <- function(i) 8*i a <- function(i) 1+4*i*(i-1) library(ggplot2) library(reshape2) i <- 1:(8*365/7) d <- data.frame(i=i,p=p(i),a=sapply(i,a)) d <- melt(d, id.vars='i') p <- ggplot(d, aes(i, value, linetype=variable)) + geom_hline(yintercept=700^2) + geom_line() + scale_linetype_manual(values=c(2,1)) + #geom_point() + scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) + #scale_y_continuous(breaks=c(0,700^2), labels=c(0,expression(L^2))) scale_y_sqrt() + #scale_y_log10() + annotate('text', 8*365/7, 1e3, label="P(i)=8i", hjust=1, size=3) + annotate('text', 8*365/7, 2.5e5, label="A(i)=1+4i(i-1)", hjust=1, size=3) print(p + theme_classic()) 

output

I know that I can use fontface = 3 and put everything in italics. But I do not want the numbers to be in italics, but only the variable i . Preferably P and A are not italic.

Any ideas?

+8
r ggplot2
source share
3 answers

Use parse=TRUE and put the 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

+12
source share

Right now this page is the best google search result for ggplot annotating italics. In the interest of those who just want to italicize an entire annotation, I am writing this post. Use the fontface option fontface . 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

+9
source share

A better response rating is good, but in a more complex scenario with linebreaks, this did not work for me, so instead I used unicode characters instead. For your example:

 library(Unicode) italic_i <- u_char_inspect(u_char_from_name("MATHEMATICAL ITALIC SMALL I"))["Char"] label1 <- paste("P(", italic_i, ")=8", italic_i, sep="") label2 <- paste("A(", italic_i, ")=1+4", italic_i, "(", italic_i, "-1)", sep="") i <- 1:(8*365/7) d <- data.frame(i=i,p=p(i),a=sapply(i,a)) d <- melt(d, id.vars='i') p <- ggplot(d, aes(i, value, linetype=variable)) + geom_hline(yintercept=700^2) + geom_line() + scale_linetype_manual(values=c(2,1)) + #geom_point() + scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) + #scale_y_continuous(breaks=c(0,700^2), labels=c(0,expression(L^2))) scale_y_sqrt() + #scale_y_log10() + annotate('text', 8*365/7, 1e3, label=label1, hjust=1, size=3) + annotate('text', 8*365/7, 2.5e5, label=label2, hjust=1, size=3) print(p + theme_classic()) 

Italian abstract ggplot2

Edit: I just noticed that saving a PDF file using pdf () doest does not display unicode correctly, but you can just use cairo_pdf (), which works just fine (see Unicode characters in ggplot2 PDF Output )

+4
source share

All Articles