Home > Enterprise >  Adding count and percent to bars ggplot
Adding count and percent to bars ggplot

Time:09-22

I have added counts to the top of bars, but cannot figure out how to add % in brackets next to the count! My attempts return prop*100 next to the count above the bar. Here is my code that generates count only - what do I need to modify to this to add the %?

df %>% 
  ggplot(aes(x = day_of_week, fill = day_of_week))  
    geom_bar(position = "dodge")  
    geom_text(aes(label = ..count..), stat = "count", vjust = -1.0, colour = "black")  
    ylim(0, 2600)  
    theme_classic()  
    theme(legend.position="none",
           axis.text.x = element_text(angle=45, hjust=1)
          )  
  scale_fill_brewer(palette = 1, direction = - 1)  
    xlab("day_of_week")  
    ylab("Count (n)")

CodePudding user response:

Here is an option without creating a new column:

df %>% 
  ggplot(aes(x = day_of_week, fill = day_of_week))  
  geom_bar(position = "dodge")  
  geom_text(aes(label = paste0(..count..,"(",round(..count..*100/nrow(df)), "%)")), stat = "count", vjust = -1.0, colour = "black")  
  ylim(0, 2600)  
  theme_classic()  
  theme(legend.position="none",
        axis.text.x = element_text(angle=45, hjust=1)
  )  
  scale_fill_brewer(palette = 1, direction = - 1)  
  xlab("day_of_week")  
  ylab("Count (n)")

enter image description here

And here is an option with a new column created before the plot:

df %>% group_by(day_of_week) %>%
  summarise(n =n())%>%
  mutate(pct= round(n/sum(n)*100, digit=0))%>%
  ggplot(aes(x = day_of_week, y = n, fill = day_of_week))  
  geom_bar(stat = "identity")  
  geom_text(aes(label = paste0(n, " (",pct, "%)")), vjust = -1.0, colour = "black")  
  ylim(0, 2600)  
  theme_classic()  
  theme(legend.position="none",
        axis.text.x = element_text(angle=45, hjust=1)
  )  
  scale_fill_brewer(palette = 1, direction = - 1)  
  xlab("day_of_week")  
  ylab("Count (n)")

enter image description here

Data


set.seed(14) 
df = data.frame(day_of_week = factor(sample(c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
                                     size = 3000,
                                     prob = c(1,3,5,2,6,10,1),
                                     replace = TRUE), levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")))

CodePudding user response:

I ended up create a new col with a count of each category of interest and ran the code below!

df2 <- df %>%
  select(day_of_week) %>%
  group_by(day_of_week) %>% 
  mutate(count_day_of_week_group = n()) %>%
  ungroup

df2 <- df2 %>%  
  distinct(count_day_of_week_group, .keep_all = TRUE)

ggplot(df2, aes(x = day_of_week, y = count_day_of_week_group))   
  geom_bar(stat = "identity", color = "black", fill = "dodgerblue1")  
  geom_text(label = with(df2, 
                         sprintf("%d (%.0f%%)", 
                                 count_day_of_week_group, 100*count_day_of_week_group/sum(count_day_of_week_group))), 
            vjust=-1) 
  ylim(0,3000)
  • Related