Adding a regression line on a ggplot

前端 未结 5 1415
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 18:54

I\'m trying hard to add a regression line on a ggplot. I first tried with abline but I didn\'t manage to make it work. Then I tried this...

data = data.frame         


        
5条回答
  •  星月不相逢
    2020-11-29 19:15

    The simple solution using geom_abline:

    geom_abline(slope = coef(data.lm)[[2]], intercept = coef(data.lm)[[1]])
    

    Where data.lm is an lm object, and coef(data.lm) looks something like this:

    > coef(data.lm)
    (Intercept)    DepDelay 
      -2.006045    1.025109 
    

    The numeric indexing assumes that (Intercept) is listed first, which is the case if the model includes an intercept. If you have some other linear model object, just plug in the slope and intercept values similarly.

提交回复
热议问题