[英]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.