11.16 Bar Chart Ordered X Axis


ds %>%
  count(wind_dir_9am) %>%
  ggplot(aes(x=reorder(wind_dir_9am, n), y=n)) +
  geom_col() +
  scale_y_continuous(labels=comma) +  
  labs(x=vnames["wind_dir_9am"], y="Count")

In Section 11.18 a bar chart is shown with the x axis ordered by the levels of a factor. Often we want to order the bars increasing (or decreasing) according the bar heights. In this example we simply add stats::reorder() within the aesthetics to order by the count (n in this case).

To order the bars by decreasing heights replace n with -n.

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.