11.7 Bar Chart Dodge Labelled Colour Brewer

20210413

ds %>%
  ggplot(aes(x=wind_dir_3pm, fill=rain_tomorrow)) +
  geom_bar(position="dodge") +
  geom_text(stat="count", aes(label=comma(..count..)),
             position=position_dodge(width=0.9), vjust=-0.2, size=2) +
  scale_y_continuous(labels=comma, limits=c(0, 18500)) +  
  labs(x=vnames["wind_dir_3pm"], y="Count", fill="Rains Tomorrow?") +  
  theme(legend.position="top") +
  scale_fill_brewer(palette="Set2")

A dodged bar chart is produced when a fill= option is provided for the ggplot2::aes() and position="dodge" option for the ggplot2::geom_bar(). We can add numeric labels using ggplot2::geom_label() (as in Section 11.14) or using ggplot2::geom_text() (as in Section 11.19). Here we demonstrate with ggplot2::geom_text().

Notice the use of scales::comma() for the labels as should be standard practice.

To tune the plot we’ve moved the legend to the top with the legend.position="top" option of ggplot2::theme(), increased the y-axis to fit the label using the limits= option of the ggplot2::scale_continuous() family of functions.



Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.