Annotate ggplot bar plot in R

Viewed 42

How can I annotate the following plot? I want to include counts on top of each bar.

g <- ggplot(mpg, aes(class))
# Number of cars in each class:
g + geom_bar()

I only know how to do it if I do a group by and create a new column 'count' for example.

1 Answers

You can do:

ggplot(mpg, aes(class)) + 
  geom_bar() +
  geom_text(stat = "count", aes(label = after_stat(count)), nudge_y = 1)

enter image description here

Related