-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path088-r_marky_markdown-patchwork.Rmd
More file actions
247 lines (150 loc) · 7.11 KB
/
088-r_marky_markdown-patchwork.Rmd
File metadata and controls
247 lines (150 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(flair)
library(here)
```
# Multiple plots with `patchwork`
*Written by Michael Chong and last updated on 7 October 2021.*
## Introduction
In this lesson, you will learn how to:
- use the [`patchwork`](https://patchwork.data-imaginist.com/) R package to make multi-panel plots
Prerequisite skills include:
- familiarity with `ggplot()`
## What is `patchwork`?
In some situations it might be useful to arrange multiple plots side by side. The `patchwork` R package provides a really easy way to do this.
[The package website](https://patchwork.data-imaginist.com/) is a great resource on its own. Most of the content in this lesson can also be found there.
**To get started**, make sure to have the package installed, which you can get using the default install command:
```{r, eval = FALSE}
install.packages("patchwork")
```
and load the package:
```{r}
library(patchwork)
```
## Getting started
First, make some plots that you want to put together. Here's an example using the [`palmerpenguins`](https://allisonhorst.github.io/palmerpenguins/index.html) dataset.
```{r}
library(palmerpenguins)
# Make a scatterplot of flipper length vs. body mass
first_plot <- ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
# Make a boxplot of body mass by sex
second_plot <- ggplot(penguins, aes(x = sex, y = body_mass_g)) +
geom_boxplot()
```
Putting two plots together is as simple as just "adding" them with a `+` sign:
```{r, warning = FALSE}
# Add them together side by side
first_plot + second_plot
```
If we want to stack the plots on top of each other, we use the `/` symbol instead:
```{r, warning = FALSE}
# Stack them on top
first_plot / second_plot
```
It's that simple!
## More complicated layouts
Okay, what if you have more than 2 plots that you want to arrange? Suppose we also want to plot how size varies by island.
```{r, warning = FALSE}
third_plot <- ggplot(penguins, aes(x = island, y = body_mass_g)) +
geom_boxplot()
```
To put them in a row, I'd recommend using `|` (instead of `+`, which doesn't always force them in a row)
```{r, warning = FALSE}
first_plot | second_plot | third_plot
```
or, you can stack as many as you want by adding more `/` symbols:
```{r, warning = FALSE}
first_plot / second_plot / third_plot
```
Or, you can arrange them using a mix of `|` and `/`:
```{r, warning = FALSE}
first_plot | second_plot / third_plot
```
You can use parentheses `( )` to change "the order of operations" to change how things are arranged:
```{r, warning = FALSE}
(first_plot | second_plot) / third_plot
```
With these few tools, you can get good looking multi-panel plots relatively easily!
## Customization and annotation
After you have the plots arranged in the way you want, maybe you want to add a title and labels, which you can do using the `plot_annotation()` function. You can just add this to the `patchwork` expression.
As an example, I'll add a title, subtitle, caption, as well as "tag" the individual subplots with "a", "b", "c".
```{r, warning = FALSE}
(first_plot | second_plot / third_plot) +
plot_annotation(
title = "Exploration of penguin size",
subtitle = "I love penguins!",
caption = "Here goes a descriptive caption explaining the plots.",
tag_levels = "a"
)
```
Note that I have to wrap `first_plot | second_plot / third_plot` in parentheses `( )` -- otherwise the annotations won't be added correctly. You can also assign the patchwork as its own object before adding the annotations:
```{r, warning = FALSE}
my_patchwork <- first_plot | second_plot / third_plot
```
The arguments for `title`, `subtitle`, and `caption` are straightforward -- you just provide a string of text to display.
For `tag_levels`, you specify how you want the subplots to be labelled. You can provide:
* `a` for lowercase letters (shown above),
* `A` for uppercase letters,
* `1` for numbers,
* `i` or `I` for lowercase or uppercase Roman numerals
Remember you can always check how to use the function using `?plot_annotation`.
## Exercises
### Exercise 1
```{r, echo = FALSE, warning = FALSE}
(first_plot / first_plot) | first_plot
```
Using the plot `first_plot` three times, recreate the `patchwork` layout above.
```{r patchwork-exercise-1, exercise = TRUE, warning = FALSE}
```
```{r patchwork-exercise-1-solution, warning = FALSE}
(first_plot / first_plot) | first_plot
```
### Exercise 2
Annotate the plot you made in Exercise 1! Replicate the following:
```{r, echo = FALSE, warning = FALSE}
first_plot_modified <- first_plot + labs(title = "This plot has its own title")
((first_plot / first_plot) | first_plot_modified) +
plot_annotation(
title = "The same plot...three times!",
caption = "What a great caption!"
)
```
```{r patchwork-exercise-2, exercise = TRUE, warning = FALSE}
```
```{r patchwork-exercise-2-hint-1, warning = FALSE, eval = FALSE}
You'll need to modify one of the subplots.
```
```{r patchwork-exercise-2-solution, warning = FALSE}
first_plot_modified <- first_plot + labs(title = "This plot has its own title")
((first_plot / first_plot) | first_plot_modified) +
plot_annotation(
title = "The same plot...three times!",
caption = "What a great caption!"
)
```
## Common Mistakes & Errors
**Annotations aren't showing up** You might need to make sure you're adding the `plot_annotation()` to the patchwork object. Try wrapping your patchwork in parentheses (e.g. `(plot1 | plot2) + plot_annotation(...)`), or saving it to a different object first (`patchwork <- plot1 | plot2; patchwork + plot_annotation(...)`)
**I'm having trouble laying out my plots how I want** Check out some of the resources under Next Steps -- in particular browsing the patchwork website. This lesson just introduces some of the basic functionality, and there's lots more to know!
**`Error: Can't add ____ to a ggplot object`** First, make sure you've loaded the `patchwork` package with `library(patchwork)`. Then, make sure that all the plots you're trying to add together are `ggplot` objects (see code chunk below).
```{r, warning = FALSE}
class(first_plot)
```
**patchwork and `plot()`** Note that plots made using base R's `plot()` function aren't compatible with patchwork (as far as I know)! If you need to put base R plots together in a similar way, you can try using the `cowplot` R package.
## Next Steps
If you need more functionality, the `patchwork` package is capable of much more! You can browse the [main website](https://patchwork.data-imaginist.com/index.html) or check out these vignettes:
* [More advanced layouts](https://patchwork.data-imaginist.com/articles/guides/layout.html)
* [Alignment across multiple pages](https://patchwork.data-imaginist.com/articles/guides/multipage.html)
If you still need more functionality to get those plots perfectly arranged, you can try the [`cowplot` package](https://wilkelab.org/cowplot/index.html). It's more powerful, but the syntax is a bit more difficult.
## Exercises
### Question 1
### Question 2
### Question 3
### Question 4
### Question 5
### Question 6
### Question 7
### Question 8
### Question 9
### Question 10