使用 ggplot2 为前 2 个类别中的每个子类别绘制分组条形图

Plot grouped bar plot for each subcategory within top 2 categories using ggplot2

我有一个与下面给出的示例数据集非常相似的数据集:

| Category | SubCategory | id | State | Population |
|----------|-------------|----|-------|------------|
| A        | chair       | 1  | CA    | 100        |
| A        | desk        | 2  | CA    | 150        |
| A        | chair       | 3  | NY    | 80         |
| E        | fan         | 4  | AZ    | 60         |
| E        | ac          | 5  | PA    | 55         |
| B        | chair       | 6  | PA    | 78         |
| E        | fan         | 7  | PA    | 90         |
| B        | table       | 8  | PA    | 120        |
| A        | bed         | 9  | UT    | 105        |
| A        | chair       | 10 | NY    | 156        |
| A        | desk        | 11 | NY    | 99         |

下面是数据框:

category= c("A","A","A","E","E","B","E","B","A","A","A")
subcategory = c("chair","desk","chair","fan","ac","chair","fan","table","bed","chair","desk")
id = c(1,2,3,4,5,6,7,8,9,10,11)
population = c(100,150,80,60,55,78,90,120,105,156,99)
df= data.frame(category, subcategory, id, population, stringsAsFactors = T)

如果我们查看 table,每个类别 A、B 和 E 的列 'id' 的总数为 6、2、3(基本上是每个类别的出现次数)。现在,我一直想使用 ggplot2 为属于前 2 个类别的每个子类别绘制一个分组条形图(前 2 个由 'id' 列的计数决定)。因此,我的输出应该只显示类别 A 和 E,列 id 的计数应该根据属于每个子类别的 y 轴绘制(如下所示):

我的代码块根据每个类别的 id 计数为我提供前 2 个子类别,而我希望根据前 2 个类别的 id 计数绘制所有子类别。


df %>% add_count(category, name = 'rank') %>%
  filter(dense_rank(desc(rank)) %in% 1:2) %>%
  count(category, subcategory) %>%
  ggplot(aes(x = category, y = n, fill = subcategory)) +
  geom_col(position = 'dodge') +
  geom_text(aes(label = n), position = position_dodge(1), vjust = 1)