Skip to content

Plotmath

Mara Averick edited this page Nov 2, 2018 · 4 revisions

Note: The ggplot2 wiki is no longer maintained, please use the ggplot2 website instead!

Text labels

library(ggplot2)
p = ggplot(data.frame(x=1,y=1,label="alpha")) 
 
p + geom_text(aes(x,y,label=label))

## use the parse argument to interpret the plotmath expression
( p2 = p + geom_text(aes(x,y,label=label), parse=TRUE) )

Axis labels, ...

p2 + xlab(expression(beta)) + opts(title = expression(gamma))

Facet labels

qplot(x,y, data=data.frame(x=1:2,y=1:2, f = factor(c("alpha","beta"))), geom="point") +
facet_grid(.~f, labeller= label_parsed)

Legend labels

d <- data.frame(x=1:10,y=1:10,f=rep(c("alpha","beta"),each=5), stringsAsFactors=FALSE)

value <- 3.1415
my.labs <- list(bquote(alpha==.(value)),bquote(beta==.(value)))

qplot(x,y,data=d,colour=f) +
  scale_colour_manual(values=1:2,breaks=c("alpha","beta"),
                      labels=my.labs)

Note: The ggplot2 wiki is no longer maintained, please use the ggplot2 website instead!

Clone this wiki locally