patchwork Package in R | Introduction, Tutorial & Programming Examples (2025)

Recently, I have discovered the patchwork package of Thomas Lin Pedersen, and I was impressed how easy it is to combine multiple ggplot2 plots in a plot composition using this package.

In this tutorial, I want to explain the basics of the patchwork package and some of its most important functions.

Before we dive into the examples…

  • Here you can find the documentation of the patchwork package.
  • Here you can find the CRAN page of the patchwork package.
  • Here you can find the homepage of the patchwork package.

Make sure to check out the previous links for detailed instructions on how to use the patchwork package in R.

In the following, however, I’ll give an introduction to the basic functionality of the package. So keep on reading!

Example Data, Add-On Packages & ggplot2 Plots

First, let’s load some example data to R:

data(iris) # Load iris flower data sethead(iris) # Head of iris flower data set# Sepal.Length Sepal.Width Petal.Length Petal.Width Species# 1 5.1 3.5 1.4 0.2 setosa# 2 4.9 3.0 1.4 0.2 setosa# 3 4.7 3.2 1.3 0.2 setosa# 4 4.6 3.1 1.5 0.2 setosa# 5 5.0 3.6 1.4 0.2 setosa# 6 5.4 3.9 1.7 0.4 setosa

In this tutorial, we’ll use the iris flower data set as a basis.

The previous output of the RStudio console shows the structure of our example data. It consists of four numeric columns and a factor variable containing three different flower species.

If we want to use the functions of the patchwork package, we also have to install and load patchwork:

install.packages("patchwork") # Install & load patchwork packagelibrary("patchwork")

The patchwork package is used to combine plots created by the ggplot2 package. For that reason, we also have to install and load the ggplot2 package:

install.packages("ggplot2") # Install & load ggplot2 packagelibrary("ggplot2")

Now, we can plot our data in different types of ggplot2 plots as follows:

ggp1 <- ggplot(iris, # Create ggplot2 scatterplot aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point()ggp1 # Draw ggplot2 scatterplot

patchwork Package in R | Introduction, Tutorial & Programming Examples (1)

ggp2 <- ggplot(iris, # Create ggplot2 barchart aes(x = Species, y = Sepal.Width, fill = Species)) + geom_bar(stat = "identity")ggp2 # Draw ggplot2 barchart

patchwork Package in R | Introduction, Tutorial & Programming Examples (2)

ggp3 <- ggplot(iris, # Create ggplot2 boxplot aes(x = Species, y = Sepal.Width, col = Species)) + geom_boxplot()ggp3 # Draw ggplot2 boxplot

patchwork Package in R | Introduction, Tutorial & Programming Examples (3)

As shown in Figures 1, 2, and 3, we have created a scatterplot, a barplot, and a boxplot of the iris flower data set with the previously shown R code.

Example 1: Draw Composition of ggplot2 Plots Using patchwork Package

In this example, I’ll show how to draw a grid of plots using the patchwork package. Have a look at the following R code and the resulting graphic:

ggp_sbs <- (ggp1 + ggp2) / ggp3 # Create plot compositionggp_sbs # Draw plot composition

patchwork Package in R | Introduction, Tutorial & Programming Examples (4)

As shown in Figure 4, the previous R syntax created a plot composition of multiple ggplot2 graphs. The plot composition consists of several rows and columns.

If you want to learn more about how to arrange multiple ggplot2 plots using the patchwork package, you can have a look at this tutorial or at the following video on my YouTube channel:

Example 2: Print ggplot2 Plots On Top of Each Other Using patchwork Package

The following R programming syntax shows how to use the inset_element function of the patchwork package to draw different plots on top of each other. Consider the following R syntax:

ggp_top <- ggp1 + # Add plots on top of each other inset_element(ggp2, 0.01, 0.01, 0.7, 0.5) + inset_element(ggp3, 0.4, 0.6, 0.99, 0.99)ggp_top # Draw combined plots

patchwork Package in R | Introduction, Tutorial & Programming Examples (5)

As shown in Figure 5, the previous R programming code created a graphic containing three different plots. Our example plot ggp1 was used as background and the two plots ggp2 and ggp3 were added as inset to this plot.

If you want to learn more on how to add an inset to a plot, you may have a look here, or you may check out the following YouTube tutorial:

If you want to know how to add an image on top of a ggplot2 graph using the inset_element function, you might check out this tutorial or the following video:

In summary: You learned in this tutorial how to apply the functions of the patchwork package in R. Please let me know in the comments, if you have any additional questions.

10 Comments. Leave new

  • patchwork Package in R | Introduction, Tutorial & Programming Examples (6)

    Petros

    November 16, 2020 7:48 am

    Very flexible and it increases the presentation capabilities a lot. Thank you for sharing, Joachim..

    Reply
    • patchwork Package in R | Introduction, Tutorial & Programming Examples (7)

      Joachim

      November 16, 2020 7:55 am

      Thanks a lot for the positive feedback Petros! I definitely agree, it’s a great package 🙂

      Reply
  • patchwork Package in R | Introduction, Tutorial & Programming Examples (8)

    Rutuja

    November 17, 2020 4:13 am

    This is definately one of the best blog on R Data visualization, I have also liked this video on YouTube which has helped me alot

    Reply
    • patchwork Package in R | Introduction, Tutorial & Programming Examples (9)

      Joachim

      November 17, 2020 6:20 am

      Thanks a lot Rutuja, it’s great to hear that you like my content! 🙂

      Reply
  • patchwork Package in R | Introduction, Tutorial & Programming Examples (10)

    Cyrus

    June 8, 2021 8:24 am

    Hello Joachim,
    this is very nice
    You can also use alternatively the cowplot package
    cowplot::plot_grid()

    Reply
    • patchwork Package in R | Introduction, Tutorial & Programming Examples (11)

      Joachim

      June 8, 2021 9:02 am

      Hey Cyrus,

      Thanks a lot for the kind words!

      Indeed, the cowplot package provides nice solutions for this as well. Another package to consider is the gridExtra package.

      Regards

      Joachim

      Reply
  • patchwork Package in R | Introduction, Tutorial & Programming Examples (12)

    Mhamdi

    October 2, 2021 10:40 am

    Very import things, thank you

    Reply
    • patchwork Package in R | Introduction, Tutorial & Programming Examples (13)

      Joachim

      October 3, 2021 12:59 pm

      Thanks a lot Mhamdi, glad you like my content! 🙂

      Reply
  • patchwork Package in R | Introduction, Tutorial & Programming Examples (14)

    Christian

    January 29, 2022 6:03 am

    Really helpful. Great job, as always!!

    Reply
    • patchwork Package in R | Introduction, Tutorial & Programming Examples (15)

      Joachim

      February 7, 2022 11:52 am

      Thank you very much for the wonderful feedback Christian! 🙂

      Reply

Leave a Reply

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Related Tutorials

Control Size of ggplot2 Legend Items in R (Example) | How to Adjust Symbols

Draw Stacked Barplot in R (3 Examples)

patchwork Package in R | Introduction, Tutorial & Programming Examples (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 5794

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.