0

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)
Love_qq_xq
  • 148
  • 13

1 Answers1

2

Center plot

Is this something that you are looking for?

pp + annotation_custom(grid::textGrob(lb), + xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)

annotation_custom is the function that should find the center position.

Shreyas
  • 999
  • 6
  • 19