forked from thomasp85/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
129 lines (97 loc) · 3.66 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# patchwork
[![Travis-CI Build Status](https://travis-ci.org/thomasp85/patchwork.svg?branch=master)](https://travis-ci.org/thomasp85/patchwork)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/thomasp85/patchwork?branch=master&svg=true)](https://ci.appveyor.com/project/thomasp85/patchwork)
[![CRAN_Release_Badge](http://www.r-pkg.org/badges/version-ago/patchwork)](https://CRAN.R-project.org/package=patchwork)
[![CRAN_Download_Badge](http://cranlogs.r-pkg.org/badges/patchwork)](https://CRAN.R-project.org/package=patchwork)
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()` but using an API that incites exploration and
iteration.
## Installation
You can install patchwork from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("thomasp85/patchwork")
```
## Example
The usage of `patchwork` is simple: just add plots together!
```{r example}
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:
```{r}
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
```{r}
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
```{r}
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
```{r}
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:
```{r}
(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:
```{r}
(p1 + p2) / p3 + plot_layout(ncol = 1)
```
Now `p1 + p2` and `p3` is on the same level...
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:
```{r}
(p1 + (p2 + p3) + p4 + plot_layout(ncol = 1)) * theme_bw()
```
whereas `^` will recurse into nested levels:
```{r}
(p1 + (p2 + p3) + p4 + plot_layout(ncol = 1)) ^ theme_bw()
```
This is all it does for now, but stay tuned as more functionality is added, such
as collapsing guides, etc...