Home > database >  Show Percentage and Count in bar graph in Different AgeGroup Rstudio
Show Percentage and Count in bar graph in Different AgeGroup Rstudio

Time:12-03

I am trying to plot the bar graph to show the total population of Male and Female by their age groups and I used the following code to get the graph.

p1<- ggplot(data=people,aes(x=AgeGroup,fill=sex))
p1<- p1  geom_bar() 
     geom_text(stat="count", aes(label=after_stat(count)), vjust=-1)
p1<-p1 theme_minimal()
p1<-p1 theme(axis.text.x=element_text(angle=90,hjust=0))
p1

In the graph below, the text of count(Males and Females) is merged and not in their respective place/color.

  1. I want to show the total number of males and females in their respective place/colors.
  2. Secondly, I also need help to show the percentage of Male and Females in their respective Age Groups and colors along with total count. In this case, my Y-axis can be empty.

enter image description here

CodePudding user response:

You need to add a group aesthetic to the text layer, and also ensure it is set to position = position_stack() if you want it to match the bars. Adding percentages per group is more complex, requiring some on-the-fly data wrangling:

library(tidyverse)

ggplot(data = people, aes(x = AgeGroup))   
  geom_bar(aes(fill = sex))  
  geom_text(data = . %>% group_by(sex, AgeGroup) %>% 
              summarize(n = n()) %>% group_by(AgeGroup) %>%
              summarize(sex = sex, n = n, perc = n / sum(n)),
              aes(y = n, label = paste(scales::comma(n), 
                                       scales::percent(perc, 0.1), 
                                       sep = '\n'), group = sex), 
            position = position_stack(vjust = 0.5))  
  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()) 

enter image description here


Data used (inferred from image in question

people <- 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)))
  • Related