1

I'd like to manually add a regression equation to a plot that includes the proper mathematical notation (e.g., italics, superscripts). Annotate() seems perfect for this, but it doesn't seem to accept the expression() function in the labels argument. Here is a simple example. The annotation with expression() in it won't display. What am I doing wrong?

 library(ggplot2)
 x <- 1:10
 y <- x^2
 df <- data.frame(x, y)
 sp <- ggplot(df, aes(x, y)) + geom_point()
 sp + annotate('text', label = expression('y = x^2'), x = 2.5, y = 50) +
      annotate('text', label = 'y = x^2', x = 2.5, y = 75)
grmccreath
  • 13
  • 1
  • 3

1 Answers1

2

Use geom_text instead of annotate:

sp + geom_text(aes(2.5,75, label=(paste(expression("y = x "^-2*"")))),parse = TRUE)
Geo-sp
  • 1,704
  • 3
  • 19
  • 42