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!!
CodePudding user response:
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))
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)))