-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphTypes.qmd
More file actions
474 lines (339 loc) · 9.61 KB
/
GraphTypes.qmd
File metadata and controls
474 lines (339 loc) · 9.61 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
---
title: "Graph types"
author: "Sean Raleigh <br> Westminster University"
execute:
echo: false
format:
revealjs:
theme: sky
incremental: false
embed-resources: true
---
# Data preparation
```{r}
library(palmerpenguins)
library(gt)
library(tidyverse)
library(ggbeeswarm)
library(viridis)
library(janitor)
library(tsibble)
theme_resize <- theme(title = element_text(size = rel(1.5)),
axis.title = element_text(size = rel(1.25)),
axis.text = element_text(size = rel(1)))
```
## Data preparation
- Make sure your data is "tidy":
- Observations are rows.
- Variables are columns.
- Every cell is a single value.
- First row (and only first row) consists of variable names.
- No extra stuff outside the rectangle.
## Data preparation
Measurements for penguins in the Palmer Archipelago, Antarctica.
```{r}
penguins <- penguins %>%
drop_na()
penguins %>%
head(n = 20) %>%
gt()
```
# Identifying variable types
## Identifying variable types
- Categorical (qualitative, nominal, factor)
- Classifies data by category.
- color, species, sex
- Numerical (quantitative, scale, interval/ratio)
- Numerical measurements, usually with meaningful units.
- height, GDP, score
## Identifying variable types
**CAREFUL!**
Numbers are not always numerical.
- *Do you own a car?*
- 0 = "No", 1 = "Yes"
- *What is your zip code?*
# Single variable
- Single variables usually won't answer very interesting questions by themselves.
- Graphs of single variables are often valuable for exploring your data, but generally not suitable for inclusion in the final product.
## Single categorical variable
Penguin species.
```{r}
penguins %>%
select(species) %>%
head(n = 20) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Single categorical variable
- Frequency table
```{r}
tabyl(penguins, species) %>%
select(-percent) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Single categorical variable
- Bar chart
```{r}
ggplot(penguins, aes(x = species)) +
geom_bar() +
labs(title = "Penguin species",
x = "Species",
y = "Count") +
theme_resize
```
## Single categorical variable
- ~~Pie chart~~ **Danger! Danger!**
```{r}
penguins %>%
count(species) %>%
rename(Species = species) %>%
ggplot(aes(x = "", y = n, fill = Species)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y") +
theme_void() +
scale_fill_viridis(discrete = TRUE)
```
## Single categorical variable
- Relative frequency table
```{r}
tabyl(penguins, species) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Single categorical variable
<a href = "http://www.statschat.org.nz/wp-content/uploads/2015/06/devourThePie3.gif" target = "_blank">Click here to see how to improve a pie chart.</a>
## Single numerical variable
Penguin body mass in grams.
```{r}
penguins %>%
select(body_mass_g) %>%
head(n = 20) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Single numerical variable
- Histogram
```{r}
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram(color = "black", fill = "white",
binwidth = 250, boundary = 1000) +
labs(title = "Penguin body mass (g)",
x = "Body mass (g)",
y = "Count") +
theme_resize
```
## Single numerical variable
- Tabular summaries
"Five-number summary"
```{r}
penguins %>%
reframe(Percentiles = c("0%", "25%", "50%", "75%", "100%"),
Body_mass = quantile(body_mass_g)) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Single numerical variable
- A bunch of other types I don't prefer:
> boxplot, stem-and-leaf plot, dotplot
# Multiple variables
There are at least six elements of a plot that can be assigned to variables:
- x-axis (horizontal axis)
- y-axis (vertical axis)
- facets
- color/fill
- size
- shape (e.g., dots vs crosses, solid vs dashed lines, etc.)
## Two categorical variables
Penguin species and island.
```{r}
penguins %>%
select(species, island) %>%
head(n = 20) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Two categorical variables
- Contingency table (okay)
```{r}
tabyl(penguins, species, island) %>%
adorn_totals() %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Two categorical variables
- Contingency table (better)
```{r}
tabyl(penguins, species, island) %>%
adorn_totals() %>%
adorn_percentages("col") %>%
adorn_pct_formatting() %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Two categorical variables
- Side-by-side bar chart (okay)
```{r}
penguins %>%
count(species, island) %>%
bind_rows(tribble(~species, ~island, ~n, # Fix spacing
"Gentoo", "Torgersen", 0)) %>%
ggplot(aes(x = island, y = n, fill = species)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Penguin species distribution by island",
x = "Island",
y = "Count",
fill = "Species") +
scale_fill_viridis(discrete = TRUE) +
theme_resize
```
## Two categorical variables
- Side-by-side bar chart (better)
```{r}
penguins %>%
count(species, island) %>%
group_by(island) %>%
mutate(prop = n / sum(n)) %>%
bind_rows(tribble(~species, ~island, ~n, # Fix spacing
"Gentoo", "Torgersen", 0)) %>%
ggplot(aes(x = island, y = prop, fill = species)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Penguin species distribution by island",
x = "Island",
y = "Count",
fill = "Species") +
scale_fill_viridis(discrete = TRUE) +
theme_resize
```
## Two categorical variables
- Stacked bar chart (okay)
```{r}
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar() +
labs(title = "Penguin species distribution by island",
x = "Island",
y = "Count",
fill = "Species") +
scale_fill_viridis(discrete = TRUE) +
theme_resize
```
## Two categorical variables
- Stacked bar chart (okay)
```{r}
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar(position = "fill") +
labs(title = "Penguin species distribution by island",
x = "Island",
y = "Count",
fill = "Species") +
scale_fill_viridis(discrete = TRUE) +
theme_resize
```
## One categorical and one numerical variable
Penguin species and body mass (g).
```{r}
penguins %>%
select(species, body_mass_g) %>%
head(n = 20) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## One categorical and one numerical variable
- Side-by-side boxplot (okay)
```{r}
ggplot(penguins, aes(x = species, y = body_mass_g)) +
geom_boxplot() +
labs(title = "Penguin body mass (g) by species",
x = "Species",
y = "Body mass (g)") +
theme_resize
```
## One categorical and one numerical variable
- Stacked histogram (better)
```{r}
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram(binwidth = 250, boundary = 1000,
color = "black",
fill = "white") +
facet_grid(rows = vars(species)) +
labs(title = "Penguin body mass (g) by species",
y = "Count",
x = "Body mass (g)") +
theme_resize
```
## One categorical and one numerical variable
- ~~Dynamite plot~~ **Danger! Danger!**
```{r}
ggplot(penguins, aes(x = species, y = body_mass_g, fill = species)) +
stat_summary(geom = "errorbar", fun.data = "mean_sdl",
width = .5, size = 1) +
stat_summary(geom = "bar", fun.y = "mean") +
labs(title = "Penguin body mass (g) by species",
x = "Species",
y = "Mean body mass (g)") +
scale_fill_viridis(discrete = TRUE) +
theme_resize
```
## One categorical and one numerical variable
- Beeswarm (better)
```{r}
ggplot(penguins, aes(y = body_mass_g, x = species)) +
geom_quasirandom() +
labs(title = "Penguin body mass (g) by species",
y = "Count",
x = "Body mass (g)") +
theme_resize
```
## Two numerical variables
Penguin flipper length (mm) and body mass (g).
```{r}
penguins %>%
select(flipper_length_mm, body_mass_g) %>%
head(n = 20) %>%
gt() %>%
tab_options(table.font.size = "75%")
```
## Two numerical variables
- Scatterplot
```{r}
ggplot(penguins, aes(y = flipper_length_mm, x = body_mass_g)) +
geom_point() +
labs(title = "Penguin flipper length (mm) by\nbody mass (g)",
y = "Flipper length (mm)",
x = "Body mass (g)") +
theme_resize
```
## Two numerical variables
- If one variable is ordered (like time) and there is only one observation of y for each x value, use a lineplot.
```{r}
ggplot(as_tsibble(co2), aes(y = value, x = index)) +
geom_line() +
labs(title = "Atmospheric CO2 in ppm (1959 to 1998)\nMauna Loa",
x = "Year",
y = "Atmospheric CO2 (ppm)\n") +
theme_resize
```
## Three or more variables
```{r}
ggplot(penguins, aes(x = island, y = body_mass_g, fill = species)) +
geom_boxplot() +
labs(x = "Island",
y = "Body mass (g)",
fill = "Species") +
scale_fill_viridis(discrete = TRUE) +
theme_resize
```
## Three or more variables
```{r}
ggplot(penguins, aes(x = body_mass_g, y = flipper_length_mm, color = sex)) +
geom_point() +
facet_grid(rows = vars(species),
cols = vars(island)) +
labs(y = "Flipper length (mm)",
x = "Body mass (g)",
color = "Sex") +
scale_color_viridis(discrete = TRUE) +
theme_dark() +
theme_resize +
theme(axis.text = element_text(size = rel(0.75)))
```