I try to add the R squared value in my plot using ggplot2.
I want to put this value in the center position of this plot.
Could someone tell me how to automatically get the center position (xposition and yposition values in my code)? Because I have a bunch of plots to make, I need to get these two values automatically.
Below is my code:
xposition <- 2.5
yposition <- 2.5
plotdata <- data.frame(x=1:10,y=2*(1:10))
relation <- lm(plotdata$y ~ plotdata$x)
lb <- paste("R^2 == ",round(summary(relation)$r.squared,2))
pp <- ggplot(data=plotdata,aes(x=x, y=y)) + geom_point(colour="blue",size=1.5)
pp <- pp + geom_smooth(method = "lm", se = FALSE)
pp <- pp + annotate("text",x=xposition,y=yposition,label=lb, parse=TRUE,size=6)