[英]How can I show the changing proportion of binary outcome under different conditions in ggplot?
我正在尝试生成一个图表,显示在几种不同条件下单个结果的百分比如何变化。
我正在粘贴示例数据的输入 output。分配 SampleData 的帧 object:
SampleData <- structure(list(Patient = c(7L, 29L, 10L, 6L, 14L, 11L, 28L, 21L,
5L, 18L, 4L, 1L, 24L, 20L, 22L, 23L, 19L, 3L, 12L, 30L), Symptom_Dur = structure(c(2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("<2.5 Months", ">2.5 Months"), class = "factor"),
Outcome = structure(c(2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Better w/ Steroids",
"Needed Surgery"), class = "factor")), row.names = c(NA,
-20L), class = "data.frame")
具体来说,我正在尝试生成一个 plot,它显示在出现症状少于 2.5 个月时需要手术的患者百分比与在出现症状超过 2.5 个月时需要手术的患者百分比。 (我将来也可能会考虑不同的时间点)。
我尝试使用 x = 结果和按症状持续时间进行分面制作条形图,如下所示,但我无法获得排除“Better w/Steroids”分类的图表:
SampleData %>%
ggplot() +
geom_bar(mapping = aes(x = Outcome, y = stat(prop), group = 1)) +
facet_wrap(~ Symptom_Dur)
我希望得到类似于下面的 plot 的东西:
请注意,上图仅显示Needed Surgery
的患者百分比。 我试图避免一个图表同时包含Better w/ Steroids
和Needed Surgery
患者的数据。
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.