ggplot2

How to reorder arrange bars with in each Facet of ggplot

One of the problems that we usually face with ggplot is that rearranging the bars in ascending or descending order. If that problem is solved using reorder() or fct_reorder(), the next problem is when we have facets and ordering bars within each facet. Recently I came acrosss this function reorder_within() from the package tidytext (Thanks to Julia Silge and Tyler Rinker - who created this solution originally) Example Code: library(tidyr) library(ggplot2) iris_gathered <- gather(iris, metric, value, -Species) ggplot(iris_gathered, aes(reorder(Species, value), value)) + geom_bar(stat = 'identity') + facet_wrap(~ metric) As you can see above, the bars in the last facet isn’t ordered properly.

How to arrange ggplot (barplot) bars in ascending or descending order

One of the reasons you’d see a bar plot made with ggplot2 with no ascending / descending order - ordering / arranged is because, By default, ggplot arranges bars in a bar plot alphabetically. But most of the times, it would make more sense to arrange it based on the y-axis it represents (rather than alphabetically). It could be your month-wise time series or high-medium-low bars - these are some examples where an alphabetically-sorted bar chart wouldn’t make sense in fact would hinder the process of data communication.

How to make your Facet Wrap / Facet Grid ggplots span across multiple pages in PDF

This is going to be a very short post. It’s a problem I faced a few days back and after some DDGing, Github Issues scanning and some SOing - I found the answer. So, I thought to share it - for archival and also my future reference. Problem The problem here is when you use facet_grid() or facet_wrap() of ggplot inside an R chunk of an Rmd (Rmarkdown) file. The rendered PDF wouldn’t show those faceted multiple plots in multiple pages instead just embedded one on top another in the same page.