1

Here's a bar chart:

ggplot(mtcars) +
  geom_bar(aes(x = reorder(factor(cyl), mpg), y = mpg), stat="identity") +
  coord_flip()

Should produce this: enter image description here

I would like to add labels on the end showing the total value of mpg in each bar. For example, 4cyl looks to be around about 290 just from eyeballing. I want to add a label showing the exact number to the bars.

I'd like to experiment and see how they look, so for completeness:

  • Inside at the top of the bars
  • Outside the bars along the top
  • Bonus is I'm able to control whether the labels display vertically or horizontally.

I found this SO post but have struggled to replicate the chosen answer. Here's my attempt:

ggplot(mtcars) +
  geom_bar(aes(x = reorder(factor(cyl), mpg), y = mpg), stat="identity") +
  coord_flip() +
  geom_text(aes(label = mpg))

Which gives an error:

Error: geom_text requires the following missing aesthetics: x, y

How can I add labels to the ends of the bars?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • Bonus: Why is the chart not in descending order, which was my goal with using ``reorder()`` – Doug Fir Jun 01 '17 at 06:46
  • There are plenty of discussions on `coord_flip` screwing up factor orders, e.g. [Order of legend entries in ggplot2 barplots with coord_flip()](https://stackoverflow.com/questions/7299440/order-of-legend-entries-in-ggplot2-barplots-with-coord-flip) – Adam Quek Jun 01 '17 at 06:52
  • Please see this for your other question: [ggplot2 coord_flip() with geom_text](https://stackoverflow.com/questions/42585523/ggplot2-coord-flip-with-geom-text) – Adam Quek Jun 01 '17 at 06:54
  • @DougFir You need to place the aes in the ggplot part, e.g. `ggplot(mtcars, aes(x = reorder(factor(cyl),mpg), y = mpg)) +geom_bar(stat="identity") + coord_flip()+ geom_text(aes(label = as.character(mpg)),stat = "identity")` That is the problem with your code. Now it is the right order and has labels – Buggy Jun 01 '17 at 06:57
  • 1
    Thanks @user3293236 that gets the chart labels to appear but the added labels are at the roots of the bar and ineligible. Does this happen for you too? ``ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + geom_bar(stat="identity") + coord_flip() + geom_text(aes(label = mpg), stat = "identity")`` – Doug Fir Jun 01 '17 at 07:01
  • 1
    Thanks for the tips on factors screwing up ordering, I'll focus on that separatley @AdamQuek – Doug Fir Jun 01 '17 at 07:01
  • `stat="identity"` adds the value of y together. In your code, you are putting 7 to 14 separate mpg values for each bar, with the corresponding mpg value (10.4 to 33.9) as y-axis values. A bit of data manipulation would be required to put the label at the top of the bar. – Adam Quek Jun 01 '17 at 07:06

1 Answers1

5

This would do what you need through generating a new data.frame for label plotting. You can customize the location of texts by adjusting nudge_y and angle.

library(dplyr)
tmp <- mtcars %>% group_by(cyl) %>% summarise(tot_mpg = sum(mpg))
tmp$cyl <- factor(tmp$cyl)
ggplot(mtcars) +
  geom_bar(aes(x = reorder(factor(cyl), mpg), y = mpg), stat="identity") +
  coord_flip() + geom_text(data = tmp, nudge_y = 10, angle = 270,
                           aes(x = cyl, y = tot_mpg, label = tot_mpg))
amatsuo_net
  • 2,409
  • 11
  • 20