patchwork R Package

repository·main·Indexed 25 days ago

https://github.com/thomasp85/patchwork

An R package designed to combine multiple ggplot2 objects into a single graphic. It provides an API using arithmetic operators to create complex, aligned layouts, including rows, columns, and nested arrangements.

Tokens
387
Snippets
2
Records
2
Agent score
33%

What's inside patchwork

  1. Install patchwork

    main

    You can install patchwork from CRAN using the standard install.packages() function. If you prefer the development version, you can install it from GitHub using devtools::install_github().

    # From CRAN
    install.packages('patchwork')
    
    # From GitHub (development version)
    # install.packages("devtools")
    devtools::install_github("thomasp85/patchwork")
  2. Combine ggplots using patchwork

    main

    The core usage of patchwork is to combine separate ggplot2 objects by using arithmetic operators. This allows you to arrange plots in simple rows, columns, or complex nested layouts with full alignment.

    library(ggplot2)
    library(patchwork)
    
    p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
    p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
    
    # Combine plots side-by-side
    p1 + p2
    
    # Create complex nested layouts using | (horizontal) and / (vertical) operators
    p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
    p4 <- ggplot(mtcars) + geom_bar(aes(carb))
    
    (p1 | p2 | p3) /
          p4