首页 > 解决方案 > R:使用 dplyr 和 geom_text 计算和显示百分比

问题描述



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")
              

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

在此处输入图像描述

在这个例子中,百分比很容易看出,因为两种语言都有相同数量的案例 (10),但这不一定是真实数据的情况。谢谢你的帮助!

标签: rggplot2dplyrgeom-text

解决方案


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

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")

在此处输入图像描述


推荐阅读