You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: arranging-plots.Rmd
+56-18Lines changed: 56 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,22 @@ source("common.R")
5
5
columns(1, 1 / 1.61, 1)
6
6
```
7
7
8
-
The grammar presented in ggplot2 is concerned with creating single plots. While the faceting system provides the means to produce several subplots all of these are part of the same main visualization, sharing layers, data, and scales. However, it is often necessary to use multiple disparate plots to tell a story or make an argument. These can of course be created individually and assembled in a layout program, but it is beneficial to do this in code to avoid time consuming and non-reproducible manual labor. A range of packages have risen to the occasion and provide different approaches to arranging separate plots. While this chapter will focus on the patchwork package you may also find some of the same functionalities in the cowplot, gridExtra and ggpubr packages.
8
+
The grammar presented in ggplot2 is concerned with creating single plots.
9
+
While the faceting system provides the means to produce several subplots all of these are part of the same main visualization, sharing layers, data, and scales.
10
+
However, it is often necessary to use multiple disparate plots to tell a story or make an argument.
11
+
These can of course be created individually and assembled in a layout program, but it is beneficial to do this in code to avoid time consuming and non-reproducible manual labor.
12
+
A range of packages have risen to the occasion and provide different approaches to arranging separate plots.
13
+
While this chapter will focus on the patchwork package you may also find some of the same functionalities in the cowplot, gridExtra and ggpubr packages.
9
14
10
-
This chapter will be split into two parts. The first will be concerned with arranging plots side by side with no overlap, while the second will be concerned with arranging plots on top of each other. While these two scenarios are not necessarily in opposition to each other, the former scenario will often benefit from functionality that makes little sense in the latter, e.g. alignment of plotting regions.
15
+
This chapter will be split into two parts.
16
+
The first will be concerned with arranging plots side by side with no overlap, while the second will be concerned with arranging plots on top of each other.
17
+
While these two scenarios are not necessarily in opposition to each other, the former scenario will often benefit from functionality that makes little sense in the latter, e.g. alignment of plotting regions.
11
18
12
19
## Laying out plots side by side
13
20
14
-
Often, one wants to show two or more plots side by side to show different aspects of the same story in a compelling way. This is the scenario that patchwork was build to solve. At its heart, patchwork is a package that extends ggplot2's use of the `+` operator to work between multiple plots, as well as add additional operators for specialized compositions and working with compositions of plots.
21
+
Often, one wants to show two or more plots side by side to show different aspects of the same story in a compelling way.
22
+
This is the scenario that patchwork was build to solve.
23
+
At its heart, patchwork is a package that extends ggplot2's use of the `+` operator to work between multiple plots, as well as add additional operators for specialized compositions and working with compositions of plots.
15
24
16
25
As an example of the most basic use of patchwork, we'll use the following 4 plots of the `mpg` dataset
17
26
@@ -40,23 +49,29 @@ library(patchwork)
40
49
p1 + p2
41
50
```
42
51
43
-
`+` does not specify any specific layout, only that the plots should be displayed together. In the absence of a layout the same algorithm that governs the number of rows and columns in `facet_wrap()` will decide the number of rows and columns. This means that adding 3 plots together will create a 1x3 grid while adding 4 plots together will create a 2x2 grid.
52
+
`+` does not specify any specific layout, only that the plots should be displayed together.
53
+
In the absence of a layout the same algorithm that governs the number of rows and columns in `facet_wrap()` will decide the number of rows and columns.
54
+
This means that adding 3 plots together will create a 1x3 grid while adding 4 plots together will create a 2x2 grid.
44
55
45
56
```{r}
46
57
p1 + p2 + p3 + p4
47
58
```
48
59
49
-
As can be seen from the two examples above, patchwork takes care of aligning the different parts of the plots with each other. You can see that all plotting regions are aligned, even in the presence of faceting. Further, you can see that the y-axis titles in the two left-most plots are aligned despite the axis text in the bottom left plot being wider.
60
+
As can be seen from the two examples above, patchwork takes care of aligning the different parts of the plots with each other.
61
+
You can see that all plotting regions are aligned, even in the presence of faceting.
62
+
Further, you can see that the y-axis titles in the two left-most plots are aligned despite the axis text in the bottom left plot being wider.
50
63
51
64
### Taking control of the layout
52
65
53
-
It is often that the automatically created grid is not what you want and it is of course possible to control it. The most direct and powerful way is to do this is to add a `plot_layout()` specification to the plot:
66
+
It is often that the automatically created grid is not what you want and it is of course possible to control it.
67
+
The most direct and powerful way is to do this is to add a `plot_layout()` specification to the plot:
54
68
55
69
```{r}
56
70
p1 + p2 + p3 + plot_layout(ncol = 2)
57
71
```
58
72
59
-
A common scenario is wanting to force a single row or column. patchwork provides two operators, `|` and `/` respectively, to facilitate this (under the hood they simply set number of rows or columns in the layout to 1).
73
+
A common scenario is wanting to force a single row or column.
74
+
patchwork provides two operators, `|` and `/` respectively, to facilitate this (under the hood they simply set number of rows or columns in the layout to 1).
60
75
61
76
```{r}
62
77
p1 / p2
@@ -85,29 +100,37 @@ CDD
85
100
p1 + p2 + p3 + p4 + plot_layout(design = layout)
86
101
```
87
102
88
-
As has been apparent in the last couple of plots the legend often becomes redundant between plots. While it is possible to remove the legend in all but one plot before assembling them, patchwork provides something easier for the common case:
103
+
As has been apparent in the last couple of plots the legend often becomes redundant between plots.
104
+
While it is possible to remove the legend in all but one plot before assembling them, patchwork provides something easier for the common case:
Electing to collect guides will take all guides and put them together at the position governed by the global theme. Further, it will remove any duplicate guide leaving only unique guides in the plot. The duplication detection looks at the appearance of the guide, and not the underlying scale it comes from. Thus, it will only remove guides that are exactly alike. If you want to optimize space use by putting guides in an empty area of the layout, you can specify a plotting area for collected guides:
110
+
Electing to collect guides will take all guides and put them together at the position governed by the global theme.
111
+
Further, it will remove any duplicate guide leaving only unique guides in the plot.
112
+
The duplication detection looks at the appearance of the guide, and not the underlying scale it comes from.
113
+
Thus, it will only remove guides that are exactly alike.
114
+
If you want to optimize space use by putting guides in an empty area of the layout, you can specify a plotting area for collected guides:
One of the tenets of patchwork is that the plots remain as standard ggplot objects until rendered. This means that they are amenable to modification after they have been assembled. The specific plots can by retrieved and set with `[[]]` indexing:
122
+
One of the tenets of patchwork is that the plots remain as standard ggplot objects until rendered.
123
+
This means that they are amenable to modification after they have been assembled.
124
+
The specific plots can by retrieved and set with `[[]]` indexing:
103
125
104
126
```{r}
105
127
p12 <- p1 + p2
106
128
p12[[2]] <- p12[[2]] + theme_light()
107
129
p12
108
130
```
109
131
110
-
Often though, it is necessary to modify all subplots at once to e.g. give them a common theme. patchwork provides the `&` for this scenario:
132
+
Often though, it is necessary to modify all subplots at once to e.g. give them a common theme.
Once plots have been assembled, they start to form a single unit. This also means that titles, subtitles, and captions will often pertain to the full ensemble and not individual plots. Titles etc. can be added to patchwork plots using the `plot_annotation()` function.
147
+
Once plots have been assembled, they start to form a single unit.
148
+
This also means that titles, subtitles, and captions will often pertain to the full ensemble and not individual plots.
149
+
Titles etc. can be added to patchwork plots using the `plot_annotation()` function.
125
150
126
151
```{r}
127
152
p34 <- p3 + p4 + plot_annotation(
@@ -143,7 +168,9 @@ As the global theme often follows the theme of the subplots, using `&` along wit
143
168
p34 & theme_gray(base_family = "mono")
144
169
```
145
170
146
-
Another type of annotation, known especially in scientific literature, is to add tags to each subplot that will then be used to identify them in the text and caption. ggplot2 has the `tag` element for exactly this and patchwork offers functionality to set this automatically using the `tag_levels` argument. It can generate automatic levels in latin characters, arabic numerals, or roman numerals
171
+
Another type of annotation, known especially in scientific literature, is to add tags to each subplot that will then be used to identify them in the text and caption.
172
+
ggplot2 has the `tag` element for exactly this and patchwork offers functionality to set this automatically using the `tag_levels` argument.
173
+
It can generate automatic levels in latin characters, arabic numerals, or roman numerals
147
174
148
175
```{r}
149
176
p123 <- p1 | (p2 / p3)
@@ -163,13 +190,18 @@ As can be seen, patchwork offers a long range of possibilities when it comes to
163
190
164
191
## Arranging plots on top of each other
165
192
166
-
While a lot of the functionality in patchwork is concerned with aligning plots in a grid, it also allows you to make insets, i.e. small plots placed on top of another plot. The functionality for this is wrapped in the `inset_element()` function which serves to mark the given plot as an inset to be placed on the preceding plot, along with recording the wanted placement etc. The basic usage is like this:
193
+
While a lot of the functionality in patchwork is concerned with aligning plots in a grid, it also allows you to make insets, i.e. small plots placed on top of another plot.
194
+
The functionality for this is wrapped in the `inset_element()` function which serves to mark the given plot as an inset to be placed on the preceding plot, along with recording the wanted placement etc.
195
+
The basic usage is like this:
167
196
168
197
```{r}
169
198
p1 + inset_element(p2, left = 0.5, bottom = 0.4, right = 0.9, top = 0.95)
170
199
```
171
200
172
-
The position is specified by given the left, right, top, and bottom location of the inset. The default is to use `npc` units which goes from 0 to 1 in the given area, but any `grid::unit()` can be used by giving them explicitly. The location is by default set to the panel area, but this can be changed with the `align_to` argument. Combining all this we can place an inset exactly 15 mm from the top right corner like this:
201
+
The position is specified by given the left, right, top, and bottom location of the inset.
202
+
The default is to use `npc` units which goes from 0 to 1 in the given area, but any `grid::unit()` can be used by giving them explicitly.
203
+
The location is by default set to the panel area, but this can be changed with the `align_to` argument.
204
+
Combining all this we can place an inset exactly 15 mm from the top right corner like this:
173
205
174
206
```{r}
175
207
p1 +
@@ -183,14 +215,16 @@ p1 +
183
215
)
184
216
```
185
217
186
-
insets are not confined to ggplots. Any graphics supported by `wrap_elements()` can be used, including patchworks:
218
+
insets are not confined to ggplots.
219
+
Any graphics supported by `wrap_elements()` can be used, including patchworks:
187
220
188
221
```{r}
189
222
p24 <- p2 / p4 + plot_layout(guides = "collect")
190
223
p1 + inset_element(p24, left = 0.5, bottom = 0.05, right = 0.95, top = 0.9)
191
224
```
192
225
193
-
A nice feature of insets is that they behave as standard patchwork subplots until they are rendered. This means that they are amenable to modifications after assembly, e.g. using `&`:
226
+
A nice feature of insets is that they behave as standard patchwork subplots until they are rendered.
227
+
This means that they are amenable to modifications after assembly, e.g. using `&`:
194
228
195
229
```{r}
196
230
p12 <- p1 + inset_element(p2, left = 0.5, bottom = 0.5, right = 0.9, top = 0.95)
This chapter has given a brief overview of some of the composition possibilities provided by patchwork, but is in no way exhaustive. Patchwork provides support for more than just ggplots and allows you to combine grid and base graphic elements with your plots as well if need be. It also allows even more complex designs using the `area()` constructor instead of the textual representation showcased here. All of these functionalities and many more are covered in the different guides available on its website: <https://patchwork.data-imaginist.com>
242
+
This chapter has given a brief overview of some of the composition possibilities provided by patchwork, but is in no way exhaustive.
243
+
Patchwork provides support for more than just ggplots and allows you to combine grid and base graphic elements with your plots as well if need be.
244
+
It also allows even more complex designs using the `area()` constructor instead of the textual representation showcased here.
245
+
All of these functionalities and many more are covered in the different guides available on its website: <https://patchwork.data-imaginist.com>
0 commit comments