Search code examples
rggplot2bar-chart

How do I display suffix in horizontal barchart with values of millions to "M" or thousands to "K" instead of the full numbers?


I would like to add the "M" or "K" suffix in my barchart. The numbers for each labels show the entire raw numbers but I would like to be round up to the nearest thousand and display the suffix "K" or "M" for example. Unfortunately, I tried the solution above but it's not working. I tried this solution:

geom_text(aes(label=label_number(scale=1e-3, suffix='K')))

but it's not working properly in my code. Could please help me with it?

Link to the dataset

db %>%
  ggplot(aes(x = reorder(name, totalEarnings), y = totalEarnings, fill = totalEarnings)) + 
  geom_bar(stat = "identity", show.legend = FALSE) + 
  coord_flip() + 
  theme(plot.background = element_blank(),
     panel.background = element_blank(),
     axis.title = element_blank(),
     axis.text.x = element_blank(),
     axis.text.y = element_text(colour = "black")) +
  geom_text(aes(label = totalEarnings), size = 3, hjust = -0.1, color = "black", fontface = "bold")

enter image description here


Solution

  • Try label_number(scale_cut = cut_short_scale()).