如果这是我的数据集。
Surgery Surv_Prob Group
CV 0.5113 Diabetic
Hip 0.6619 Diabetic
Knee 0.6665 Diabetic
QFox 0.7054 Diabetic
CV 0.5113 Non-Diabetic
Hip 0.6629 Non-Diabetic
Knee 0.6744 Non-Diabetic
QFox 0.7073 Non-Diabetic
如何绘制如下所示的堆积条形图。
请注意,这些值本质上是累积的,因此该曲线应显示从CV到Hip的极小增加(delta = 0.6619-0.5113)
顺序应该是CV - > Hip - > Knee - > QFox
可能有一种方法可以直接绘制累积值,但是一种方法是获取实际值并通过使用Surgery
按照您想要的顺序排列factor
数据来绘制堆积条形图。对于factor
级别,我使用rev(unique(Surgery))
是为了方便,因为您希望按照它们在数据集中出现的相反顺序进行排序。对于更复杂的类型,您可能需要手动添加levels
。
library(tidyverse)
df %>%
group_by(Group) %>%
mutate(Surv_Prob1 = c(Surv_Prob[1], diff(Surv_Prob)),
Surgery = factor(Surgery, levels = rev(unique(Surgery)))) %>%
ggplot() + aes(Group, Surv_Prob1, fill = Surgery, label = Surv_Prob) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5))
数据
df <- structure(list(Surgery = structure(c(1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L), .Label = c("CV", "Hip", "Knee", "QFox"), class = "factor"),
Surv_Prob = c(0.5113, 0.6619, 0.6665, 0.7054, 0.5113, 0.6629,
0.6744, 0.7073), Group = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L), .Label = c("Diabetic", "Non-Diabetic"), class =
"factor")), class = "data.frame", row.names = c(NA, -8L))