-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslides_tidy-viz.qmd
More file actions
338 lines (229 loc) · 6.45 KB
/
slides_tidy-viz.qmd
File metadata and controls
338 lines (229 loc) · 6.45 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
---
format: ### slides
revealjs: ### slides
echo: true ### slides
code-line-numbers: false ### slides
fig-align: center ### slides
slide-number: true ### slides
self-contained: true ### slides
---
# Visualization with _ggplot2_
(A few remarks and tips before the practical session)
# R is the best technology for doing computational science
<p align="right">(Subjectively.)</p>
# _ggplot2_ is the most powerful visualization framework
<p align="right">(Objectively. 🙂)</p>
## _ggplot2_ is a core _tidyverse_ package
<center>{width="50%"}</center>
## "Grammar of Graphics"
A formal syntax and grammar for describing visualizations
<center>{width="30%"}</center>
<center>**What does this mean?**</center>
## Let's consider base R plotting
<br>
```{r}
#| eval: false
library(palmerpenguins)
head(penguins, 7)
```
<br>
```{r}
#| echo: false
library(dplyr)
library(palmerpenguins)
library(kableExtra)
penguins %>%
head(7) %>%
kable %>%
kable_styling(font_size = 20)
```
## A base R histogram
```{r}
#| fig-align: center
hist(penguins$body_mass_g)
```
## A base R histogram
```{r}
#| fig-align: center
hist(penguins$body_mass_g, xlim = c(2000, 8000))
```
## A base R histogram
```{r}
#| fig-align: center
hist(penguins$body_mass_g, xlim = c(2000, 8000),
main = "Histogram of body mass of penguins")
```
## A base R histogram
```{r}
#| fig-align: center
hist(penguins$body_mass_g, xlim = c(2000, 8000),
main = "Histogram of body mass of penguins",
xlab = "Body mass [grams]")
```
## A base R histogram
```{r}
#| fig-align: center
hist(penguins$body_mass_g, xlim = c(2000, 8000),
main = "Histogram of body mass of penguins",
xlab = "Body mass [grams]",
col = "black", border = "white")
```
## A base R histogram
```{r}
#| code-fold: true
#| fig-align: center
par(mfrow = c(1, 3))
species_1 <- filter(penguins, species == "Adelie")
hist(species_1$body_mass_g, xlim = c(2000, 8000),
main = "Species 'Adelie'",
xlab = "Body mass [grams]",
col = "darkgreen", border = "white")
species_2 <- filter(penguins, species == "Chinstrap")
hist(species_2$body_mass_g, xlim = c(2000, 8000),
main = "Species 'Chinstrap'",
xlab = "Body mass [grams]",
col = "darkblue", border = "white")
species_3 <- filter(penguins, species == "Gentoo")
hist(species_3$body_mass_g, xlim = c(2000, 8000),
main = "Species 'Gentoo'",
xlab = "Body mass [grams]",
col = "darkorange", border = "white")
```
#
<center>Base R plots use a single function and specify a
(potentially large) number of parameters which change their
"aesthetic" properties and visual elements.</center>
. . .
<br>
<center>**Creating a new figure (even from the same data) usually requires
a completely different command.**</center>
# _tidyverse_ provides "grammar for data manipulation"...
A "mini-language" with verbs like `select()`, `filter()`, etc.
(Helps us avoid "R bootcamp"-level nightmares.)
# _ggplot2_ provides "grammar for visualizations"...
(To make visualizations easier in a similar sense.)
## Layers in "Grammar of Graphics"
<center>{width="65%"}
</center>
::: {.aside}
<small>Illustration of Layers in Grammar of Graphics from <a href="https://r.qcbs.ca/">
Quebec Centre for Biodiversity Science</a></small>
:::
## Layers in "Grammar of Graphics"
::: {.columns}
::: {.column}
::: {.fragment}
- `data` --- our data frame
:::
::: {.fragment}
- `aesthetics` --- "mapping" of columns to visual properties of
a figure (`x`, or `y` axes, `color`, `shape`, etc.)
:::
::: {.fragment}
- `geoms` --- graphical elements to be plotted (histograms, points, lines, etc.).
:::
::: {.fragment}
- many other customizations
:::
:::
::: {.column}
<br>
<br>
<center>
</center>
:::
::::
# A tiny example
A quick advertisement for the "layering" concept of _ggplot2_
```{r}
#| echo: false
library(ggplot2)
```
## What data to visualize?
```{r}
#| fig-align: center
#| code-line-numbers: "1"
ggplot(penguins)
```
## How to visualize it? (`x` "aesthetic")
```{r}
#| fig-align: center
#| code-line-numbers: "1"
ggplot(penguins, aes(x = body_mass_g))
```
## How to visualize it? (`x` "aesthetic")
```{r}
#| fig-align: center
#| eval: false
#| code-line-numbers: "1"
ggplot(penguins, aes(x = body_mass_g)) +
```
```{r}
#| fig-align: center
#| echo: false
#| code-line-numbers: "1"
ggplot(penguins, aes(x = body_mass_g))
```
## On what dimensions?
```{r}
#| fig-align: center
#| code-line-numbers: "2"
ggplot(penguins, aes(x = body_mass_g)) + # the + is a bit like %>%
xlim(2000, 8000)
```
## What to visualize? ("geom")
```{r}
#| fig-align: center
#| code-line-numbers: "3"
ggplot(penguins, aes(x = body_mass_g)) +
xlim(2000, 8000) +
geom_histogram()
```
## How to visualize it? (`fill` "aesthetic")
```{r}
#| fig-align: center
#| code-line-numbers: "1,"
ggplot(penguins, aes(x = body_mass_g, fill = species)) +
xlim(2000, 8000) +
geom_histogram()
```
## How to visualize it? (adding "facets")
```{r}
#| fig-align: center
#| code-line-numbers: "4"
ggplot(penguins, aes(x = body_mass_g, fill = species)) +
xlim(2000, 8000) +
geom_histogram() +
facet_wrap(~ species)
```
## What to visualize? ("geom" again)
```{r}
#| fig-align: center
#| code-line-numbers: "3"
ggplot(penguins, aes(x = body_mass_g, fill = species)) +
xlim(2000, 8000) +
geom_density() +
facet_wrap(~ species)
```
## More "layers"...
```{r}
#| fig-align: center
#| code-line-numbers: "5"
ggplot(penguins, aes(x = body_mass_g, fill = species)) +
xlim(2000, 8000) +
geom_density() +
facet_wrap(~ species) +
theme_minimal()
```
# Today we're going to learn _ggplot2_ on our metadata and IBD data
# A bit of catch-up work on IBDs from yesterday
We need to make a copy of an IBD processing pipeline.
# Let's get started!
1. Go to [www.bodkan.net/simgen](https://bodkan.net/simgen)
2. Click on _"Visualization with _ggplot2_"_ in the left panel
3. _"Cheatsheets and handouts"_ section in the left panel has
a single-page version of these slides and the _dplyr_ cheatsheet
and _ggplot2_ cheatsheets
4. Open your RStudio and start working!
<br><br><br>
<small>_ggplot2_ version 4.0.0 was released just a few days ago. 🤞😬</small>