-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathioslides_deck.Rmd
686 lines (452 loc) · 16.7 KB
/
ioslides_deck.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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
---
title: "Animating Plots in R"
subtitle: "Chicago Data Viz Community x R Ladies Chicago"
author: "Stephanie Kirmer <BR> [www.stephaniekirmer.com](http://www.stephaniekirmer.com) <BR> @[data_stephanie](http://www.twitter.com/data_stephanie)"
date: "July 24 2019"
output:
ioslides_presentation:
widescreen: true
---
```{r, include = FALSE}
library(gapminder)
library(ggplot2)
library(gganimate)
```
# Agenda | <BR> Animation: When and Why? <BR> Introducing the Tools <BR> Hands-On Tutorial
# 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>
## So You're Thinking About Animating | What are some reasons to animate a 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>
## How to Think About the Design
- What information do you want the audience to take away?
- Movement leads the viewer to interpretation of the data - this can be good or bad
- Speed creates meaning, be conscious of that
- 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>
# Tutorial
## Project Overview
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?
Follow along with the notebook: https://github.com/skirmer/animating_dataviz/blob/master/ioslides_deck.Rmd
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>
## Load 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}
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()
```
```{r, echo = FALSE}
plotDT$X_country_origin <- ifelse(plotDT$X_country_origin == "Serbia and Kosovo (S/RES/1244 (1999))", "Serbia/Kosovo", as.character(plotDT$X_country_origin))
```
## Our New Data
```{r, echo=FALSE}
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
```
## What if we try to make a race plot instead?
![Let's make this!](final_race_plot2.gif)
## Adjustment 1: Flip Axis
```{r, fig.width = 8, fig.height=4}
baseplot2 <- baseplot1 +
### <b>
coord_flip(clip = "off", expand = FALSE, ylim = c(0, 50000))
### </b>
```
## Flipped Axis - Render
```{r, echo=FALSE, fig.width = 10}
baseplot2
```
## Adjustment 2: Stop the bar grouping (dodge), switch axis to country from year
Can't use the same stub now.
```{r}
baseplot3 <- ggplot(plotDT,
### <b>
aes(x=rank,
group = X_country_origin,
fill = as.factor(X_country_origin),
color = as.factor(X_country_origin)))+
### </b>
theme_bw()+
theme(legend.position = "bottom")+
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)) +
### <b>
geom_bar(aes(y = X_affected), stat = "identity", position = "identity")
### </b>
```
## Stop Grouping - Render
```{r, echo=FALSE, fig.width = 10}
baseplot3
```
## Check in: baseplot2 vs baseplot3
Left side: grouped bar, sorted by year
Right side: not grouped bar, sorted by rank
```{r, echo = FALSE, fig.width = 10}
library(patchwork)
baseplot2 + baseplot3 + plot_layout(ncol = 2)
```
## Adjustment 3: Adjust margins, fix axis text, drop legend
If your stub has a `theme()` segment, applying a new one will overrule it.
```{r}
baseplot3 <- baseplot3 +
### <b>
theme(legend.position = "none",
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.margin = margin(1,1,1,5, "cm"))
### </b>
```
## Drop Legend - Render
```{r, echo=FALSE, fig.width = 10}
baseplot3
```
## Adjustment 4: Prettify Y, reverse X direction
```{r}
baseplot3 <- baseplot3 +
### <b>
scale_y_continuous(labels = scales::comma) +
scale_x_reverse()
### </b>
```
## Reverse X - Render
Now we have the different "frames" all layered on top of each other.
```{r, echo = FALSE, fig.width = 10}
baseplot3
```
## Decision Point: How to Proceed? | Before continuing - are we SURE animation is our best option?
Revisit our goals:
1. Where do most refugees come from?
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?
Let's try static options one last time just to be sure.
## Faceting?
```{r, echo = FALSE, fig.width = 10}
baseplot3+facet_wrap(X_date_year~.)
```
No.
## Faceting?
```{r, echo = FALSE, fig.width = 10}
baseplot3+facet_grid(X_date_year~.)
```
Definitely not.
# 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>
```
## Basic Animation - Render
<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>
## Add Dynamic Labels
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 - Render
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## Transition Options: Exit/Enter
Solving Problem 3: how do we want the animation elements to move?
Option: shrink and grow on exit and enter
```{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 - Render
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)
```
## Transition Options: Easing
Solving Problem 3: how do we want the animation elements to move?
Option: Ease between positions (moving on page, not exiting or entering)
```{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>
```
## Easing Transitions - Render
Makes the transition speed change as it moves (functions may be cubic, quartic, etc)
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## Transition Options: More Energetic
Solving Problem 3: how do we want the animation elements to move?
Option: For fun, let's try "back" to see a springier approach
```{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('back-in-out')
### </b>
```
## Bouncy Transitions - Render
Feels a little cartoony- interesting, but again perhaps not what we need
```{r, fig.width = 10, fig.height=6, echo = FALSE, cache = TRUE}
animate(animp, fps = 10, duration = 20)
```
## Transition Options: Timing
Solving Problem 3: how do we want the animation elements to move?
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 - Render
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 - What Does It Do? {.smaller}
```{r, eval=FALSE}
transition_states(
X_date_year,
transition_length = 5,
state_length = c(rep(.25, 21), 20),
wrap = FALSE)+
```
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** determines whether to apply transition smoothing between the end and restarting
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 = 30, duration = 20, 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)