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