-
Notifications
You must be signed in to change notification settings - Fork 7
/
ioslides_deck_dc.Rmd
623 lines (412 loc) · 15.2 KB
/
ioslides_deck_dc.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
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
---
title: "So You're Thinking About Animating a Plot in R"
subtitle: "DC R 2019"
author: "Stephanie Kirmer <BR> [www.stephaniekirmer.com](http://www.stephaniekirmer.com) <BR> @[data_stephanie](http://www.twitter.com/data_stephanie)"
date: "November 8, 2019"
output:
ioslides_presentation:
widescreen: true
---
```{r, include = FALSE}
library(gapminder)
library(ggplot2)
library(gganimate)
```
# Follow Along! | https://github.com/skirmer/animating_dataviz
<div class="notes">
Introductions, get the about-me out of the way and give a quick overview
</div>
## Should you animate that plot?
### **Bad Reasons**
- It's neat
- I want to show off
- One of my dimensions is time
### **Good Reasons**
- I have four or more meaningful data dimensions
- I want to display many levels of a single dimension
Also: If you need to see all the data points at the same time, don't use animation
<div class="notes">
Balance between encouragement and caution
Time does not necessarily need to be represented in actual temporality
Talk about what Meaningful Data Dimensions means
Talk about levels of dimension as factor
</div>
## Use Case Ideas
<div class="columns-2">
```{r, echo = FALSE, cache=TRUE}
samplep <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent, ncol = 2) +
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
animate(samplep, width = 450)
```
<BR>
Thomas Lin Pedersen's example from https://gganimate.com - (@[thomasp85](https://twitter.com/thomasp85))
- Data in meaningful groups changing over long periods of time
- What does this plot actually tell the viewer?
</div>
<div class="notes">
TLP is the creator of gganimate- encourage people to check his stuff out
Discuss this specific plot- what can you learn from it and what is it not good for?
- trends? y/n
- comparisons across continents? y/n
- comparisons within continents across countries? y/n
</div>
## More Good Examples {.smaller}
<div class="columns-2">
### Multiple animations
- Mikey Harper: https://mikeyharper.uk/animated-plots-with-r/
### GIS
- Daniela Vasquez: https://d4tagirl.com/2017/05/how-to-plot-animated-maps-with-gganimate
### Forecasting
- Hao Ye: https://haoye.us/post/2018-09-03-gganimate/
- Troy @ Peak Maximum [https://peakmaximum.com/](https://peakmaximum.com/2018/12/22/visualizing-prophet-with-gganimate/)
<BR>
### Shadowing
- Len Kiefer: https://twitter.com/lenkiefer/status/1102274364995719169
### Model Performance
- Dariya Sydykova: [https://github.com/dariyasydykova](https://github.com/dariyasydykova/open_projects/tree/master/ROC_animation)
### Groups Over Time with Reveal
- Jovan Lekovic: [https://constantlyplotting.com/](https://constantlyplotting.com/snapshots-young-people-arent-watching-much-tv/)
</div>
<div class="notes">
Show these examples in realtime - tell people to google for more to get ideas
It can be hard to imagine your data in an animation, so looking at examples of how other people do it can be v helpful
</div>
## Think About Design
- What information do you want the audience to take away?
- Movement and speed lead the viewer to interpretation of the data - this can be good or bad
- Each static frame should be informative alone as well
(Be open to the idea that you might not need animation to be most effective)
<div class="notes">
Thinking about your own data and planning ahead is huge
</div>
# The Tools
## Key Packages to Get
```{r}
library(ggplot2) # make plots
library(gganimate) # animate the plots
library(gifski) # render gifs
```
Optional, maybe interesting packages
```{r}
library(transformr) # additional smooth transformations
library(patchwork) # arrange multiple plots in a single layout
library(ggforce) # extended options for ggplot functionality
```
<div class="notes">
To make the example here work, you need the first three for sure- some different transformation options are made available with the fourth
</div>
## Key Concepts
### **Frame**
- Animations are a series of single images (frames) strung together (think of "frames per second")
### **State**
- The current grouping variable being shown. It may be one frame, or a single state representation can stay visible for multiple frames
### **Transition**
- How the visual rendering moves from state to state
### **Enter/Exit**
- How datapoints with no predecessor or follower are handled
<div class="notes">
These are really concepts in the space of animation generally
Think about old timey cartooning, or flipbooks- one cel at a time, moving past you quick enough that it looks like movement
What we are doing here is not dissimilar.
Frame: a measure of time. State: a property of your data.
Option: look back at the examples from earlier and talk about what the states are?
</div>
# Building the Viz
## Example Project
Using data on displaced persons living in South Africa, show patterns and trends in refugee movement over twenty-plus years.
1. Where do most refugees come from?
- These may be places with high instability or political turmoil at a given time
2. Over time, are the most common countries of origin about the same?
3. Is the number of refugees coming from the most common countries of origin stable?
Data Source: UNHCR via data.world: [UNHCR's populations of concern residing in South Africa](https://data.world/unhcr/244d728f-06e6-4c25-8eed-69e8fe3bffe0)
<div class="notes">
Example for today's purposes - no special reason
</div>
## Initial Data
Grab dataset of displaced persons living in South Africa
```{r, echo = FALSE}
dtset <- read.csv("data/unhcr-time-series-residing-zaf-csv-1.csv", skip = 1)
names(dtset) <- gsub("\\.", "_", names(dtset))
dtset <- dplyr::filter(dtset, X_date_year > 1995)
knitr::kable(head(dtset, 4))
```
## Mutate Data
Select the top 10 countries of origin for each year, apply some filters.
```{r, message=FALSE, error=FALSE, warning=FALSE, echo=FALSE}
library(magrittr)
library(dplyr)
plotDT <- dtset %>%
filter(X_country_origin != "Various/Unknown") %>%
filter(X_population_type == "Refugees (incl. refugee-like situations)") %>%
group_by(X_date_year) %>%
mutate(rank = rank(-X_affected, ties.method = "first") * 1) %>%
ungroup() %>%
filter(rank <= 10) %>%
data.frame()
plotDT$X_country_origin <- ifelse(plotDT$X_country_origin == "Serbia and Kosovo (S/RES/1244 (1999))", "Serbia/Kosovo", as.character(plotDT$X_country_origin))
knitr::kable(head(plotDT, 4))
```
## Grouped Bar Plot - Code
Group by country, bar height is affected persons
```{r}
baseplot1 <- ggplot(
plotDT,
aes(X_date_year,
group = X_country_origin,
fill = as.factor(X_country_origin),
color = as.factor(X_country_origin))) +
theme_bw() +
theme(legend.position = "bottom") +
geom_bar(aes(y = X_affected), stat = "identity", position = "dodge")
```
## Grouped Bar Plot - Rendered
Ew. This is not effective.
```{r, echo=FALSE, fig.width = 10}
baseplot1
```
## Can Faceting Solve?
No.
```{r, echo=FALSE, fig.width = 10}
baseplot1+facet_grid(X_country_origin~.)
```
## Can Faceting Solve?
Nooooo.
```{r, echo=FALSE, fig.width = 10}
baseplot1+facet_wrap(X_country_origin~.)
```
## What if we try to make a race plot instead?
![Let's make this!](final_race_plot2.gif)
## Reformat the Plot
From this...
```{r}
baseplot1 <- ggplot(plotDT,
aes(X_date_year,
group = X_country_origin,
fill = as.factor(X_country_origin),
color = as.factor(X_country_origin))) +
theme_bw() +
theme(legend.position = "bottom") +
geom_bar(aes(y = X_affected), stat = "identity", position = "dodge")
```
## Reformat the Plot
To this!
```{r}
baseplot3 <- ggplot(plotDT,
### <b>
aes(x=rank,
### </b>
group = X_country_origin,
fill = as.factor(X_country_origin),
color = as.factor(X_country_origin)))+
theme_bw()+
### <b>
theme(legend.position = "none", # Give the plot enough space to have labels
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.margin = margin(1,1,1,5, "cm"))+
geom_text(aes(y = 0, label = paste(X_country_origin, " ")), vjust = 0.2, hjust = 1) +
coord_flip(clip = "off", expand = FALSE, ylim = c(0, 50000)) + # Flip the plot
### </b>
geom_bar(aes(y = X_affected), stat = "identity", position = "identity")+
### <b>
scale_y_continuous(labels = scales::comma) +
scale_x_reverse()
### </b>
```
</div>
## Render
Now we have the different "frames" all layered on top of each other.
```{r, echo = FALSE, fig.width = 10}
baseplot3
```
# Develop the Animation
## Basic Version
Literally add one more line of code to your ggplot object.
```{r}
animp <- baseplot3 +
### <b>
transition_states(X_date_year)
### </b>
```
## It's Alive!
<div class="columns-2">
```{r, fig.width = 8, fig.height=4, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20, height = 400)
```
It's nice, but we can do better
1. Viewer can't tell what the frames represent
2. Y axis needs labeling, bars need clear labels
3. It's moving abruptly between frames
</div>
## Improvements
Solving Problem 1 and 2: Added a descriptive title/label that indicate the year of the frame, label bars
```{r}
animp <- baseplot3 +
### <b>
geom_text(aes(y = X_affected,
label = as.character(X_affected)),
color = "black", vjust = 0.2, hjust = -.5)+
labs(title = "Refugees Residing in South Africa by Origin, {closest_state}"
, y="Affected Persons")+
### </b>
transition_states(X_date_year)
```
## Dynamic Labels
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## How Things Move
Solving Problem 3: how do we want the animation elements to move?
Choose exit and enter styles: grow and shrink?
```{r}
animp <- baseplot3 +
geom_text(aes(y = X_affected,
label = as.character(X_affected)),
color = "black", vjust = 0.2, hjust = -.5)+
labs(title = "Refugees Residing in South Africa by Origin, {closest_state}"
, y="Affected Persons")+
transition_states(X_date_year)+
### <b>
enter_grow() +
exit_shrink()
### </b>
```
## Exit/Enter
It's interesting, but probably not serving the project objectives
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## How More Things Move
Choose easing:
* movement between positions (moving on page, not exiting or entering)
* transition speed can change as it moves (functions may be cubic, quartic, etc)
Here we see `quartic-in-out`
```{r}
animp <- baseplot3 +
geom_text(aes(y = X_affected,
label = as.character(X_affected)),
color = "black", vjust = 0.2, hjust = -.5)+
labs(title = "Refugees Residing in South Africa by Origin, {closest_state}"
, y="Affected Persons")+
transition_states(X_date_year)+
### <b>
ease_aes('quartic-in-out')
### </b>
```
## Quartic Easing
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## Bouncy Easing
Easing `back-in-out`: Feels a little cartoony- interesting, but again perhaps not what we need
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animp <- baseplot3 +
geom_text(aes(y = X_affected,
label = as.character(X_affected)),
color = "black", vjust = 0.2, hjust = -.5)+
labs(title = "Refugees Residing in South Africa by Origin, {closest_state}"
, y="Affected Persons")+
transition_states(X_date_year)+
### <b>
ease_aes('back-in-out')
### </b>
animate(animp, fps = 10, duration = 20)
```
## How Fast Things Move (Timing)
In addition to entry, exit, and transition easing:
- Set pace for the states/transitions
- Ensure no smoothing between the end and restarting
```{r}
animp <- baseplot3 +
geom_text(aes(y = X_affected,
label = as.character(X_affected)),
color = "black", vjust = 0.2, hjust = -.5)+
labs(title = "Refugees Residing in South Africa by Origin, {closest_state}"
, y="Affected Persons")+
### <b>
transition_states(X_date_year,transition_length = 5,
state_length = c(rep(.25, 21), 20), wrap = FALSE)+
### </b>
ease_aes('linear')+
enter_fade() +
exit_fade()
```
## Set Timings
Slower pace feels smoother, and doesn't insinuate that the last frame and the first flow into each other
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## Transition States Drilldown
<!-- {.smaller} -->
<div class="columns-2">
```{r, eval=FALSE}
transition_states(
states = X_date_year,
transition_length = 5,
state_length = c(rep(.25, 21), 20),
wrap = FALSE)+
```
**States**: Assign the state unit - here we use the year.
**Transition length**
- period of time we use for change from state to state
- slow it down if you want a smooth looking animation
**State length**
- period of time where the state stays static
- here I am making the very last state stay static longer
**Wrap**: determine whether to apply transition smoothing between the end and restarting
<BR>
Experiment with these settings to get the look you want!
## Render the Final Animation
```{r, fig.width = 10, fig.height=6, eval=TRUE, cache = TRUE}
animate(animp, fps = 10, width = 800, height = 450)
```
<div class="notes">
Shorter duration: faster animation
More FPS: smoother animation, but bigger file
The Math:
20 second animation
10 frames per second
200 frame animation total
Relative values:
Static state = .25
Transition = 5
22 years = 22 static states
22 transitions, not including start and return to start (no transitions there)
22 x .25 = 5.5
22 x 5 = 110
200/115.5 = 1.73 frames per state-unit
.43 frames per static state = .043 sec per static state
8.65 frames per transition = .865 sec per transition
</div>
## Save the Animation
```{r, eval=FALSE}
anim_save(filename = "final_race_plot2.gif")
```
## Lessons to Take Away
### **1. Do you need animation?**
Prioritize the transmission of information **effectively**
Make your plot serve the audience, don't be fancy if it's not helpful
### **2. Make your animation awesome**
Think carefully about transitions and speed
- Figure out the in/out transitions that make sense and are not misleading/distracting
- Give the eye enough time to understand each state
Get feedback and test your animation on naive viewers
<div class="notes">
</div>
## Further Links/Reference
https://gganimate.com/
https://ggplot2.tidyverse.org/
https://ggforce.data-imaginist.com/
https://stackoverflow.com/questions/53162821/animated-sorted-bar-chart-with-bars-overtaking-each-other/53163549 (Hat tip to Jon Spring for this awesome starting point for this kind of thing!)
### Even More Examples
- https://twitter.com/WeAreRLadies/status/1123209959020220419
Similar project in D3: https://observablehq.com/@johnburnmurdoch/bar-chart-race-the-most-populous-cities-in-the-world
# Thank you! | [www.stephaniekirmer.com](http://www.stephaniekirmer.com) | @[data_stephanie](http://www.twitter.com/data_stephanie) | [www.journera.com](http://www.journera.com)