11.8 Bar Chart Dodge Labelled Colour Brewer


## Warning: Removed 1 rows containing missing values (geom_bar).
## Warning: Removed 1 rows containing missing values (geom_text).

ds %>%
  ggplot(aes(x=wind_dir_3pm, fill=rain_tomorrow)) +
  geom_bar(position="dodge") +
  geom_text(stat="count", aes(label=..count..),
            vjust=-0.2, size=2) +
  scale_y_continuous(labels=comma, limits=c(0, 18500)) +  
  labs(x=vnames["wind_dir_3pm"], y="Count", fill="Rain Tomorrow") +  
  scale_fill_brewer(palette="Set2") +
  ggthemes::theme_solarized_2() +

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.15) or using ggplot2::geom_text() (as in Section 11.20). 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.

We have also chosen ggthemes::theme_solarized_2() for a different style of presentation. Notice that the ggplot2::theme() is placed after the ggthemes::theme_solarized_2() to override the default of the theme which is to place the legend on the right.

Your donation will support ongoing availability 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-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0