Eisen
Eisen

Reputation: 1887

Annotate ggplot bar plot in R

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.

Upvotes: 1

Views: 788

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

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

Upvotes: 1

Related Questions