2

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! How can I put the labels a little higher?
Adjust the nudge_y however you please @Eisen
I used the position argument with position_dodge() instead. I'm having trouble however with reordering. It seems I can't reorder by stat = 'count'? Do you know how I can get around this?
Do you have multiple groups at each x position, or are you just reordering the x axis?
reordering x axis!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.