这确实类似于之前提出的一些问题,但更具体。这是我使用ggplot制作的堆积条形图。它显示了在16个不同的实验室中测试的10种不同抗体的阳性和阴性样品的数量(更改了特定参数以保护机密性)。我想显示每个条形图上方的正百分比(即,在条形图区域之外并悬停在绿色部分上方)。但是,对于那里没有空间的区域(例如“实验室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")
让我知道是否也应该发布数据(我真的不知道该怎么做)。
谢谢
乔什
数据
这里是一些虚假数据的副本:
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"))
答案 0 :(得分:1)
让我们不要将我们的数据称为“数据”,因为这是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")