r - 如何在 ggplot2 中绘制置信区间限制的虚线?工作室R

标签 r ggplot2

enter image description here

ggplot(data=Dane, aes(x=reg$fitted.values, y=reg$residuals))+ 
geom_smooth(method="lm", se=TRUE, level=0.95)+ 
theme(panel.background = element_rect(fill = "white", colour = "grey50"))+ 
geom_point()

最佳答案

以下内容将使用内置数据集 iris 的子集,其中 Species == "setosa"

请注意,为了获得预测的置信带,必须在绘图之前拟合线性模型。

library(ggplot2)

data(iris)

subdf <- iris[iris$Species == "setosa", ]

pred <- predict(lm(Sepal.Width ~ Sepal.Length, subdf), 
                se.fit = TRUE, interval = "confidence")
limits <- as.data.frame(pred$fit)

ggplot(subdf, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  theme(panel.background = element_rect(fill = "white", 
                                       colour = "grey50"))+
  geom_smooth(method = "lm") +
  geom_line(aes(x = Sepal.Length, y = limits$lwr), 
            linetype = 2) +
  geom_line(aes(x = Sepal.Length, y = limits$upr), 
            linetype = 2)

enter image description here

关于r - 如何在 ggplot2 中绘制置信区间限制的虚线?工作室R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53598044/

相关文章:

r - 如何使用R精确匹配整个数据集中的两列值

r - 使用 R 动画库和 Imagemagick 保存 gif 视频时出错

基于行的汇总计算

r - R 中的动态范围问题

r - 组合 data.table 中的条件集以使用二进制搜索提取值

r - 在ggplot2中添加加权最小二乘趋势线

r - 注释绘图边缘而不更改绘图限制或将 "expand"设置为 0

r - 为条形图定义一个固定的宽度/高度,然后设置条形的绝对宽度和条形之间的绝对间距,以像素为单位

r - ggplot直方图的极值

r - 没有轴或网格的 ggplot2 主题