r - 如何用 ggplot2 (R) 绘制单个置信区间?
问题描述
我正在尝试在单个图表上显示单个置信区间ggplot
。
当前代码
d1<-rnorm(50,26,6)
d2<-rnorm(20,19,4)
all_test<-t.test(d1,d2)
mean_CI<-(all_test$conf.int[2]+all_test$conf.int[1])/2
ggplot(data.frame(x=c()), aes(x)) +
geom_segment(aes(x = 0, y = all_test$conf.int[1], xend = 0, yend = all_test$conf.int[2]), arrow = arrow(length = unit(0.02, "npc"),type = "closed",ends="both")) +
annotate("point", x = 0, y = mean_CI,size =4)+
geom_hline(yintercept = 0, linetype = 3, size =1.5) +
theme_classic()
电流输出
当前输出达到预期所需的关键调整:
- 用线段替换箭头末端
- 更改 x 缩放比例
预期产出
其他尝试
geom_errorbar()
-> 不成功geom_segment() + annotate()
-> 我认为这可能不是一个优雅的解决方案。
评论
我使用的原因ggplot2
是因为我创建了第二个ggplot
面板,我想将两者结合在同一页面上。
解决方案
使用geom_errorbar
andgeom_point
这可以像这样实现:
library(ggplot2)
set.seed(42)
d1<-rnorm(50,26,6)
d2<-rnorm(20,19,4)
all_test<-t.test(d1,d2)
mean_CI<-(all_test$conf.int[2]+all_test$conf.int[1])/2
d <- data.frame(x = 0,
y = (all_test$conf.int[2] + all_test$conf.int[1]) / 2,
ymin = all_test$conf.int[1],
ymax = all_test$conf.int[2])
ggplot(d)+
geom_errorbar(aes(x, ymin = ymin, ymax = ymax), width = .1) +
geom_point(aes(x, y), size = 4)+
geom_hline(yintercept = 0, linetype = 3, size =1.5) +
xlim(-.2, .2) +
theme_classic() +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())
推荐阅读
- html - 不透明的 CSS 动画不适用于 safari,但适用于谷歌浏览器
- c++ - 如何打开一个文本文件
- python - 如何将excel日期时间作为浮点数而不是xlwings中的'datetime.datetime'?
- python - 如何在 BeautifulSoup 上获得多个具有相同 html 标签的项目
- amazon-web-services - 如何获取通过 Amazon SES 发送的电子邮件的状态
- elasticsearch - 需要一个在预定时间前 5 分钟开始的弹性搜索查询过滤器范围
- java - 为什么原子不知道周期
- r - 在 R 中将 Excel 文件批量转换为 PDF
- javascript - 使用从上一页传入的输入值在页面加载时过滤列表
- excel - 如果单元格 A 中的数字发生变化,则增加新单元格中的值