繁体   English   中英

如何添加误差线和自定义堆积条形图

[英]How to add error bars and customize stacked bar chart

我有一个类似于下图的数据集和堆积条形图。 该图表是按治疗类型(trt=1 或 2)随时间变化的特定疾病的患病率(CVD=0 表示否,1 表示是)。

我想知道是否有人可以帮助我:

  1. 在每个访问次数下添加 N 作为样本量。 例如,在visit=0 下,N=7,在visit=1 下,N=7 等。
  2. 用 CVD 患病率的点估计值和置信区间的误差线对图表进行注释? 我希望流行率估计值具有 95% 的置信区间,以便更好地了解治疗 1 和 2 之间是否存在差异。
  3. 获得一个单独的表格,其中包含置信区间的实际数字。
  4. 检查/显示是否的最佳方法是什么

a) 随时间的变化具有统计学意义 b) trt 1 和 2 之间是否存在统计学上的显着差异?

任何帮助/指导将不胜感激。

谢谢!

这是一个类似于我正在处理的模拟数据集以及我用来生成堆叠图的一些代码。

ID = c(001, 001, 001, 001, 001, 002, 002, 002, 003, 003, 004, 004, 004, 004, 005, 005, 006, 007, 007, 007, 008, 008, 008, 008, 008, 009, 009, 009, 009, 009)
Visit = c(00, 01, 02, 03, 04, 00, 01, 02,  01, 02, 00, 01, 02, 03, 00, 02, 00, 01, 02, 04, 00, 01, 02, 03, 04, 00, 01, 02, 03, 04)
CVD = c(0, 0, 1, 1, 1, 1, 1, 0, 1, 1,  1, 0, 0, 1, 1,  0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0)
TRT= c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
BIO=c(12.00, 11.9, 15.24, 13.10, 30.01, 45.90, 20.09, 23.45, 14.78, 18.05,
      24.23, 12.34, 80.01, 13.98, 12.50, 36.95, 29.00, 39.87, 19.03, 11.48,
      14.14, 28.06, 12.22, 72.08, 15.00, 11.33, 58.00, 17.71, 52.08, 15.25)
df<-data.frame(ID,Visit, CVD, TRT, BIO)
percentData <- df %>% group_by(Visit, TRT) %>% count(CVD) %>%
 mutate(ratio=scales::percent(n/sum(n))) %>% 
  ungroup()
ggplot(df,aes(x=Visit,fill=factor(CVD)))+
  geom_bar(position="fill")+ facet_wrap(~TRT)+
  geom_text(data=percentData, aes(y=n,label=ratio),
            position=position_fill(vjust=0.5))+
  ylab('Percentage')

在一个问题中要问的问题很多。 我解决这个问题的方法是首先生成一个数据框,其中包含绘制制表结果所需的所有数字:

library(tidyverse)

df2 <- df %>% 
  group_by(Visit, TRT) %>% 
  summarize(no_CVD = table(factor(CVD, 0:1))[1],
            CVD = table(factor(CVD, 0:1))[2],
            n = no_CVD + CVD,
            prop = CVD / n,
            lower = suppressWarnings(prop.test(CVD, n)$conf.int[1]),
            upper = suppressWarnings(prop.test(CVD, n)$conf.int[2])) %>%
  mutate(Percent = scales::percent(prop, 0.1),
         CI = paste0("(", scales::percent(lower, 0.1), " - ",
                     scales::percent(upper, 0.1), ")"),
         TRT = paste("Treatment", TRT))

你可以看到这给了我们点估计和置信区间作为原始比例和文本标签,这应该足以满足问题 3:

df2
#> # A tibble: 10 x 10
#> # Groups:   Visit [5]
#>    Visit TRT         no_CVD   CVD     n  prop  lower upper Percent CI           
#>    <dbl> <chr>        <int> <int> <int> <dbl>  <dbl> <dbl> <chr>   <chr>        
#>  1     0 Treatment 1      3     1     4 0.25  0.0132 0.781 25.0%   (1.3% - 78.1~
#>  2     0 Treatment 2      0     3     3 1     0.310  1     100.0%  (31.0% - 100~
#>  3     1 Treatment 1      2     2     4 0.5   0.150  0.850 50.0%   (15.0% - 85.~
#>  4     1 Treatment 2      2     1     3 0.333 0.0177 0.875 33.3%   (1.8% - 87.5~
#>  5     2 Treatment 1      1     3     4 0.75  0.219  0.987 75.0%   (21.9% - 98.~
#>  6     2 Treatment 2      3     1     4 0.25  0.0132 0.781 25.0%   (1.3% - 78.1~
#>  7     3 Treatment 1      0     2     2 1     0.198  1     100.0%  (19.8% - 100~
#>  8     3 Treatment 2      1     1     2 0.5   0.0945 0.905 50.0%   (9.5% - 90.5~
#>  9     4 Treatment 1      1     2     3 0.667 0.125  0.982 66.7%   (12.5% - 98.~
#> 10     4 Treatment 2      1     0     1 0     0      0.945 0.0%    (0.0% - 94.5~

要将 N 放在每次访问下,我们可以简单地将访问编号paste到包含该表中计数的字符串(这处理问题 1)。

问题2有点奇怪。 您的绘图中已经存在 CVD 的样本患病率(这就是蓝条显示的内容)。 将误差线添加到堆叠条形图会看起来很混乱,并且这些条实际上并没有提供任何信息,就像在这种情况下一个简单的点所做的那样。 因此,我更改了绘图以显示带有误差线的点。

ggplot(df2, aes(paste0(Visit, "\n(n = ", n, ")"), prop)) +
  geom_errorbar(aes(ymin = lower, ymax = upper, color = factor(TRT)), 
                size = 1, width = 0.25) +
  geom_point(size = 2) +
  geom_text(aes(label = paste(Percent, CI, sep = "\n"), y = 1.1), size = 3) +
  facet_grid(.~TRT, scales = "free_x") +
  scale_y_continuous(labels = scales::percent, name = "CVD Prevalance") +
  xlab("Visit") +
  theme_light(base_size = 16) +
  theme(legend.position = "none") +
  scale_color_brewer(palette = "Set1")

在此处输入图像描述

你的最后两点是统计问题而不是编程问题,所以这里是题外话。 您可能需要一个二项式广义线性混合模型来处理重复测量,但如果您无法联系统计学家,您应该将该问题发布在我们的姊妹网站CrossValidated上,以获取统计问题。

reprex 包于 2022-06-21 创建 (v2.0.1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM