簡體   English   中英

如何在ggplot中顯示不同條件下二元結果的變化比例?

[英]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/ SteroidsNeeded Surgery患者的數據。

謝謝!

為了獲得您想要的 plot,請使用x = Symptom_Dur而不是x = Outcome並刪除facet_wrap部分。 然后只需編輯實驗室並添加標題。

SampleData %>% 
  ggplot() +
  geom_bar(mapping = aes(x = Symptom_Dur , y = stat(prop), group = 1)) +
  ggtitle( "Percentage of patients needing surgery based on symptom duration")

在此處輸入圖像描述

這是一種使另一組透明的方法。

ggplot(SampleData) +
  geom_bar(aes(x = Symptom_Dur, fill = Outcome), position = "fill") +
  scale_fill_manual(values = c("transparent", "black"),
                    guide = "none")

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM