0

I'd like to write a model equation in the graph.

nitrogen<- c(10,15,20,25)
yield<- c(50,70,85,30)
dataA<- data.frame(nitrogen,yield)

ggplot(data=dataA, aes(x=nitrogen, y=yield))+
  geom_point (col="Black", size=4) +
  stat_smooth(method='lm', linetype=1, se=FALSE, formula=y~poly(x,2, raw=TRUE), size=0.5, color="Blue") +
  windows(width=5.5, height=5)

Now I'd like to write below equation inside the graph. I know there are many questions about this, but I cannot find a solution, and still struggling. So I ask again. Sorry for duplicated question.

Could you please let me know how to write both superscript and subscript text?

Always many thanks!!

enter image description here

enter image description here

Jin.w.Kim
  • 599
  • 1
  • 4
  • 15
  • 2
    Where? As an axis label, as a `geom_text`, an an `annotate`ion, a title, or something else? – r2evans Nov 02 '22 at 22:16
  • 1
    To write math in general, see [`help("plotmath")`](https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html). To add the regression equation to a `ggplot` graph, see [here](https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph). I don't hammer close as a duplicate because of the subscripts part of the question. – Rui Barradas Nov 02 '22 at 22:34
  • @r2evans whereever inside graph, i.e. x=20, y=70 ? – Jin.w.Kim Nov 02 '22 at 22:42

1 Answers1

2
library(ggplot2)

nitrogen<- c(10,15,20,25)
yield<- c(50,70,85,30)
dataA<- data.frame(nitrogen,yield)
coef = coef(lm(yield ~ poly(nitrogen,2, raw=TRUE)))

ggplot(data=dataA, aes(x=nitrogen, y=yield))+
  geom_point (col="Black", size=4) +
  stat_smooth(method='lm', linetype=1, se=FALSE, formula=y~poly(x,2, raw=TRUE), size=0.5, color="Blue") +
  windows(width=5.5, height=5) + 
  annotate("text", x=15, y=16, label = bquote(~ Y[i][j]==.(coef[1])+.(coef[2])*x+.(coef[3])*x^2))

enter image description here

Edit This version gives more control on formula display:

ggplot(data=dataA, aes(x=nitrogen, y=yield))+
  geom_point (col="Black", size=4) +
  stat_smooth(method='lm', linetype=1, se=FALSE, formula=y~poly(x,2, raw=TRUE), size=0.5, color="Blue") +
  windows(width=5.5, height=5) + 
  annotate("text", x=15, y=16, label = bquote(
    ~ paste(Y[i][j],"=", .(sprintf("%.2f", coef[[1]])), .(sprintf("%+.2f",coef[[2]])),x, .(sprintf("%+.4f",coef[[3]])),x^2)))
Ric
  • 5,362
  • 1
  • 10
  • 23
  • Thank you so much!! How about writing inside graph? i.e. x=20, y=70 ? – Jin.w.Kim Nov 02 '22 at 22:51
  • just use `annotate("text", x=15, y=15, label = bquote(~ Y[i][j]==.(coef[[1]])+.(coef[[2]])*x+.(coef[[3]])*x^2))` instead of `ggtitle`, and adjust x and y – Ric Nov 02 '22 at 22:55
  • If you prefer more control to numeric format (also prevent the last "+-" in the formula), you can use sprintf as follows: `label = bquote(~ paste(Y[i][j],"=", .(coef[[1]]), .(sprintf("%+f",coef[[2]])),x, .(sprintf("%+f",coef[[3]])),x^2))` – Ric Nov 02 '22 at 23:02
  • If I use annotate(), it says Error:! All columns in a tibble must be vectors. x Column `label` is a call. Could you check this for me? – Jin.w.Kim Nov 02 '22 at 23:02
  • But thank you so much!! The reason I wanted to write an equation was when I use 'stat_poly_eq()', the equation model does not reflect blocks. So model equation inside graph was different from my model, 'lm(y~poly(x,2,raw=TRUE)+ factor(Rep)'. So I was trying to write directly. this code '(coef[1])+.(coef[2])*x+.(coef[3])*x^2)' is so nice. I can reflect blocks now when I set up like 'coef(lm(y~poly(x,2,raw=TRUE)+ factor(Rep)' Thank you so much!!! You saved my whole day. – Jin.w.Kim Nov 02 '22 at 23:08
  • I edited the example to put the formula inside the plot. – Ric Nov 02 '22 at 23:14