繁体   English   中英

在回归线 R 中添加 +- 2 SE

[英]Add +- 2 SE in regression line R

我想知道是否可以在 R 中绘制一条回归线,包括 +- 2* 标准误差。 例如,使用iris

library(ggplot2)
ggplot(iris, aes(x = Petal.Length, y = Sepal.Width))+geom_smooth(method=lm, color="black")+ theme_bw()

输出是:

在此处输入图片说明

如何(如果可能)将灰色区域扩展为标准误差的两倍?

实现所需结果的一种选择是通过stat_smooth手动添加置信stat_smooth并使用after_stat

library(ggplot2)
ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) +
  geom_smooth(method = lm, color = "black", se = FALSE) +
  stat_smooth(aes(ymin = after_stat(y - 2 * se), ymax = after_stat(y + 2 * se)), geom = "ribbon", method = lm, fill = "grey60", alpha = .4) +
  theme_bw()
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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