Home > OS >  Show individual bar count label in grouped bar plot
Show individual bar count label in grouped bar plot

Time:12-05

I am trying to plot Male and Female in different Age Groups. I am trying to show the individual Male and Female Count in their respective bars/colours but the graphs shows the total count value in the AgeGroup. How I am going to show/label the individual count of male and female in their respective bars/colours by AgeGroup. Example Data is presented. Thanks

Age sex AgeGroup
22 F 18-25 Years
36 F 36-45 Years
20 M 18-25 Years

Code I used:

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

CodePudding user response:

  1. You need to specify that the labels should be grouped by sex.
  2. You also need to apply position_dodge() to your labels.
  3. After adding the position adjustment, nudge_y will no longer work. You can use vjust instead.
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())

CodePudding user response:

You need to add sex as a grouping variable in the text layer:

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

enter image description here


Reproducible data taken from answer to OP's previous question

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

CodePudding user response:

One more version using geom_col and calculating stats before plotting: Data from @Allan Cameron (many thanks!):

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

enter image description here

  • Related