forked from rjake/talks-and-presentations
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_viz.Rmd
More file actions
652 lines (491 loc) · 12.9 KB
/
data_viz.Rmd
File metadata and controls
652 lines (491 loc) · 12.9 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
---
title: "Data Visualization in R"
author: "Jake Riley Sept. 23, 2020"
output:
powerpoint_presentation:
reference_doc: assets/reference.pptx
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding, output_dir = "output")
})
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
out.height = "300px",
cache = TRUE
)
```
## Today's talk
- Intro
- What is ggplot
- Tips & Tricks
- Best Practices
- Try it out
## An intro
- Jake Riley
- Clinical Data Analyst at Children's Hospital of Philadelphia
- Avid package developer
- dogdad
@yake_84
## Before we get started
- this talk is aimed at intermediate `ggplot2` users
- everything is within the `tidyverse` framework & R for Data Science (R4DS)
- the pipe `%>%` is used in many places and allows us to create a sequence of manipulations
`iris %>% head()` is the same as `head(iris)`
- the `+` used with `ggplot()` is another type of pipe
- you can pipe from a `dplyr` sequence into a `ggplot()` sequence
## What is ggplot?
- grammar of graphics
- just like every sentence has a **subject, verb, and noun**, every chart has a **coordinate system, geom, and aesthetics**
- the hope is that we will invent new types of charts
```{r}
econ <-
ggplot(economics, aes(date, unemploy))
econ +
geom_line()
econ +
geom_point()
econ +
geom_area(fill = "lightblue") +
geom_point()
econ +
geom_area(fill = "lightblue") +
geom_point() +
coord_polar()
```
```{r}
library(tidyverse)
p <-
ggplot(mpg) +
geom_count(aes(cty, hwy), alpha = 0.5, color = "navyblue") +
theme_bw() +
theme(legend.position = "none")
```
## an example
```{r grammar, eval = FALSE}
p
p + coord_polar()
```
```{r}
gridExtra::grid.arrange(
p,
p + coord_polar(),
ncol = 1
)
```
## Demystifying `aes()`
- `aes()` = aesthetics
- dynamic, data driven **variables** go inside the `aes()`
- constant, static **values** go outside
- the first 2 arguments of `aes()` are `x` and `y` and I will mostly omit naming these
## Note the difference
- `geom_point(aes(color = class, size = n), ...)`
- `geom_point(aes(...), color = "blue", size = 2)`
```{r out.width = c("50%", "50%"), fig.show = "hold", echo = FALSE}
df <-
mpg %>%
count(cyl = factor(cyl), cty, hwy)
gridExtra::grid.arrange(
ggplot(df, aes(x = cty, y = hwy)) + geom_point(aes(color = cyl, size = n)),
ggplot(df, aes(x = cty, y = hwy)) + geom_point(color = "blue", size = 2),
nrow = 1
)
```
# Tips & Tricks
## Descending bar charts
The number one things I get asked is how to make a barchart go in descending order.
```{r}
ggplot(mpg, aes(y = class)) +
geom_bar()
```
## Arrange by volume: `fct_infreq()`
```{r}
mpg %>%
mutate(class = fct_infreq(class)) %>%
ggplot(aes(y = class)) +
geom_bar()
```
## Arrange in descending order: `fct_rev()`
```{r, }
mpg %>%
mutate(class = fct_infreq(class)) %>%
ggplot(aes(y = fct_rev(class))) +
geom_bar()
```
## Aggregated data: `fct_reorder()`
```{r}
mpg %>%
count(class) %>%
mutate(class = fct_reorder(class, n, sum))
```
## Aggregated data: `geom_col()`
```{r}
mpg %>%
count(class) %>%
mutate(class = fct_reorder(class, n, sum)) %>%
ggplot(aes(n, class)) +
geom_col()
```
## Too many bars
```{r}
ggplot(mpg, aes(y = model)) +
geom_bar()
```
## Too many bars: `fct_lump()`
```{r}
mpg %>%
mutate(
manufacturer =
fct_lump(manufacturer, 5) %>%
fct_infreq() %>%
fct_rev()
) %>%
ggplot(aes(y = manufacturer)) +
geom_bar()
```
## Sorting by fill
```{r}
ggplot(mpg, aes(y = manufacturer, fill = (cyl == 8))) +
geom_bar()
```
## Sorting by fill
```{r}
mpg %>%
mutate(
cyl_8 = (cyl == 8),
manufacturer = fct_reorder(manufacturer, cyl_8, sum)
) %>%
ggplot(aes(manufacturer, fill = cyl_8)) +
geom_bar() +
coord_flip()
```
## Sorting by fill (percent)
```{r}
mpg %>%
mutate(
cyl_8 = (cyl == 8),
manufacturer =
fct_reorder(manufacturer, cyl_8, mean)
) %>%
ggplot(aes(y = manufacturer, fill = cyl_8)) +
geom_bar(position = "fill")
```
## `facet_grid()` vs `facet_wrap()`
```{r}
p <-
mpg %>%
filter(class != "2seater", cyl != 5) %>%
ggplot(aes(cty, hwy, color = class)) +
geom_count(alpha = 0.5) +
lims(x = c(0, NA), y = c(0, NA)) +
# can also use xlim() or scale_x_continuous
guides(color = FALSE) +
theme(aspect.ratio = 1)
p
```
## `facet_grid()`: new syntax
```{r}
# this is the new syntax, replaces `facet_grid(~class)`
p + facet_grid(cols = vars(class))
```
## facets: scales
```{r}
# scales allows the x & y to vary
# also "free_x", "free_y"
p + facet_grid(cols = vars(class), scales = "free")
```
## facets: scales & space
```{r}
p + facet_grid(cols = vars(class), scales = "free", space = "free")
```
## facets: margins
```{r}
p +
facet_grid(
rows = vars(year),
cols = vars(class),
margins = TRUE
)
```
## facet_wrap: # of columns/rows
```{r}
# also nrow
p + facet_wrap(~class, ncol = 3)
```
## facet_wrap: scales
```{r}
# space does not work with facet_wrap()
p + facet_wrap(~class, ncol = 3, scales = "free")
```
## facets: (a + b)
```{r}
# also works with facet_grid
p + facet_wrap(~class + year, nrow = 2)
```
## `scale_*_identity()`
Sometimes I want to have better control over colors & sizes.
Here, I am hard coding the colors
```{r}
df <-
mpg %>%
mutate(category =
case_when(
cty < 14 ~ "coral",
cty > 19 ~ "turquoise",
TRUE ~ "grey40"
)
)
```
## `scale_color_identity()`
```{r}
ggplot(df, aes(cty, hwy, color = category)) +
geom_count() +
scale_color_identity()
```
## `scale_fill_identity()`
```{r}
ggplot(df, aes(cty, fill = category)) +
geom_dotplot() +
scale_fill_identity()
```
## Geom defaults
```{r}
# default behavior
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
# with updates
update_geom_defaults(
"point",
list(color = "dodgerblue", size = 3)
)
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
# revert
update_geom_defaults("point", list(color = "black", size = 2))
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
```
## theme default
```{r}
# will apply to all future charts
theme_set(theme_classic(base_size = 14))
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
theme(panel.grid.major.x = element_line(color = "orange"))
# can revert back using this code
theme_set(theme_gray())
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
```
# Best practices
## The golden ratio 1:1.6
- Try to give your charts the proportion of a credit card
- Also look this up
```{r, out.width = c("60%", "40%"), fig.show = "hold"}
p <- ggplot(mpg, aes(cty)) + geom_area(stat = "count")
gridExtra::grid.arrange(
p + coord_fixed(1/10),
p + theme(aspect.ratio = 1/1.6), # ratio depends on the units
nrow = 1,
widths = c(0.7, 0.3)
)
```
## Dealing with spaghetti charts
```{r echo = FALSE}
set.seed(1234)
df <-
expand.grid(
group = LETTERS[1:5],
x = as.Date(paste0("2019-01-", 1:30)),
KEEP.OUT.ATTRS = FALSE
) %>%
mutate(
rand = rnorm(-74:75, mean = 0, sd = 4),
y = ((row_number()/2) %/% 3) + rand + 10,
category = factor(ntile(y, 4))
)
#mutate(y = row_number() %% 3 + row_number() %% 4 + row_number()/150)
my_theme <-
theme(
panel.background = element_rect(fill = "white"),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 5/30,
legend.position = "bottom"
)
```
This is one of the most common questions to answer: **change for multiple categories over time**. This often results in a chart like the one here It is hard to read but there are some ways you can help your audience
```{r}
ggplot(df, aes(x, y, color = group)) +
geom_line()
```
## Highlight the focus & use an informative title
```{r}
ggplot(df, aes(x, y, group = group)) +
geom_line(data = filter(df, group != "B"), color = "grey", size = 1) +
geom_line(data = filter(df, group == "B"), color = "black", size = 2) +
labs(title = "Group B is currently in the lead")
```
## Facet the data
```{r}
ggplot(df, aes(x, y, fill = (group == "B"))) +
geom_area() +
facet_grid(cols = vars(group)) +
scale_fill_manual(values = c("grey", "navyblue"))
```
## Try a heatmap but beware
```{r}
h <-
ggplot(df, aes(x, fct_reorder(group, y, last), fill = y)) +
geom_tile(color = "white") +
scale_fill_gradient2(
low = "red", mid = "yellow", high = "darkgreen", midpoint = 25
) +
my_theme +
labs(title = "An improvement, but not colorblind friendly")
h
```
## not colorblind safe
`remotes::install_github("clauswilke/colorblindr")`
```{r}
colorblindr::c
```
## Not every point needs a color
```{r}
ggplot(df, aes(x, fct_reorder(group, y), fill = category)) +
geom_tile(color = "white", size = 0.1) +
scale_fill_manual(
values = c("grey90", "grey85", "palegreen3", "seagreen4"),
labels = c("poor", "good", "very good", "excellent")
) +
my_theme +
labs(title = "All teams are now performing their best")
```
# Partner Activity: Extensions & Addins
## Partner activity
- left side: focus on extensions
- right side: focus on add-ins
- bonus: my `simplecolors` package
- with your partner, review the code and resources below
- find a function or feature that you think is interesting or useful
- place screenshots here https://bit.ly/2XiG5C7
- you don't need to run the code, you can use images from the vignettes
- we'll share at the end
## Extensions & Addins
- [ggplot extensions](https://www.ggplot2-exts.org/ggiraph.html)
- [ggplot addins](https://github.com/daattali/addinslist)
## Extensions
- [ggradar - spider/radar plots](https://github.com/ricardo-bion/ggradar)
- [gganimate](https://gganimate.com/)
- [ggrepel](https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html)
- [ggforce](https://cran.rstudio.com/web/packages/ggforce/vignettes/Visual_Guide.html)
- [cowplot](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html)
- [more](https://www.ggplot2-exts.org/ggiraph.html)
## `addinslist`:
`addinslist::addinslistAddin()`
```{r eval = FALSE, echo = TRUE}
# install.packages("addinslist")
# install.packages("esquisse")
# install.packages("ggedit")
# install.packages("ggThemeAssist")
# install.packages("colourpicker")
data(iris)
data(mpg)
p <-
ggplot(mpg, aes(cty, hwy)) +
geom_point()
```
## Addins in action
```{r eval = FALSE, echo = TRUE}
# esquisse
esquisse:::esquisser()
esquisse:::esquisser(mpg)
# others
ggThemeAssist::ggThemeAssistGadget(p)
ggedit(p)
colourpicker::colourPicker()
```
## `simplecolors`
https://rjake.github.io/simplecolors/articles/intro.html
```{r}
#devtools::install_github("rjake/simplecolors")
library(simplecolors)
simplecolors::show_colors(labels = FALSE)
```
## `simplecolors`
Three main functions:
`sc()` `sc_across()` `sc_*()`
```{r, eval = FALSE}
p <-
ggplot(mpg, aes(hwy, fill = drv)) +
geom_density(alpha = 0.3)
p + scale_fill_manual(values = sc("blue3", "red3", "violet3"))
p + scale_fill_manual(values = sc_pink(light = c(1,3,5)))
p + scale_fill_manual(values = sc_across("RTV", light = 4, sat = "bright"))
```
```{r, echo = FALSE}
p <-
ggplot(mpg, aes(hwy, fill = drv)) +
geom_density(alpha = 0.3)
p1 <- p + scale_fill_manual(values = sc("blue3", "red3", "violet3"))
p2 <- p + scale_fill_manual(values = sc_pink(light = c(1,3,5)))
p3 <- p + scale_fill_manual(values = sc_across("RTV", light = 4, sat = "bright"))
gridExtra::grid.arrange(p1,p2,p3, nrow = 1)
```
# Appendix
## R4DS
R for Data Science is a book all about the `tidyverse`. It is less "data science-y" and more about data manipulation and visualization. It is free online [here](https://r4ds.had.co.nz/) as well as available for sale.
## Stackoverflow
- try `datapasta` for a minimal reprex
- include images rather than links
- incorporate `styler`
## Cheatsheet
https://github.com/rstudio/cheatsheets/raw/master/data-visualization-2.1.pdf
## Take care when cropping data
The usual methods to "zoom in" can yield unexpected results when stat_ geoms are used. For example, `geom_boxplot()` calls `stat_boxplot()` and filters out data **before** doing the stats and your boxplot will keep readjusting the quartiles
```{r, echo = FALSE}
p <-
ggplot(diamonds, aes("", price)) +
geom_boxplot()
p
find_limits <- function(x) {
floor(ggplot_build(x)$data[[1]][2:4])
}
```
## Use `coord_cartesian()` to zoom in
Do not use `ylim()` or `scale_*_continuous()`
```{r}
# find_limits() is a custom function
bind_rows(
find_limits(p),
find_limits(p + ylim(0, 12000)),
find_limits(p + scale_y_continuous(limits = c(0, 12000))),
find_limits(p + coord_cartesian(ylim = c(0, 12000)))
)
```
# Answers
## Q1
```{r}
mpg %>%
mutate(
drv = fct_infreq(drv) %>% fct_rev()
) %>%
ggplot(aes(drv)) +
geom_bar() +
coord_flip()
```
## Q2
```{r}
mpg %>%
mutate(
cyl_8 = (cyl == 8),
manufacturer = fct_reorder(manufacturer, cyl_8, mean)
) %>%
ggplot(aes(manufacturer, fill = cyl_8)) +
geom_bar(position = "fill") +
coord_flip()
```