Home > Software engineering >  How to label geom_bar with pecentage (position fill)?
How to label geom_bar with pecentage (position fill)?

Time:12-10

I am trying to add % to the bars but having some challenges. Can anyone help?

ggplot(age_gender, aes(x = Q1, fill = Q2))   geom_bar(position = "fill")   
  theme_classic()   labs(title = "Proportion of gender by age group", x = "Age group",
                         y = "Proportion/Percentage")   scale_fill_discrete(name= "Gender")

enter image description here

CodePudding user response:

To get frequency within a group:

age_gender1 <-age_gender %>% 
      count(Q1, Q2) %>%
      mutate(Freq = n/sum(n))

To create the plot:

ggplot(age_gender1, aes(x=Q1, y=Freq, fill=Q2))   
  geom_bar(stat="identity")   
  geom_text(aes(label=scales::percent(Freq)), position = position_stack(vjust = .5)) 
  theme_classic()   
  labs(title = "Proportion of gender by age group", x = "Age group", y = "%", fill="Gender")   
  scale_fill_discrete(name= "Gender")

Sample data:

age_gender<-structure(list(Q1 = c("18-21", "18-21", "18-21", "18-21", "18-21", 
"18-21", "18-21", "18-21", "18-21", "18-21", "18-21", "18-21", 
"22-24", "22-24", "22-24", "22-24", "22-24", "22-24", "22-24", 
"22-24", "22-24", "22-24", "22-24", "25-29", "25-29", "25-29", 
"25-29", "25-29", "30-34", "30-34", "30-34", "30-34", "30-34", 
"35-39", "35-39", "35-39", "35-39", "35-39", "35-39", "40-44", 
"40-44", "40-44", "40-44", "40-44", "40-44", "40-44", "40-44", 
"40-44", "40-44", "40-44", "40-44", "40-44", "45-49", "45-49", 
"45-49", "45-49", "45-49", "45-49", "45-49", "50-54", "50-54", 
"50-54", "50-54", "50-54", "50-54", "50-54", "50-54", "55-59", 
"55-59", "55-59", "55-59", "55-59", "55-59", "55-59", "60-69", 
"60-69", "60-69", "60-69", "60-69", "60-69", "60-69", "70 ", 
"70 ", "70 ", "70 ", "70 ", "70 ", "70 ", "70 ", "70 ", "70 "
), Q2 = c("M", "M", "M", "M", "M", "F", "F", "F", "F", "F", "F", 
"F", "M", "M", "M", "M", "F", "F", "F", "F", "F", "F", "F", "M", 
"M", "M", "M", "M", "M", "M", "F", "F", "F", "M", "M", "M", "F", 
"F", "F", "M", "M", "M", "M", "M", "F", "F", "F", "F", "F", "M", 
"M", "M", "F", "F", "F", "M", "M", "M", "M", "M", "M", "M", "M", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "M", "M", 
"M", "F", "F", "F", "F", "M", "M", "M", "M", "M", "M", "M", "F", 
"F", "F")), class = "data.frame", row.names = c(NA, -91L))

enter image description here

  • Related