lochi
lochi

Reputation: 880

ggplot2 annotation with superscripts

How do I include a superscript in ggplot annotation? I want to display Rsuperscript2 = somevalue I tried using parse=TRUE inside annotate.. It gave me = Rsuperscript2 , somevalue instead

lm1 <- lm(dData$RF ~ dData$Exp -1)
lb1 <- paste("R^2 = ", round(summary(lm1)$r.squared,4))
p1 <- ggplot(dData, aes(x=dData$Exp, y=dData$RF)) +
  scale_x_continuous("Experimental") + 
  scale_y_continuous("Predicted") + 
  geom_point() + geom_smooth(method="lm") + 
  annotate("text", x=max(dData$Exp), y=min(dData$RF)+1, label=lb1, 
           hjust=1, size=3, vjust=1)

Upvotes: 17

Views: 23966

Answers (1)

Brian Diggs
Brian Diggs

Reputation: 58855

Is the problem with superscripts or with the equals sign? Switching to == in the expression, with parse=TRUE works for me. Not having your dData, here is a dummy example.

lb1 <- paste("R^2 == ", round(runif(1),4))
qplot(1:10, 1:10) + 
  annotate("text", x=2, y=8, label=lb1, parse=TRUE)

enter image description here

Upvotes: 41

Related Questions