Patchwork is a package for the R programming language that simplifies data visualization layouts through a simple math-like syntax.
The Composer of ggplots
With Patchwork the task of combining and organizing ggplots is very easy and can produce ornate visualizations with simple code.
All charts here are made with sample data sets and are just used for example purposes.
Simple Chart Combinations
Vertically Stacked Charts
If i wanted to take a couple charts of mind and compare them next to each other either one above the other it may look something like this:
Looking at this code what we do is create 2 ggplots and assign each to their own variable, top
and bottom
respectively.
Conveniently named we can see where each is going to be placed in the final visual.
The simple Patchwork syntax is finding those 2 variables at the bottom and with a simple division sign saying that top
should be divided by bottom
.
The syntax is super transparent and intuitive with what it will do with the plots and how you might structure more advanced combinations later on. For now, the stacked visualization will look like
Horizontal Stacking
If you wanted to stack your plots horizontally its as simple as adding them together:
With the +
operator you can add charts together flowing from left to right and we show this with the above defined variables conveniently named left
and right
:
It is also useful to know that using the |
pipe operator you can equally divide the sections of the final aggregate visual. So if i ran this code:
My output would actually look like a 50/25/25 split between the charts:
Overflowing and Defining The Grid
So that is a quick way of putting some visualizations together, what are some more elaborate uses of Patchwork and some of its behavior?
If we add a bunch of charts together with the +
operator would it just squeeze the plots together until there's no room left? We now know that the |
will evenly divide available space between the plots, so the more we add the evenly divided space will get more cramped over time. With that known what does adding a lot of plots together achieve?
So Patchwork will try to keep the plots in a square grid shaped combination with the usage of +
so if we have 4 plots we should expect a 2x2 grid square combo plot:
This is helpful so that at least with minimal tampering everything is somewhat visible and less squished than if we used the |
to combine the plots.
You can imagine this grid behavior as plots moving into available slots in an assembly line fashion. The first row (slots 1 and 2) must be filled before flowing down to Row 2 to fill slots 3 and 4.
Lets say you what to redefine the dimensions of your grid so that you have 3 rows, and 2 columns (a 3x2 grid) and you want to fill the grid by columns not rows. You can imagine the assembly line filling in your grid from top to bottom slot 1, 2, then 3, and finally the top slot of column 2 for the 4th plot.
How do we achieve this with Patchwork syntax?
What we're saying is add +
all the plots together and overflow the grid, but with plot_layout
we define with nrow
that we want the grid to have 3 rows, and byrow
is asking the question "Should i fill each row first before moving down to the next?" and we're saying No, Fill each column first then move to the next:
Stacking and Packing
Overflowing the grid is useful but still, you say you want more granular control of the layout? Say no more, we can combine multiple operators to achieve this.
So what is going on here? You might be able to guess what this might look like by now, but just in case lets read it from inside out.
- We're saying take the
top
chart and place thebottom
chart below it in a vertically stacked combo - Then we have a combination of the
left
chart taking up the left half and the combo oftop
andbottom
taking up the right half - And finally we take that 3 part combo chart and put the
right
chart underneath it to finish it off.
You can see how with some simple order of operations (we all remember the PEMDAS acronym from algebra right?) we can easily and logically control the layout of the plots. Lets see the fruits of our labor:
This isn't the prettiest example but you can see by this example the power and flexibility that Patchwork give you with your layouts.
HOWEVER! We left one important thing out, we need to name our Franken-Plot, but with so many pieces involved in this…. Patchwork Plot…. Sorry, I'll stop. With so many pieces how do we appropriately name the whole aggregate visual?
By adding one more thing:
plot_annotation
will let us appropriately title the whole aggregate visualization in the top left:
Ugly visualizations aside you can see the gold of the Patchwork package shine through and the value it could provide you in your work with R.
Quick Summary of The Operators and What They Do
A Quick Recap of the various operators and functions covered:
+
Add plots together, defaults left to right, overflows the grid/
Divide plots, Put Left hand side on top of right hand side|
Evenly separate plots into equal sized portions based on how many|
are used:one | two | three
produces 3 x 33.33% slots(
&)
are used for order of operations "Do This First then the stuff outside of us"
Code?
If you would like all the example code used in the video / this article you can grab it here: