将百分比标签添加到条形图上方的堆积条形图

Add percentage labels to a stacked bar chart above bars

这确实与之前问过的一些问题类似,但更具体。这是我使用 ggplot 制作的堆叠条形图。它显示了在 16 个不同实验室中测试的 10 种不同抗体的阳性和阴性样本数量(为保护机密而更改了细节)。我想在每个条形图的顶部显示正百分比(即在条形图区域之外并悬停在绿色部分上方)。但是,对于没有 space 的那些(例如“Lab 11”),它应该位于栏的绿色区域的内部,并且可能带有白色文本,因此它会显示。

这是我使用的代码:

bar <- ggplot(datas, aes(fill=Status, y=Number, x=Antibody)) + 
    geom_bar(position="stack", stat="identity") +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    panel.spacing.x=unit(0.1, "lines") , panel.spacing.y=unit(0.1,"lines"),
    legend.position ="bottom") +
    facet_wrap(~Lab,nrow=4) + scale_fill_brewer(palette = "Set2")

让我知道我是否也应该 post 数据(我真的不知道该怎么做)。

谢谢,

乔什


数据


这是一些假数据的输入:

datas = structure(list(Antibody = c("ab_1", "ab_1", "ab_1", "ab_1", "ab_1", 
"ab_1", "ab_2", "ab_2", "ab_2", "ab_2", "ab_2", "ab_2", "ab_3", 
"ab_3", "ab_3", "ab_3", "ab_3", "ab_3"), Lab = c("lab_1", "lab_1", 
"lab_2", "lab_2", "lab_3", "lab_3", "lab_1", "lab_1", "lab_2", 
"lab_2", "lab_3", "lab_3", "lab_1", "lab_1", "lab_2", "lab_2", 
"lab_3", "lab_3"), number_tests = c(1382, 1382, 1951, 1951, 1034, 
1034, 1382, 1382, 1951, 1951, 1034, 1034, 1382, 1382, 1951, 1951, 
1034, 1034), prop_pos = c(0.587053193943575, 0.587053193943575, 
0.587053193943575, 0.587053193943575, 0.587053193943575, 0.587053193943575, 
0.683785125147551, 0.683785125147551, 0.683785125147551, 0.683785125147551, 
0.683785125147551, 0.683785125147551, 0.279249225975946, 0.279249225975946, 
0.279249225975946, 0.279249225975946, 0.279249225975946, 0.279249225975946
), Status = c("npos", "nneg", "npos", "nneg", "npos", "nneg", 
"npos", "nneg", "npos", "nneg", "npos", "nneg", "npos", "nneg", 
"npos", "nneg", "npos", "nneg"), Number = c(799, 583, 1144, 807, 
606, 428, 945, 437, 1320, 631, 708, 326, 380, 1002, 554, 1397, 
276, 758)), row.names = c(NA, -18L), class = c("tbl_df", "tbl", 
"data.frame"))

尽量不要称我们的数据为“数据”,因为这是 R 中的一个函数!

使用我编辑到你的问题中的数据。

您可以通过添加只查看阳性数据的 geom_text 来做您想做的事。

ggplot(datas, aes(fill=Status, y=Number, x=Antibody)) + 
 geom_bar(position="stack", stat="identity") +
 theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
       panel.spacing.x=unit(0.1, "lines") , panel.spacing.y=unit(0.1,"lines"),
       legend.position ="bottom") +
 facet_wrap(~Lab,nrow=4) + 
 scale_fill_brewer(palette = "Set2") +
 geom_text(data = data %>%
            filter(Status == "npos"), 
           aes(label = round(Number/number_tests, 3)),
           vjust = 0)


数据


library(tidyverse)
datas <- tibble(Lab = rep(paste0("lab_", 1:3), times = 3),
             Antibody = rep(paste0("ab_", 1:3), each = 3)) %>%
 group_by(lab) %>%
 nest() %>% 
 mutate(number_tests = round(runif(1, 1000, 2100))) %>%
 unnest(data) %>%
 group_by(antibody) %>%
 nest() %>% 
 mutate(prop_pos = runif(n = 1)) %>% 
 unnest(data) %>% 
 ungroup() %>% 
 mutate(npos = map2_dbl(number_tests, prop_pos,
                        ~ rbinom(n = 1, size = (.x), prob = .y)),
        nneg = number_tests - npos) %>%
 pivot_longer(cols = c(npos, nneg), names_to = "Status", values_to = "Number")