创建具有 95% 可信区间的条形图

Creating a bar graph with 95% credible intervals

我正在尝试创建一个条形图来显示 PLE 的百分比及其对应的 95% 可信区间。我想在水平线上有年龄组,在垂直线上有百分比和 Non_C、CG(<14h/w)、CG (>=14h/w) 作为条形。鉴于我有大约 20 tables,我想知道 R 代码是否可以创建我感兴趣的图表。 我想比较不同年龄组的 PLE 和它们周围的 95% 可信区间(最小值、最大值)。

下面的代码将下面的 table 转换成数据框。

    structure(list("Caregiving status" = c("Non-C", "Non-C","Non-C","CG<14h/w", "CG<14h/w","CG<14h/w","CG>=14h/w","CG>=14h/w","CG>=14h/w"),

  Age = c("50-51", "60-61", "70-71", "50-51", "60-61", "70-71","50-51", "60-61", "70-71"), 

                 PLE = c(78,78,78,81,81,80,78,78,77),
                 Min = c(78,77,76,79,79,78,75,74,74),
                 Max = c(79,79,79,83,83,83,80,80,80)), 
               class = "data.frame", row.names = c(NA, -9L))

以下代码为您提供了一个带有误差条的简单条形图。还不是很好,但作为起点...要调整错误栏的宽度,...请查看 position_dodge2.

的文档
library(ggplot2)

df <- structure(list(caregiving_status = c("Non-C", "Non-C","Non-C","CG<14h/w", "CG<14h/w","CG<14h/w","CG>=14h/w","CG>=14h/w","CG>=14h/w"),

               age = c("50-51", "60-61", "70-71", "50-51", "60-61", "70-71","50-51", "60-61", "70-71"), 

               ple = c(78,78,78,81,81,80,78,78,77),
               min = c(78,77,76,79,79,78,75,74,74),
               max = c(79,79,79,83,83,83,80,80,80)), 
          class = "data.frame", row.names = c(NA, -9L))

ggplot(df, aes(age, group = caregiving_status)) +
  geom_col(aes(y = ple, fill = caregiving_status), position = "dodge2") +
  geom_point(aes(y = ple), position = position_dodge2(.9)) +
  geom_errorbar(aes(ymin = min, ymax = max), position = position_dodge2(padding = .5)) +
  labs(x = "Age", y = "PLE", fill = "Caregiving Status")

reprex package (v0.3.0)

于 2020 年 3 月 11 日创建