我想在置信区间的x位置上绘制垂直线。我进行了统计,但是找不到将其添加到绘图中的方法。请遵循此MWE:
xseq<-seq(-4,4,.01)
densities<-dnorm(xseq, 0,1)
par(mfrow=c(1,3), mar=c(3,4,4,2))
plot(xseq, densities, col="darkgreen",xlab="", ylab="Densidade", type="l",lwd=2, cex=2, main="Normal", cex.axis=.8)
ci为:
x<-t.test(xseq, conf.level = 0.95)$conf.int
但是当我尝试用以下方式绘制线条时:
line(x[1], x[2])
它给了我错误:
Error in structure(.Call(C_tukeyline, as.double(xy$x[ok]), as.double(xy$y[ok]), :
insufficient observations
评论指出abline()后有效:
但是,我认为t.test将使顺式呈正态分布是错误的。
答案 0 :(得分:1)
使用ggplot
:
library(ggplot2)
df <- as.data.frame(cbind(xseq, densities))
plot <- ggplot(data = df, aes(x=xseq, y=densities)) +
geom_point() + geom_vline(xintercept = c(x[1], x[2]))
plot
以适当的置信区间:
x2 <- qnorm(c(0.05, 0.95), mean = mean(xseq), sd = sd(xseq))
plot <- ggplot(data = df, aes(x=xseq, y=densities)) +
geom_point() + geom_vline(xintercept = c(x2[1], x2[2]))
plot