forked from jtr13/cc22mw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cheatsheet_patchwork.Rmd
110 lines (80 loc) · 1.91 KB
/
cheatsheet_patchwork.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
# Cheatsheet for 'patchwork'
Maria Pratyusha
Link to presentation and resources :- <https://docs.google.com/presentation/d/1nKju2-jcMC8Ep7tjBPAkacSX181DVra8hawrTlqaF7Q/edit?usp=sharing>
*Note: although this code works fine locally, it is causing issues when rendered through GitHub Actions and therefore the chunks that involve `patchwork` will not be evaluated.*
Import the dataset
```{r}
data("iris")
head(iris)
```
Install the package
```{r}
library("patchwork")
```
Used in combination with ggplot2 so install and load that as well
```{r}
library("ggplot2")
```
Let's plot a few graphs using 'ggplot2'
Scatterplot
```{r}
ggp1 <- ggplot(iris,
aes(x = Sepal.Length,
y = Sepal.Width,
col = Species)) +
geom_point()
ggp1
```
Barchart
```{r}
ggp2 <- ggplot(iris,
aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_bar(stat = "identity")
ggp2
```
Boxplot
```{r}
ggp3 <- ggplot(iris,
aes(x = Species,
y = Sepal.Width,
col = Species)) +
geom_boxplot()
ggp3
```
Composition plot using plot_arithmetic
```{r}
ggp1 + ggp2 + ggp3 + plot_layout(ncol = 1)
ggp1 + ggp2 - ggp3 + plot_layout(ncol = 1)
ggp_sbs <- (ggp1 + ggp2) / ggp3
ggp_sbs
```
area()
```{r}
layout <- c(
area(1, 1),
area(1, 3, 3),
area(3, 1, 3, 2)
)
plot(layout)
ggp1 + ggp2 + ggp3 + plot_layout(design = layout)
```
guide_area()
```{r, eval = FALSE}
ggp1 + ggp2 + ggp3 +
plot_layout(guides = 'collect', ncol = 2)
```
inset_element()
```{r}
ggp1 +
inset_element(ggp2, 0.01, 0.01, 0.7, 0.5) +
inset_element(ggp3, 0.4, 0.6, 0.99, 0.99)
```
plot_annotation
```{r}
ggp1 / (ggp2 | ggp3) +
plot_annotation(tag_levels = 'A')
ggp1 / ((ggp2 | ggp3) + plot_layout(tag_level = 'new')) +
plot_annotation(tag_levels = c('A', '1'))
```