The RMarkdown source to this file can be found here
Using facet_wrap() in ggplot2 is a great way to create multiple panelled plots. Though when I am running these, particularly on datasets with different scales, the axis are not as clean as I like. To provide an illustration of what I am referring to, I will make up some fake data with some different scales in the y values and then plot these in ggplot
Plot as is
When using a single panel or data that is all on the same scale, I frequently use
coord_cartesian. Unfortunately when you have panels with different scales this is no longer as useful because it forces everything on the same scale (which depending on what you are looking at may not be bad).
expand_limits and scale_y_continuous
Another option is to use
expand_limits() to force each panel to start at the origin and then use
scale_y_continuous(expand=c(0,0)) to remove any extra space on around the y-axis limits. The two values in expand are c(multiplier buffer, additive buffer). By including a c(0,0) we are not including any buffer on the axis scale.
As you can see in the above plot, using the combo of
scale_y_continuous(expand=c(0,0) we can force the minimum to start at 0 but it max values are not as clean as I would like. This is where
geom_blank() comes in. The purpose, according to the help files for
geom_blank() are to essentially draw nothing. When I first saw this, I was really baffled. Why would you not want something that drew nothing? I then realized the benefit of this in axis formatting.
The first thing to do, is to create a dataset with the mins and maxes of the y axis for each group level in our dataset. This is then passed to ggplot using
So we now have a plot with much cleaner y-axis ranges and plots that we can now present in public.