7

I cant work out how to get the regression line equation, r^2 and p value of the linear regression I have plotted using the function geom_smooth.

This is my code:

   g <- ggplot(data=data.male, aes(x=mid_year, y=mean_tc, colour=data.male$survey_type))  
   g <- g + geom_point(shape = 20, size =2) 
   g <- g + geom_smooth(method=lm, na.rm = FALSE, se = TRUE, aes(group=1), colour = "black")
   g <- g + theme_gray(base_size=24)
   g <- g+ xlab("Year")
   g <- g + ylab("Mean serum total cholesterol (mmol/L)")
   g <- g + theme(legend.position="bottom")
   g <- g + scale_y_continuous(limits=c(3.5,6.5), breaks=c(3.5,4,4.5,5,5.5,6,6.5))
   g <- g + scale_x_continuous(limits=c(1980,2015), breaks=c(1980,1990,2000,2010))
   g <- g + scale_colour_manual(name = "Survey Type", values= c("Red", "Blue", "Green")) 
   g  

[1]:

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Nadiah
  • 175
  • 2
  • 8
  • As @Spacedman says in his answer, why not fit the model yourself and extract the necessary data? – Heroka May 21 '16 at 17:08
  • if you see the answer [here](http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph), you'll also see that it uses `lm` internally as well. – Ben Bolker May 21 '16 at 17:09
  • see also: http://stackoverflow.com/questions/10302851/extract-coefficients-from-ggplot2-created-nls-fit – Ben Bolker May 21 '16 at 17:11
  • Sorry @BenBolker do you mean that even if I dont use `lm` to fit the plot but use this code line afterwards `fit1 <- lm(mean_tc ~ mid_year, data = data.male) summary(fit1)` that it will display the same stats as I would get from the line in ggplot2? – Nadiah May 21 '16 at 17:16
  • yes (as @Spacedman suggests below) – Ben Bolker May 21 '16 at 17:23

1 Answers1

6

Don't use a plotting function for modelling. Fit the model using the lm function.

Then use the summary method to get everything you need to know about the fit.

You should get the same results as the plotting function, which I suspect uses lm internally.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 1
    Thanks for this! How do I edit the code to fit the model with 'lm' instead of 'geom_smooth'? – Nadiah May 21 '16 at 17:13
  • 1
    The "geom_smooth" function takes a parameter called "method", where you can specify "method='lm' ", so essentially the geom_smooth function will fit the model using "lm" itself. – AHegde Jul 10 '17 at 18:13
  • I don't agree with this answer. If you are making the plot anyway, why duplicate effort to redo an identical fit again. This is especially the case for a facet plot where you then have to do multiple fits. – Nick Jun 10 '20 at 12:55
  • In that case submit a PR to ggplot2 suggesting some documented and supported methods for getting the fitted model out of ggplot2 objects. Because at the moment the only way to do it is figure out where ggplot2 objects store these things hope it doesn't change in the next update. As of now, this is the best answer. There's nothing to "agree" with. – Spacedman Jun 10 '20 at 13:13