gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:11:09 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com