[英]How to draw confidence bounds to better demonstrate the confidence interval on ggplot2?
該代碼繪制了回歸線,置信區間和預測區間。 置信區間以灰色顯示。 我需要添加代碼來繪制置信區間,以更好地展示置信區間。
library(ggplot2)
x<-Allele<- c(4,4,5,5,5,7,9,11,11,11,13,14,15,15,16,17,17,19,19,19)
y<-SR<- c(5.5,5.6,5.7,6,6.1,8,8,12,12.1,13,14,14,16,16.5,17,17,
17.1,18,19,20)
D3S = data.frame(Allele, SR)
Mod <- lm(SR ~ Allele)
pred.int <- predict(Mod, interval = "prediction")
mydata <- cbind(D3S, pred.int)
# Regression line + confidence interval
p <- ggplot(mydata, aes(Allele, SR)) +
geom_point(size=3) +
stat_smooth(method = lm, color="black")
# Add prediction intervals
myplot= p + geom_line(aes(y = lwr), color = "black",
linetype = "dashed", size=0.8)+
geom_line(aes(y = upr), color = "black",
linetype = "dashed", size=0.8)
myplot + theme_bw() + theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))
您幾乎在原始代碼中擁有它。 使用interval = "confidence"
重復預測,並將這些線添加到繪圖中。
# Right after you get the prediction intervals:
conf.int <- predict(Mod, interval = "confidence")
# Change the cbind line to this:
mydata <- data.frame(D3S, pred.int, conf.int) # checks names and adds .1 to dupes
# Right after you added prediction interval, do this:
myplot <- myplot +
geom_line(aes(y = lwr.1), color="red") +
geom_line(aes(y = upr.1), color="red")
現在像以前一樣制作您的最終劇情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.