<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-plot-with-patchwork/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/05/25/how-to-plot-with-patchwork/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>INTRODUCTION</strong></p>
<p>The goal of patchwork is to make it 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.</p>
<p><strong>Installation</strong><br>
You can install patchwork from github with:</p>
<p><code># install.packages(&quot;devtools&quot;)<br>
devtools::install_github(&quot;thomasp85/patchwork&quot;)</code></p>
<p>The usage of patchwork is simple: just add plots together!</p>
<p><code>library(ggplot2)<br>
library(patchwork)
<p>p1 &lt;-ggplot(mtcars) + geom_point(aes(mpg, disp))<br>
p2 &lt;-ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))</p>
<p>p1 + p2</p>
<p>you are of course free to also add the plots together as part of the same plotting operation:</p>
<p>ggplot(mtcars) +<br>
<code>  geom_point(aes(mpg, disp)) +<br>
  ggplot(mtcars) +<br>
  geom_boxplot(aes(gear, disp, group = gear))</code></p>
<p>layouts can be specified by adding a <code>plot_layout()</code> 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</p>
<p><code>p1 + p2 + plot_layout(ncol = 1, heights = c(3, 1))</code></p>
<p>If you need to add a bit of space between your plots you can use <code>plot_spacer()</code> to fill a cell in the grid with nothing</p>
<p><code>p1 + plot_spacer() + p2</code></p>
<p>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</p>
<p><code>p3 &lt;- ggplot(mtcars) + geom_smooth(aes(disp, qsec))<br>
p4 &lt;- ggplot(mtcars) + geom_bar(aes(carb))
<p>p4 + {<br>
p1 + {<br>
p2 +<br>
p3 +<br>
plot_layout(ncol = 1)<br>
}<br>
} +<br>
plot_layout(ncol = 1)</p>
<p><strong>Advanced features</strong><br>
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:</p>
<p><code>p1 + p2 + p3 + plot_layout(ncol = 1)</code></p>
<p>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:</p>
<p><code>p1 + p2 - p3 + plot_layout(ncol = 1)</code></p>
<p>Now p1 + p2 and p3 is on the same level.</p>
<p>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:</p>
<p><code>(p1 | p2 | p3) /<br>
      p4</code></p>
<p>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 <code>&amp;</code> or <code>*</code> to add elements to all subplots. The two differ in that <code>*</code> will only affect the plots on the current nesting level:</p>
<p><code>(p1 + (p2 + p3) + p4 + plot_layout(ncol = 1)) * theme_bw()</code></p>
<p>whereas &amp; will recurse into nested levels:</p>
<p><code>p1 + (p2 + p3) + p4 + plot_layout(ncol = 1) &amp; theme_bw()</code></p>
<p>Note that parenthesis is required in the former case due to higher precedence of the <code>*</code> operator. The latter case is the most common so it has deserved the easiest use.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/11/09/how-to-display-multivariate-relationship-graphs-with-lattice/" title="How to Display Multivariate Relationship Graphs With Lattice" target="_blank">How to Display Multivariate Relationship Graphs With Lattice </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/11/16/lattice-exercises/" title="Lattice Exercises" target="_blank">Lattice Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>

				
		
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-exercises.com/2018/05/25/how-to-plot-with-patchwork/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/oDgqa75gcnA" height="1"></a></div></code></code>