【问题标题】:R: Calculate and display percentages using dplyr and geom_textR:使用 dplyr 和 geom_text 计算和显示百分比
【发布时间】:2021-08-17 07:13:55
【问题描述】:


df <- data.frame(Language = factor(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), levels = 1:2, labels = c("GER", "ENG")),
                 Agegrp =   factor(c(1, 2, 3, 1, 2, 4, 1, 2, 3, 2, 3, 3, 3, 3, 1, 1, 2, 1, 1, 4), levels = c( 1, 2, 3, 4), labels = c("10-19", "20-29", "30-39", "40+")) 
                 ) 
  

df %>% ggplot(aes(x = Agegrp, fill = Language)) + 
  geom_bar(position = 'dodge') +
  labs(title = "Age-structure between German and English",
       y = "Number of persons")
              

使用上面的示例数据,我可以创建以下图。但是

  • 如何计算每个年龄组每种语言中的百分比(使用 dplyr)和
  • 如何使用百分比绘制相同的图(y 轴应该是百分比)?

在此示例中,百分比很容易看出,因为两种语言的案例数量相同 (10),但实际数据不一定如此。谢谢你的帮助!

【问题讨论】:

    标签: r ggplot2 dplyr geom-text


    【解决方案1】:

    如果您想在条形图上添加百分比,您可以使用此代码。计算百分比的逻辑和Ronak一样(学分给Ronak)

    df %>% 
      count(Language, Agegrp) %>% 
      group_by(Language) %>% 
      mutate(percent = prop.table(n)) %>% 
      ggplot(aes(x = Agegrp, y = percent, fill = Language, label = scales::percent(percent))) + 
      geom_col(position = 'dodge') +
      geom_text(position = position_dodge(width = .9),    # move to center of bars
                vjust = -0.5,    # nudge above top of bar
                size = 3) + 
      scale_y_continuous(labels = scales::percent) +
      labs(title = "Age-structure between German and English",
           y = "Number of persons")
    

    【讨论】:

      【解决方案2】:

      要计算Language 中每个Agegrp 的百分比,您可以尝试 -

      library(dplyr)
      library(ggplot2)
      
      df %>%
        count(Agegrp, Language) %>%
        group_by(Language) %>%
        mutate(n = prop.table(n)) %>%
        ungroup %>%
        ggplot(aes(x = Agegrp, y = n, fill = Language)) + 
        geom_col(position = 'dodge') +
        scale_y_continuous(labels = scales::percent) + 
        labs(title = "Age-structure between German and English",
             y = "Percentage of persons")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多