-2

I am pretty much an R novice, and cannot find the correct code to do, it seems to mostly be people asking to do it for multiple lines at a time).

Essentially I have created a scatter-plot in g g plot 2 and used a linear regression (l M). I am interested in working out what the intercept and the gradient of that linear regression is. What sort of code could I use to do this?

Hoping that this is an easy problem :) Thanks!

Fons MA
  • 1,142
  • 1
  • 12
  • 21
  • Is this a regression created in the plot with something like `geom_smooth`? Based on [this answer](https://stackoverflow.com/questions/9789871/method-to-extract-stat-smooth-line-fit), it looks like it's possible but difficult. Would it work for your purposes to create your regression separately with `lm` and use those coefficients to add a line to the plot? – A. S. K. Jan 21 '19 at 06:32
  • Can you provide a minimal example of what you're working with? Something that shows your data and how you use a linear regression? – Werner Jan 21 '19 at 06:34
  • Hi there, Fons MA has sorted me out cheers. Essentially the suggested code is very similar to what I am doing. I had trouble loading the code in the format that was required. otherwise, that would have been here too. Thanks for the trouble! – Annemieke Jan 21 '19 at 10:47

1 Answers1

4

As A.S.K. notes in the comments, there's no straightforward way of doing it from ggplot itself.

Fortunately, rewriting the linear regression is super simple. By way of example, I use the mtcars dataset with info about, well, cars, which is pre-loaded onto R.

Supposing your code looks something like this:

require(ggplot2)
#> Loading required package: ggplot2

ggplot(data = mtcars, mapping = aes(x = mpg, y = hp)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)

Yielding a pretty plot like the one below

Then you can just use the following code:


summary(lm(formula = hp ~ mpg, data = mtcars))

which will yield the information you need for that linear model.

#> 
#> Call:
#> lm(formula = hp ~ mpg, data = mtcars)
#> 
#> Residuals:
#>    Min     1Q Median     3Q    Max 
#> -59.26 -28.93 -13.45  25.65 143.36 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)   324.08      27.43  11.813 8.25e-13 ***
#> mpg            -8.83       1.31  -6.742 1.79e-07 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 43.95 on 30 degrees of freedom
#> Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
#> F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07

Going forward, please do check out some intro to R like Datacamp's Quick-R or Hadley Wickham's R for Data Science. You'll be figuring out these questions yourself in no time.

Fons MA
  • 1,142
  • 1
  • 12
  • 21
  • Hi there Fons MA, I was already doing this with the code. Apparently, I do not understand it all. In particular where the gradient of the trend line is mentioned. I will go away and look into it further. My apologies for my ignorance. :) – Annemieke Jan 21 '19 at 07:58
  • @Annemieke, the gradient is the Estimate value right underneath the (Intercept). -8.83 in the example I gave you. – Fons MA Jan 21 '19 at 08:17
  • Hey there Fons MA, cheers for that, was just getting round to mentioning that I think I had solved it. Cheers for your help, and thank-you for the links, I will work my way through them. :) – Annemieke Jan 21 '19 at 10:49