Git Product home page Git Product logo

patchwork's Introduction

patchwork

Travis-CI Build Status AppVeyor Build Status CRAN_Release_Badge CRAN_Download_Badge

The goal of patchwork is to make it ridiculously simple to combine separate ggplots into the same graphic. As such it tries to solve the same problem as gridExtra::grid.arrange() and cowplot::plot_grid but using an API that incites exploration and iteration.

Installation

You can install patchwork from github with:

# install.packages("devtools")
devtools::install_github("thomasp85/patchwork")

Example

The usage of patchwork is simple: just add plots together!

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))

p1 + p2

you are of course free to also add the plots together as part of the same plotting operation:

ggplot(mtcars) +
  geom_point(aes(mpg, disp)) +
  ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear))

layouts can be specified by adding a plot_layout() call to the assemble. This lets you define the dimensions of the grid and how much space to allocate to the different rows and columns

p1 + p2 + plot_layout(ncol = 1, heights = c(3, 1))

If you need to add a bit of space between your plots you can use plot_spacer() to fill a cell in the grid with nothing

p1 + plot_spacer() + p2

You can make nested plots layout by wrapping part of the plots in parentheses - in these cases the layout is scoped to the different nesting levels

p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
p4 <- ggplot(mtcars) + geom_bar(aes(carb))

p4 + {
  p1 + {
    p2 +
      p3 +
      plot_layout(ncol = 1)
  }
} +
  plot_layout(ncol = 1)

Advanced features

In addition to adding plots and layouts together, patchwork defines some other operators that might be of interest. - will behave like + but put the left and right side in the same nesting level (as opposed to putting the right side into the left sides nesting level). Observe:

p1 + p2 + p3 + plot_layout(ncol = 1)

this is basically the same as without braces (just like standard math arithmetic) - the plots are added sequentially to the same nesting level. Now look:

p1 + p2 - p3 + plot_layout(ncol = 1)

Now p1 + p2 and p3 is on the same level...

A note on semantics. If - is read as subtrack its use makes little sense as we are not removing plots. Think of it as a hyphen instead...

Often you are interested in just putting plots besides or on top of each other. patchwork provides both | and / for horizontal and vertical layouts respectively. They can of course be combined for a very readable layout syntax:

(p1 | p2 | p3) /
      p4

There are two additional operators that are used for a slightly different purpose, namely to reduce code repetition. Consider the case where you want to change the theme for all plots in an assemble. Instead of modifying all plots individually you can use & or * to add elements to all subplots. The two differ in that * will only affect the plots on the current nesting level:

(p1 + (p2 + p3) + p4 + plot_layout(ncol = 1)) * theme_bw()

whereas & will recurse into nested levels:

p1 + (p2 + p3) + p4 + plot_layout(ncol = 1) & theme_bw()

Note that parenthesis is required in the former case due to higher precedence of the * operator. The latter case is the most common so it has deserved the easiest use.

This is all it does for now, but stay tuned as more functionality is added, such as collapsing guides, etc...

patchwork's People

Contributors

thomasp85 avatar kevinushey avatar mhenderson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.