placing labels on bar charts

Hi everyone,

For the below bar chart

 hotel_bookings %>%
  ggplot() +
  geom_bar(mapping = aes(x = hotel, fill = market_segment)) +
  facet_wrap(~ market_segment, nrow = 4, scales = 'free')  + 
  theme(
    axis.title = element_text(size = 20),
    axis.text.y = element_text(size = 15, angle = 45),
    axis.text.x = element_text(size = 15, vjust = 0.5),
    strip.text = element_text(size = 15)
  )

Now I wanna add labels that shows the count of each hotel_booking inside or above the bars themselves. How could it be done ?

Thanks :slightly_smiling_face:

Here's an example with a standard set of data we all have:


library(ggplot2);
ggplot(mtcars, aes(x = gear)) +
  geom_bar(aes(fill = cyl)) +
  stat_count(geom = "text", colour = "white", size = 3.5,
             aes(label = ..count..),position=position_stack(vjust=0.5)) +
  facet_wrap(~ cyl)

More approaches can be found online, like here: How to put labels over geom_bar in R with ggplot2 - Stack Overflow

2 Likes

stat_count() then. Thanks a lot Jon :slightly_smiling_face:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.