【问题标题】:Stacked percentage barplot using facet wrap to group使用 facet wrap 分组的堆叠百分比条形图
【发布时间】:2022-01-24 07:38:54
【问题描述】:

我已经绘制了我的数据图,但希望条形按百分比堆叠(数据总和已达到 100%)。

TestData <- tibble(Ethnicity = c(rep("Black",2), rep("Asian",2), rep("White",2)),
                   Stroke = c(0,1,0,1,0,1),
                   Percent = c(0.33, 0.67, 0.50, 0.50, 0.20, 0.80))

所以,我只想要一个用于变量 Stroke 的条形

        ggplot(TestData, aes(x=as.factor(Stroke),
                            y=Percent,
                            label = scales::percent(Percent),
                            fill = as.factor(Stroke) ) ) +    
            geom_bar(stat="identity", position="stack") + 
            facet_wrap(~ Ethnicity, ncol = 3, strip.position = "bottom")+
            scale_y_continuous(labels = scales::percent)+
            scale_fill_manual(values = c("#1380A1", "#FAAB18")) +
            geom_text(position = position_stack(vjust=0.5), colour="white", size = 3) + 
            theme_bw() +
            theme(legend.position="none") +         
            theme(axis.text.x = element_text(size=8)) +
            xlab("") +                              
            ylab("") +                                  
            ggtitle("Stroke among different ethnicities") +
            theme(plot.title = element_text(hjust = 0.5, size=10, face="bold")) +   
            coord_flip()

我也尝试过将数据更改为长格式,但我仍然无法让绘图看起来正确。我哪里错了?注意:我已经查看了其他答案,没有一个涉及我需要的方面。

library(reshape2)
TestDataMelted <- melt(TestData, id=c("Stroke", "Ethnicity"))

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    问题是您将Stroke 映射到x。因为您只想要一个堆叠条形图在x 上映射一个常量,例如factor(1) 并使用主题选项摆脱 y 轴文本和刻度:

    library(ggplot2)
    
    ggplot(TestData, aes(
      x = factor(1),
      y = Percent,
      label = scales::percent(Percent),
      fill = as.factor(Stroke)
    )) +
      geom_col(position = "stack") +
      facet_wrap(~Ethnicity, ncol = 3, strip.position = "bottom") +
      scale_y_continuous(labels = scales::percent) +
      scale_fill_manual(values = c("#1380A1", "#FAAB18")) +
      geom_text(position = position_stack(vjust = 0.5), colour = "white", size = 3) +
      theme_bw() +
      theme(legend.position = "none") +
      theme(axis.text.x = element_text(size = 8), 
            axis.text.y = element_blank(),
            axis.ticks.y = element_blank()) +
      labs(x = NULL, y = NULL, title = "Stroke among different ethnicities") +
      theme(plot.title = element_text(hjust = 0.5, size = 10, face = "bold")) +
      coord_flip()
    

    【讨论】:

    • 非常感谢。这是一个很好/快速的答案,在 x 上映射一个常量的巧妙技巧
    【解决方案2】:

    亲爱的@stefan,这里有一个稍微不同的方法。 这里我们使用x=""

    library(tidyverse)
    
    TestData %>% 
      ggplot(aes(x = "", y= Percent, fill=factor(Stroke), label = scales::percent(Percent))) +
      geom_col(position = position_stack()) +
      facet_wrap(~ Ethnicity, ncol = 3, strip.position = "bottom") +
      scale_fill_manual(values = c("#1380A1", "#FAAB18")) +
      geom_text(position = position_stack(vjust=0.5), colour="white", size = 3) + 
      theme_bw() +
      theme(legend.position="none") +         
      theme(axis.text.x = element_text(size=8)) +
      xlab("") +                              
      ylab("") +                                  
      ggtitle("Stroke among different ethnicities") +
      theme(plot.title = element_text(hjust = 0.5, size=10, face="bold")) +   
      coord_flip()
    

    【讨论】:

    • 我主要或几乎使用 ggplot 但我觉得我应该学习 tidyverse 看看你的解决方案
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多