Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 annotation with superscripts

Tags:

r

ggplot2

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)
like image 576
lochi Avatar asked Mar 15 '12 15:03

lochi


People also ask

How to write superscript in ggplot?

To add superscript as a title add bquote function with value inside ggtitle(). Parameter : like xlab and ylab functions, we can give the title for plot directly using this function. Here we will bquote() function for writing Superscript value ( Number VS Number2 ) as a title of plot.

How do you add a superscript in R?

To indicate a subscript, use the underscore _ character. To indicate a superscript, use a single caret character ^ . Note: this can be confusing, because the R Markdown language delimits superscripts with two carets. In LaTeX equations, a single caret indicates the superscript.

How do you annotate a plot in R?

If you want to annotate your plot or figure with labels, there are two basic options: text() will allow you to add labels to the plot region, and mtext() will allow you to add labels to the margins. For the plot region, to add labels you need to specify the coordinates and the label.


1 Answers

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

like image 156
Brian Diggs Avatar answered Oct 19 '22 02:10

Brian Diggs