information from regressions using geom_smooth

2,885 views
Skip to first unread message

Rosemary Hartman

unread,
Sep 7, 2016, 11:06:02 AM9/7/16
to ggplot2
Hello all,

If I am making a graph using ggplot, I can plot a trendline using geom_smooth()

For example:

df = data.frame(x=c(1,2,3,4,5,6,7), y= c(2,4,3,5,8,9,10))

p = ggplot(df, aes(x=x, y=y)) + geom_point() + geom_smooth(method = "lm")

Is there a way of extracting the information on the linear model used to plot the line and standard errors? Coefficients and R2 and such? I can always just use the lm function, but it would seem to save a step if I could make ggplot report it for me.

Thanks!
Rosemary Hartman

Vivek Patil

unread,
Sep 7, 2016, 12:24:58 PM9/7/16
to Rosemary Hartman, ggplot2

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brandon Hurr

unread,
Sep 7, 2016, 1:59:14 PM9/7/16
to ggplot2, roseh...@gmail.com
Rosemary, 

Hadley has expressed previously a few times that he feels that this should be done outside of ggplot as Vivek linked. Generally when this is the case, it is unlikely to be implemented. That said, you can do anything really. It might be worth looking at the functions. 


The output if you look at ggplot_build(p) is a dataframe within the list at data[[2]], which is the predicted data. The coefficients of the model are never transferred over. You could hack the code to do this perhaps, but this is much harder than computing them outside of ggplot and adding the annotation... sadly. 

B


On Wednesday, September 7, 2016 at 9:24:58 AM UTC-7, Vivek wrote:
On Wed, Sep 7, 2016 at 8:03 AM, Rosemary Hartman <roseh...@gmail.com> wrote:
Hello all,

If I am making a graph using ggplot, I can plot a trendline using geom_smooth()

For example:

df = data.frame(x=c(1,2,3,4,5,6,7), y= c(2,4,3,5,8,9,10))

p = ggplot(df, aes(x=x, y=y)) + geom_point() + geom_smooth(method = "lm")

Is there a way of extracting the information on the linear model used to plot the line and standard errors? Coefficients and R2 and such? I can always just use the lm function, but it would seem to save a step if I could make ggplot report it for me.

Thanks!
Rosemary Hartman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages