r - 如何从 R 中的 ggplot2 统计摘要中提取置信区间的值?
问题描述
我有两种密度的计数数据(级别:1 和 3)。我已经使用 R 中的 summary_stat 函数绘制了具有引导置信区间的原始数据。我想从此图中提取置信区间的上限和下限。我怎样才能做到这一点?
data <- data.frame(set = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4),
density = c(1, 3, 3, 1, 3, 1, 1, 1, 3, 3, 1, 3),
counts = c(100, 2, 3, 76, 33, 12, 44, 13, 54, 36, 65, 1),
ratio = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 90, 1))
data$density <- as.factor(data$density)
pd <- position_dodge(0.82)
library(ggplot2)
ggplot(data, aes(x=density, y=counts, fill=density)) +
theme_bw() +
stat_summary(geom="bar", fun.y=mean, position = "dodge") +
stat_summary(geom="errorbar", fun.data=mean_cl_boot, width = 0.1,
size = 1.2, col = "grey57", position = pd) +
ylab("Counts")
解决方案
您可以使用ggplot_build()
.
该函数为您提供两条信息:数据框列表(每层一个)和面板对象,其中包含有关轴限制、中断等的所有信息:
p <- ggplot(data, aes(x=density, y=counts, fill=density)) +
theme_bw() +
stat_summary(geom="bar", fun.y=mean, position = "dodge") +
stat_summary(geom="errorbar", fun.data=mean_cl_boot, width = 0.1,
size = 1.2, col = "grey57", position = pd) +
ylab("Counts")
plot_info <- ggplot_build(p)$`data`[[2]]
在您的情况下,所有相关信息都存储在 中的第二个列表的ymin
和ymax
列中$data
。
# density 1 error bounds
density1_error_min <- plot_info[1, 'ymin']
density1_error_max <- plot_info[1, 'ymax']
# density 3 error bounds
density3_error_min <- plot_info[2, 'ymin']
density3_error_max <- plot_info[2, 'ymax']
推荐阅读
- java - 将生产 Android 应用程序从 Windows 7 正确移动/移植到基于 Linux 的系统
- qt - Qt: How to implement simple internal drag&drop for reordering items in QListView using a custom model
- r - xaringan code output same style as bookdown
- agda - 在命令行使用 Agda 2.5.4.2 编译 Hello World 时出错
- java - JVM 线程是否会始终保持其到 OS 线程的映射
- ios - How to upload image in Swift using Moya?
- winforms - SkiaSharp:如何限制或降低大(4K)屏幕的绘图像素分辨率?
- php - 未定义索引但索引存在
- typescript - Typescript 类型中的嵌套三元条件扩展
- python - Get value from xml response in Python