Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Deploy Preview

on:
push:
branches-ignore:
- master
pull_request:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Hugo
uses: peaceiris/actions-hugo@v3
with:
hugo-version: '0.91.2'
extended: true

- name: Build
run: hugo --minify

- name: Deploy to Netlify
id: netlify
uses: nwtgck/actions-netlify@v3
with:
publish-dir: ./public
production-deploy: false
github-token: ${{ secrets.GITHUB_TOKEN }}
deploy-message: "Deploy preview for ${{ github.ref_name }}"
enable-pull-request-comment: true
enable-commit-comment: true
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defaultContentLanguage: en
title: Environmental Computing
theme: hugo-theme-learn
metaDataFormat: yaml
defaultContentLanguageInSubdir: yes
defaultContentLanguageInSubdir: true
googleAnalyticsID: G-KVTJFRWP8M
googleAnalytics: UA-77528433-1
params:
Expand Down
9 changes: 5 additions & 4 deletions content/Graphics/ggplot/_index.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ The package is named after a book called [The Grammar of Graphics](https://books

Start with the [basics](/graphics/ggplot/ggplot-basics) to learn the basic syntax of making a graph

Then, visit our other pages to further customise the aesthetics of the graph, including colour and formatting:
* [altering the overall appearance](/graphics/ggplot/ggplot-appearance/)
* [adding titles and axis names](/graphics/ggplot/ggplot-labels/)
* [colours and symbols](/graphics/ggplot/ggplot-colour-shapes/).
Then, visit our other pages to further customise the aesthetics of the graph, including colour and formatting:
* [altering the overall appearance](/graphics/ggplot/ggplot-appearance/)
* [adding titles and axis names](/graphics/ggplot/ggplot-labels/)
* [colours and symbols](/graphics/ggplot/ggplot-colour-shapes/)
* [multipanel figures](/graphics/ggplot/ggplot-multipanel/).

Help on all the ggplot functions can be found at the [The master ggplot help site](https://ggplot2.tidyverse.org).

Expand Down
154 changes: 154 additions & 0 deletions content/Graphics/ggplot/ggplot-multipanel/_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
title: 'Multipanel figures with ggplot2'
weight: 6
output: html_document
aliases: /plotting-with-ggplot-multipanel-figures/
---



<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-1-1.png" width="672" /></p>
<p>Multipanel figures — where several plots are arranged together into a single figure — are a common way to present related results side by side. In R with ggplot2, there are two main approaches:</p>
<ol style="list-style-type: decimal">
<li><strong>Faceting</strong>, which splits a single dataset into panels based on the values of one or more variables.</li>
<li><strong>Combining separate plots</strong> using the <a href="https://patchwork.data-imaginist.com">patchwork</a> package, which lets you arrange completely independent ggplots together.</li>
</ol>
<p>Before you get started, read the page on the <a href="/graphics/ggplot/ggplot-basics/">basics</a> of plotting with ggplot and install the package ggplot2.</p>
<pre class="r"><code>library(ggplot2)</code></pre>
<p>For the examples on this page we will use the <code>iris</code> dataset (already built into R) and the <code>mpg</code> dataset from ggplot2.</p>
<pre class="r"><code>data(iris)
data(mpg)</code></pre>
<div id="faceting-with-facet_wrap" class="section level3">
<h3>Faceting with <code>facet_wrap</code></h3>
<p>Faceting is ideal when you want to make the same type of plot for subsets of your data. <code>facet_wrap</code> takes a single grouping variable and wraps the panels into rows and columns automatically.</p>
<p>The formula syntax <code>~ variable</code> specifies which variable to split on. For example, to make a separate scatter plot for each iris species:</p>
<pre class="r"><code>p &lt;- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point() +
theme_classic()

p + facet_wrap(~ Species)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-4-1.png" width="864" /></p>
<p>By default <code>facet_wrap</code> tries to make the panel layout as square as possible. You can control the number of rows or columns with <code>nrow</code> or <code>ncol</code>:</p>
<pre class="r"><code>p + facet_wrap(~ Species, ncol = 1)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-5-1.png" width="384" /></p>
<p>You can add <code>scales = "free"</code> if you want each panel to use its own axis limits, rather than sharing the same scale across all panels. This is useful when the ranges differ greatly between groups, but makes direct visual comparison harder.</p>
<pre class="r"><code>p + facet_wrap(~ Species, scales = &quot;free&quot;)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-6-1.png" width="864" /></p>
</div>
<div id="faceting-with-facet_grid" class="section level3">
<h3>Faceting with <code>facet_grid</code></h3>
<p><code>facet_grid</code> lays panels out in a strict grid defined by two variables — one for rows and one for columns. Use the formula <code>rows ~ cols</code>. To facet only on one dimension, replace the other with a dot (<code>.</code>).</p>
<p>Here we use the <code>mpg</code> dataset, splitting by drive type (<code>drv</code>) in rows and number of cylinders (<code>cyl</code>) in columns:</p>
<pre class="r"><code>ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_classic() +
facet_grid(drv ~ cyl)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-7-1.png" width="864" /></p>
<p><code>facet_grid</code> is useful when you have two categorical variables and want to see every combination. Empty panels appear where no data exist for a given combination.</p>
<p>To facet on just one variable across rows with <code>facet_grid</code>, use <code>. ~ variable</code> (or <code>variable ~ .</code> for columns):</p>
<pre class="r"><code>ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_classic() +
facet_grid(. ~ drv)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-8-1.png" width="864" /></p>
</div>
<div id="customising-facet-labels-and-strips" class="section level3">
<h3>Customising facet labels and strips</h3>
<p>By default the facet labels are the values of the grouping variable. You can change the strip background and text colour inside a <code>theme()</code> call:</p>
<pre class="r"><code>p + facet_wrap(~ Species) +
theme(
strip.background = element_rect(fill = &quot;steelblue&quot;),
strip.text = element_text(colour = &quot;white&quot;, face = &quot;bold&quot;)
)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-9-1.png" width="864" /></p>
<p>To rename the labels without changing the underlying data, supply a named character vector to the <code>labeller</code> argument:</p>
<pre class="r"><code>species_labels &lt;- c(
setosa = &quot;I. setosa&quot;,
versicolor = &quot;I. versicolor&quot;,
virginica = &quot;I. virginica&quot;
)

p + facet_wrap(~ Species, labeller = labeller(Species = species_labels)) +
theme(strip.text = element_text(face = &quot;italic&quot;))</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-10-1.png" width="864" /></p>
</div>
<div id="combining-separate-plots-with-patchwork" class="section level3">
<h3>Combining separate plots with patchwork</h3>
<p>Faceting requires all panels to share the same underlying <code>ggplot</code> call and the same geom type. When you want to combine <em>different</em> plots — perhaps a scatter plot next to a bar plot — use the <a href="https://patchwork.data-imaginist.com">patchwork</a> package.</p>
<p>Install it once with <code>install.packages("patchwork")</code>, then load it:</p>
<pre class="r"><code>library(patchwork)</code></pre>
<p>First, create two separate ggplot objects:</p>
<pre class="r"><code>p1 &lt;- ggplot(iris, aes(Sepal.Length, Petal.Length, colour = Species)) +
geom_point() +
theme_classic()

p2 &lt;- ggplot(iris, aes(Species, Sepal.Length, fill = Species)) +
geom_boxplot(show.legend = FALSE) +
theme_classic()</code></pre>
<p>The <code>|</code> operator places plots side by side, and <code>/</code> stacks them vertically:</p>
<pre class="r"><code>p1 | p2</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-13-1.png" width="864" /></p>
<pre class="r"><code>p1 / p2</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-14-1.png" width="480" /></p>
<p>You can mix <code>|</code> and <code>/</code> and use brackets to control grouping. For example, to put two narrow plots on the right and one tall plot on the left:</p>
<pre class="r"><code>p3 &lt;- ggplot(iris, aes(Petal.Width, Petal.Length, colour = Species)) +
geom_point() +
theme_classic()

p1 | (p2 / p3)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-15-1.png" width="960" /></p>
</div>
<div id="adding-panel-labels-with-patchwork" class="section level3">
<h3>Adding panel labels with patchwork</h3>
<p>Publication figures often label each panel with a letter (A, B, C…). patchwork makes this straightforward with <code>plot_annotation</code>:</p>
<pre class="r"><code>(p1 | (p2 / p3)) +
plot_annotation(tag_levels = &quot;A&quot;)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-16-1.png" width="960" /></p>
<p>You can also add a shared title across all panels:</p>
<pre class="r"><code>(p1 | p2) +
plot_annotation(
title = &quot;Iris sepal and petal measurements&quot;,
theme = theme(plot.title = element_text(size = 14, face = &quot;bold&quot;))
)</code></pre>
<pre><code>## Warning: annotation$theme is not a valid theme.
## Please use `theme()` to construct themes.</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-17-1.png" width="960" /></p>
</div>
<div id="collecting-shared-legends" class="section level3">
<h3>Collecting shared legends</h3>
<p>When panels share the same colour or fill aesthetic, patchwork can merge their legends into one with <code>plot_layout(guides = "collect")</code>:</p>
<pre class="r"><code>(p1 | p3) +
plot_layout(guides = &quot;collect&quot;)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-18-1.png" width="960" /></p>
<p>Combine with <code>plot_annotation</code> to get a clean, publication-ready layout in a few lines of code:</p>
<pre class="r"><code>(p1 | p3) +
plot_layout(guides = &quot;collect&quot;) +
plot_annotation(
tag_levels = &quot;A&quot;,
title = &quot;Iris petal and sepal relationships&quot;
)</code></pre>
<p><img src="/Graphics/ggplot/ggplot-multipanel/_index_files/figure-html/unnamed-chunk-19-1.png" width="960" /></p>
</div>
<div id="further-help" class="section level3">
<h3>Further help</h3>
<ul>
<li><a href="https://ggplot2-book.org/facet.html">Facets chapter</a> in the ggplot2 book</li>
<li><a href="https://patchwork.data-imaginist.com">patchwork documentation</a> — including more advanced layouts with <code>plot_layout()</code> and <code>inset_element()</code></li>
<li>Our other ggplot pages:
<ul>
<li><a href="/graphics/ggplot/ggplot-basics/">The basics of ggplot2</a></li>
<li><a href="/graphics/ggplot/ggplot-appearance/">Customising your ggplot</a></li>
<li><a href="/graphics/ggplot/ggplot-labels/">Adding titles and axis labels</a></li>
<li><a href="/graphics/ggplot/ggplot-colour-shapes/">Colours and shapes</a></li>
</ul></li>
</ul>
<p style="margin-left: .5in; text-indent: -.5in;">
Pedersen, T L (2024) <em>patchwork: The Composer of Plots.</em> R package. <a href="https://patchwork.data-imaginist.com" class="uri">https://patchwork.data-imaginist.com</a>
</p>
<p style="margin-left: .5in; text-indent: -.5in;">
Wickham, H (2016) <em>ggplot2: Elegant Graphics for Data Analysis.</em> Springer-Verlag New York.
</p>
<p><strong>Author</strong>: Environmental Computing</p>
<p><strong>Year:</strong> 2025</p>
<p><strong>Last updated:</strong> Mar 2026</p>
</div>
Loading
Loading