Search code examples
rggplot2dplyrregressionggpmisc

Pull out Regression Eqn from ggplot


Here is a reproducible example of my problem:

library(ggplot2)
library(dplyr)
library(ggpmisc)

df <- mtcars %>%
  filter(cyl==4)

ggplot(mtcars,aes(x=disp,y=hp))+
  geom_point() +
  stat_poly_line() + stat_poly_eq(use_label(c("eq")))

I want to be able to pull out the slope from regression equation that's plotted here on this ggplot chart. I have tried assigning the ggplot to a value of p and then checking the contents of p, but have not been able to find anything that looks quite like a regression equation.

Can someone help point me in the right direction? The end goal is to pull out the slope for each set of data when cyl = 4, 6, and 8. Then, I would place all of these #s in a dataframe that would look like the following:

Cyl Slope
4   0.339
6  -0.300
8   0.089

Solution

  • You honestly don't need to pull the equations out of a plot. The plot calculates the coefficients internally using lm, so you can do the same in just a couple of lines:

    do.call(rbind, lapply(c(4, 6, 8), function(x) {
      data.frame(Cyl = x,
                 Slope = round(lm(hp ~ disp, mtcars[mtcars$cyl == x,])$coef[2], 3))
      })) |> 
      `row.names<-`(NULL)
    #>   Cyl  Slope
    #> 1   4  0.339
    #> 2   6 -0.300
    #> 3   8  0.089