0

I'm trying to label the mean y value with its unit (m^2/ha) in a faceted plot in an annotation to the plot.

Answers have already been provided for the axis labels and the strip label, or for math only without text, but these answers do not work for annotation.

library(ggplot2)
my.df <- data.frame(grp=c("A", "A", "B", "B"), x=c(1,2,1,2),y=c(3,5,6,8))
my.avg <- data.frame(grp=c("A", "B"), mean=c(4,7))
p1 <- ggplot(my.df, aes(x, y)) + geom_point() + facet_wrap(~grp)
p1 <- p1 + geom_text(data=my.avg, aes(x=1.5, y=7, label=paste("Avg =", mean, "m2/ha")))
p1

where m2/ha is with the superscript.
I can make it work without "Avg =".
I tried expression(), but it doesn't work in this particular case.
What is the right function here?

user13424
  • 111
  • 1
  • 4
  • Your code produced a plot with the labels "Avg = 4 m2/ha" and "Avg = 7 m2/ha". Is this not what you were expecting? – Brandon Feb 24 '18 at 18:31
  • No. I'm want the "2" to appear in superscript as in m^2/ha. – user13424 Feb 24 '18 at 19:04
  • Possible duplicate of [putting mathematical symbols and subscripts mixed with regular letters in R/ggplot2](https://stackoverflow.com/questions/15125628/putting-mathematical-symbols-and-subscripts-mixed-with-regular-letters-in-r-ggpl) – wici Feb 24 '18 at 19:25

1 Answers1

1

try

geom_text(data=my.avg, aes(x=1.5, y=7, label=paste("Avg ==", mean, "*m^2/ha")), parse=TRUE)