简体   繁体   中英

Receive the equation of the stat_smooth in ggplot2 R mtcars example

Hi I would like to know how can I retrieve the equation of stat_smooth either in the ggplot2 or in a vector or somewhere else. the code that I am using is:

p <- ggplot(data = mtcars, aes(x = disp, y = drat)) 
p <- p + geom_point() + stat_smooth(method="loess") 
p

Thanks

The ggpmisc package can be very usefull. However, it will not work with loess as loess doesn't give a formula. See here: Loess Fit and Resulting Equation

library(ggplot2)
library(ggpmisc)
p <- ggplot(data = mtcars, aes(x = disp, y = drat)) +
  geom_point() + 
  geom_smooth(method="lm", formula=y~x) +
  stat_poly_eq(parse=T, aes(label = ..eq.label..), formula=y~x)
p

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM