-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
code.Rmd
150 lines (109 loc) · 3.51 KB
/
code.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "Untitled"
author: "Bryan Jenks"
date: "8/28/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.height=9,
fig.width=9)
# Import Necessary Libraries
library(ggplot2) # Create charts to display
library(patchwork) # Organize chart display with simple syntax
library(dplyr) # Data filtering during plot generation
```
```{r Top_to_Bottom}
# =============================== #
# Assign your plots to variables #
# =============================== #
top <- ggplot(diamonds) +
aes(x = cut, y = carat, fill = color) +
geom_boxplot() +
scale_fill_hue() +
theme_minimal()
bottom <- ggplot(diamonds) +
aes(x = color, fill = clarity) +
geom_bar() +
scale_fill_hue() +
theme_minimal()
# ========================================================= #
# Simple math-like syntax to organize your plots vertically #
# ========================================================= #
top / bottom
```
```{r Side_by_Side}
# =============================== #
# Assign your plots to variables #
# =============================== #
left <- mpg %>%
filter(!(manufacturer %in% c("audi", "chevrolet", "land rover", "mercury",
"toyota", "volkswagen"))) %>%
ggplot() +
aes(x = model, y = manufacturer, fill = cyl) +
geom_tile(size = 1L) +
scale_fill_gradient() +
theme_minimal()
right <- ggplot(mpg) +
aes(x = class, fill = class) +
geom_bar() +
scale_fill_hue() +
coord_flip() +
theme_minimal()
# ================================================= #
# Simple syntax to organize your plots horizontally #
# ================================================= #
left + right
```
```{r Patchwork_Without_Variables}
# ================================= #
# Generate your plots then combine #
# ================================= #
mpg %>%
filter(!(manufacturer %in% c("audi",
"chevrolet",
"land rover",
"mercury",
"toyota",
"volkswagen"))) %>%
ggplot() +
aes(x = model, y = manufacturer, fill = cyl) +
geom_tile(size = 1L) +
scale_fill_gradient() +
theme_minimal() + # <-- HERE IS THE EXTRA PLUS SYMBOL
ggplot(mpg) +
aes(x = class, fill = class) +
geom_bar() +
scale_fill_hue() +
coord_flip() +
theme_minimal()
# ================================================== #
# Without Variables you can also just add your
# ggplots together with another plus symbol like the
# native ggplot2 syntax so it all flows seamlessly
# ================================================== #
```
```{r Smart_Grid_Format}
# ======================================================= #
# By default, patchwork will try to keep the grid square,
# and fill it out in row order
# ======================================================= #
top + bottom + left + right
```
```{r Custom_Grid}
# =========================================================== #
# This can be controlled with the addition of a plot_layout()
# =========================================================== #
top + bottom + left + right + plot_layout(nrow = 3, byrow = FALSE)
```
```{r Stacking_and_Packing}
# =============================================== #
# Using a simple syntax we can easily control the
# layout of the plots
# =============================================== #
(left | (top / bottom)) / right
```
```{r Titling_The_Aggregate_Display}
(left | (top / bottom)) +
plot_annotation(title = 'The awesomeness that is Patchwork')
```