Mike
Mike

Reputation: 1151

How to vjust a label with a grouped bar chart of counts

I've got a grouped bar chart of count data, and would like to position the labels at the mid-point of each bar. However, I can't work out how to add a vjust statement into the position of the label - it keeps erroring with geom_text and stat_count. Any help would be amazing!

data <- data.frame(month = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
                   3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
                   4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 
                   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
                   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
                   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
                   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
                   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
                   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
                   6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
                   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
                   7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
                   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 
                   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 
                   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 
                   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 
                   10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
                   10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
                   10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 
                   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
                   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11),
                   year = c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2019, 2019, 2019, 2019, 2019, 2019, 
                            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                            2020, 2020, 2020, 2020))

ggplot(data, aes(x=factor(month), fill=factor(year))) +
  geom_bar(stat="count", position = position_dodge2(preserve = "single"), colour="black") +
  stat_count(geom = "text", colour = "black", size = 4,
             aes(label = ..count..), position=position_dodge(width=0.9))

enter image description here

Upvotes: 0

Views: 232

Answers (1)

stefan
stefan

Reputation: 125228

This could be achieved by mapping ..count.. / 2 on y:

library(ggplot2)

ggplot(data, aes(x=factor(month), fill=factor(year))) +
  geom_bar(stat="count", position = position_dodge2(preserve = "single"), colour="black") +
  stat_count(geom = "text", colour = "black", size = 4,
             aes(label = ..count.., y = ..count.. / 2), position=position_dodge(width=0.9))

Upvotes: 1

Related Questions