将累积数量添加到使用facet_wrap绘制的geom_bar图中

卢卡·赞加里(Luca Zangari)

新手在这里!经过长时间的搜索,我仍然无法找到令人满意的解决方案。我有一个心力衰竭率的数据集(https://archive.ics.uci.edu/ml/datasets/Heart+failure+clinical+records),我想显示一系列几何图形,其中“存活”和“死亡”按类别(即性别,吸烟等)计数。

我认为我在准备地块方面做得不错,他们对我来说看起来不错。问题是,很难看到具有不同特征的存活患者和垂死患者之间的比率如何。

我有两个,但两个都让我望而却步:

  • 在每个小节的上方都加一个计数,这样比率就很明显
  • 在每个特性上直接显示比率。

这是我写的代码。


    library(ggplot)
    
    heart_faliure_data <- read.csv(file = "heart_failure_clinical_records_dataset.csv", header = FALSE, skip=1)
    
    #Prepare Column Names
    c_names <- c("Age",
                 "Anaemia",
                 "creatinine_phosphokinase",
                 "diabetes",
                 "ejection_fraction",
                 "high_blood_pressure",
                 "platelets",
                 "serum_creatinine",
                 "serum_sodium",
                 "sex",
                 "smoking",
                 "time",
                 "DEATH_EVENT")
    
    
    #Apply column names to the dataframe
    colnames(heart_faliure_data) <- c_names
    
    
    # Some Classes like sex, Anaemia, diabetes, high_blood_pressure smoking and DEATH_EVENT are booleans
    # (see description of Dataset) and should be transformed into factors
    heart_faliure_data$sex <- factor(heart_faliure_data$sex, 
                                     levels=c(0,1), 
                                     labels=c("Female","Male"))
    heart_faliure_data$smoking <- factor(heart_faliure_data$smoking, 
                                         levels=c(0,1), 
                                         labels=c("No","Yes"))
    heart_faliure_data$DEATH_EVENT <- factor(heart_faliure_data$DEATH_EVENT, 
                                             levels=c(0,1), 
                                             labels=c("Survived","Died"))
    heart_faliure_data$high_blood_pressure <- factor(heart_faliure_data$high_blood_pressure, 
                                                     levels=c(0,1), 
                                                     labels=c("No","Yes"))
    heart_faliure_data$Anaemia <- factor(heart_faliure_data$Anaemia, 
                                         levels=c(0,1), 
                                         labels=c("No","Yes"))
    heart_faliure_data$diabetes <- factor(heart_faliure_data$diabetes, 
                                          levels=c(0,1), 
                                          labels=c("No","Yes"))
    # Adjust Age to a int value
    heart_faliure_data$Age <- as.integer(heart_faliure_data$Age)
    
    
    # selecting the categorical variables and study the effect of each variable on death-event
    categorical.heart_failure <- heart_faliure_data  %>%
      select(Anaemia,
             diabetes,
             high_blood_pressure,
             sex,
             smoking,
             DEATH_EVENT) %>%
      gather(key = "key", value = "value", -DEATH_EVENT)
    
    
    #Visualizing this effect with a grouped barplot
    categorical.heart_failure %>% 
      ggplot(aes(value)) +
      geom_bar(aes(x        = value, 
                   fill     = DEATH_EVENT), 
                   alpha    = .2, 
                   position = "dodge", 
                   color    = "black",
                   width    = .7,
                   stat = "count") +
      labs(x = "",
           y = "") +
      theme(axis.text.y  = element_blank(),
            axis.ticks.y = element_blank()) +
      facet_wrap(~ key, 
                 scales = "free", 
                 nrow = 4) +
      scale_fill_manual(values = c("#FFA500", "#0000FF"), 
                        name   = "Death Event", 
                        labels = c("Survived", "Dead"))

这是结果的图像(还不错): 在此处输入图片说明

目标是在条形图的顶部具有一些数值。甚至只是避免迹象...

我很高兴您能给我任何帮助!

戴夫·阿姆斯特朗

那这样的事呢 为了使它起作用,我首先汇总了数据:

tmp <- categorical.heart_failure %>% 
  group_by(DEATH_EVENT, key, value) %>% 
  summarise(n = n())


#Visualizing this effect with a grouped barplot
tmp %>% 
  ggplot(aes(x = value, y=n)) +
  geom_bar(aes(fill     = DEATH_EVENT), 
           alpha    = .2, 
           position = position_dodge(width=1), 
           color    = "black",
           width    = .7,
           stat = "identity") +
  geom_text(aes(x=value, y=n*1.1, label = n, group=DEATH_EVENT), position = position_dodge(width=1), vjust=0) + 
  labs(x = "",
       y = "") +
  theme(axis.text.y  = element_blank(),
        axis.ticks.y = element_blank()) +
  facet_wrap(~ key, 
             scales = "free", 
             nrow = 4) +
  scale_fill_manual(values = c("#FFA500", "#0000FF"), 
                    name   = "Death Event", 
                    labels = c("Survived", "Dead")) + 
  coord_cartesian(ylim=c(0, max(tmp$n)*1.25))

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 ggplot / R 将 geom_bar 添加到该图中?

将标题添加到ggplot2图中,因为标题显示在facet_wrap图中

ggplot geom_bar 按组和 facet_wrap 绘制百分比

将标签添加到facet_wrap箱图

使用facet_wrap将图例添加到ggplot中

如何使用facet_wrap将NSE和PBIAS结果添加到ggplot中?

如何使用facet_wrap`表`所有因子列和ggplot geom_bar?

使用facet_wrap时,geom_bar中的条具有不同的宽度

使用 ggplot2 将颜色列添加到 geom_bar 图

使用R / ggplot2将标签添加到geom_bar()中的各个%

ggplot将刻度添加到facet_wrap中的每个图

将x和y轴添加到所有facet_wrap

将“总体”组添加到facet_wrap(ggplot2)

ggplot-在geom_bar facet_wrap中订购酒吧

重新排列facet_grid并将其添加到ggplot2的geom_bar中

facet_wrap添加geom_hline

将族注释添加到 r 中 geom_bar 旁边的 ggtree

将组均值线添加到geom_bar图并包括在图例中

带有geom_bar和facet_wrap的ggplot未显示分组堆积条形图的图例

如何使用每组行号作为 x 用 `facet_wrap()` 绘制 `geom_point()`?

为什么将 `position = "dodge"` 添加到我的 `geom_bar` 会导致值显示不正确?

使用 facet_wrap 时无法向 geom_text 添加条件

动态将geom_segment添加到图中

如何使用facet_wrap将yaxis设置为等于xaxis geom_point

无法使用facet_wrap获得geom_text标签

使用geom_boxplot,facet_grid时,将注释或文本添加到各个箱形图

当与facet_wrap()一起使用时,为什么geom_rect()添加许多多层?

如何使用 facet_wrap 在图形上添加不同的注释

ggplot在使用`facet_wrap`时添加正态分布