繁体   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