R语言 在分组条形图中显示单个条形计数标签

a14dhokn  于 2022-12-05  发布在  其他
关注(0)|答案(3)|浏览(192)

我试图绘制不同年龄组的男性和女性。我试图以各自的条形/颜色显示男性和女性个体计数,但图表显示年龄组中的总计数值。**我将如何按年龄组以各自的条形/颜色显示/标记男性和女性个体计数。**提供了示例数据。谢谢
| 年龄|性别|年龄组|
| - -|- -|- -|
| 二十二个|F级|18-25岁|
| 三十六|F级|36-45岁|
| 二十个|米|18-25岁|
我使用的代码:

library(tidyverse)

ggplot(demo_df, mapping = aes(x = AgeGroup)) + 
  geom_bar(aes(fill = sex), position="dodge")+
  geom_text(stat = "count", aes(label = scales::comma(after_stat(count))),
            nudge_y = 10000, fontface = 2) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 0),
        axis.text.y.left = element_blank(),
        axis.title.y.left = element_blank())

kfgdxczn

kfgdxczn1#

另一个使用geom_col并在绘图前计算统计数据的版本:数据来自@艾伦·卡梅隆(非常感谢!):

library(tidyverse)
library(RColorBrewer)   

demo_df %>% 
  as_tibble() %>% 
  count(sex, AgeGroup) %>% 
  ggplot(aes(x=AgeGroup, y=n, fill = sex))+
  geom_col(position = position_dodge())+
  geom_text(aes(label = n, group = sex), 
            position = position_dodge(width = .9),
            vjust = -1, size = 3)+
  scale_fill_brewer(palette = 1, direction = - 1) + 
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 0),
        axis.text.y.left = element_blank(),
        axis.title.y.left = element_blank())

cbeh67ev

cbeh67ev2#

1.您需要指定标签应按sex进行group
1.您还需要对标签应用position_dodge()
1.添加位置调整后,nudge_y将不再起作用,您可以使用vjust代替。

library(ggplot2)

ggplot(demo_df, mapping = aes(x = AgeGroup)) + 
  geom_bar(aes(fill = sex), position="dodge")+
  geom_text(
    stat = "count", 
    aes(label = scales::comma(after_stat(count)), group = sex),
    position = position_dodge(width = 0.9),
    vjust = -1,
    fontface = 2
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 0),
        axis.text.y.left = element_blank(),
        axis.title.y.left = element_blank())

cotxawn7

cotxawn73#

您需要在文本层中添加性别作为分组变量:

library(tidyverse)

ggplot(demo_df, mapping = aes(x = AgeGroup)) + 
  geom_bar(aes(fill = sex), position="dodge")+
  geom_text(stat = "count", 
            aes(label = scales::comma(after_stat(count)), group = sex),
            position = position_dodge(width = 0.9), fontface = 2,
            vjust = -0.5) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 0),
        axis.text.y.left = element_blank(),
        axis.title.y.left = element_blank())

可重现数据取自OP上一个问题的答案

demo_df <- data.frame(sex = rep(c('F', 'M'), c(514729, 470971)), 
                     AgeGroup = rep(rep(c("18-25 years", "26-35 years",
                                          "36-45 years", "46-55 years",
                                          "55-65 years", "66-75 years",
                                          "76-85 years", "86+ years"), 2),
                                    times = c(40608, 80464, 85973, 72863, 72034,
                                              62862, 54588, 45337, 37341, 77383,
                                              83620, 67367, 67190, 64193, 49171,
                                              24706)))

相关问题