Compose Plots with {patchwork}

R
graphics
Author

Ted Laderas

Published

September 13, 2024

Learn more about composing plots together with a package with straightforward syntax.

Learning Objectives

After working with this Data Snack you should be able to:

  • Explain how the {patchwork} package lets you compose plots together
  • Utilize {patchwork} to compose multiple plots in various layouts

What is {patchwork}?

The {patchwork} package solves a problem that many of us have:

how do we compose multiple plots together into a single figure?

Penguins Data

Just a quick reminder of the penguins data:

Let’s start with two plots

Let’s make two different views of the palmerpenguins data. The first is a bar plot of the penguin species:

Let’s do a histogram of penguin bill_length_mm, colored by species:

Composing Plots together

Now that we have our two plots, let’s combine them into a single plot.

The {patchwork} package has two basic operations. + composes the plots side by side, and / composes one plot on top of each other.

Let’s try out a side by side composition:

Let’s try stacking the plots on top of each other:

We can remove the legends from both:

Side by side and Stacked

How about three figures? We can compose them with a combination of + and /:

There is an equivalent syntax of using | (the pipe character), which does the same thing as +:

Plot Labeling

You can automatically label plots in your figure using plot_annotation():

Finally, let’s add a title for our figure:

Try it out!

Try out a different combination of plots, such as one plot on top and another on the bottom. Or make your own penguins plot and compose them.

Go Further

This is just the tip of the iceberg. You can learn way more about {patchwork} at Thomas Lin Pedersen’s website: https://patchwork.data-imaginist.com/index.html

Citation

BibTeX citation:
@online{laderas2024,
  author = {Laderas, Ted},
  title = {Compose {Plots} with \{Patchwork\}},
  date = {2024-09-13},
  langid = {en}
}
For attribution, please cite this work as:
Laderas, Ted. 2024. “Compose Plots with {Patchwork}.” September 13, 2024.