This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
transition_components.Rmd
156 lines (112 loc) · 5.51 KB
/
transition_components.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
---
title: "transition_components"
author: "Anna Quaglieri"
date: "22/11/2018"
output:
github_document:
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,message = FALSE,cache=TRUE)
```
```{r setup1, eval=FALSE}
devtools::install_github("hadley/emo")
devtools::install_github("ropenscilabs/icon")
```
```{r}
library(gganimate)
library(tidyverse)
library(fivethirtyeight)
```
## How does `transition_components()` work?
> ## Transition individual components through their own lifecycle
>
> This transition allows individual visual components to define their own "life-cycle". This means that the final animation will not have any common "state" and "transition" phase as any component can be moving or static at any point in time.
> ## Usage
> transition_components(time, range = NULL, enter_length = NULL,
exit_length = NULL)
Some key points to keep in mind:
In `transition_components()` you need at least a **time** component and a variable id in your dataset that groups together observations. The `transition_components()` function is useful when you have the same identifier (like a plane, a day, a person, a neighborood etc.) with multiple observations over time.
## A minimal example
The example below is the one provided in the help page `?transition_components` and I added a few simple variants.
* In the standard situation you would use the `group` argument in the `ggplot()` call to define which are the observations with the same IDs.
```{r}
data <- data.frame(
x = runif(10),
y = runif(10),
size = sample(1:3, 10, TRUE),
time = c(1, 4, 6, 7, 9, 6, 7, 8, 9, 10),
id = rep(1:2, each = 5)
)
anim <- ggplot(data, aes(x, y, size = size, group = id)) +
geom_point() +
transition_components(time)
```
* You can also achieve the same transition but with different colours per ID by using the `colour` argument, which is a more special way of grouping observations.
```{r}
anim <- ggplot(data, aes(x, y, size = size,colour=as.factor(id))) +
geom_point() +
transition_components(time)
```
* Notice what happens if you do not set any of the above (`group` or `colour`). You can see that the transition lost the knowledge about what observations belong to the same ID.
```{r}
anim <- ggplot(data, aes(x, y, size = size)) +
geom_point() +
transition_components(time)
```
## US births 1994-2003
To show another example, I will use the `US_births_1994_2003` dataset from the `fivethirtyeight` package. The title of the article where this data was used is *Some People Are Too Superstitious To Have A Baby On Friday The 13th*. Is that true?
```{r}
head(US_births_1994_2003)
```
In the example below my **grouping variable** will be Fridays of the month, stored into the variable `date_of_month` after I only filter for Fridays. For example, one id is **1**, which is the 1st Friday of a generic month across years. My plan is to compare the number of babies born on the Fridays 13th of different months (**13**) with babies born on other Fridays! To speed up the process I will compare Fridays 13th with Fridays that occurs on the 1st, 2nd, 3rd, 18th and 28th across months and years.
```{r eval=TRUE}
# Select only Fridays
fridays <- US_births_1994_2003 %>%
filter(day_of_week %in% c("Fri") & date_of_month %in% c(1,2,3,13,18,28))
head(fridays)
```
* Grouping using colouring allows me to compare different dates of Fridays
```{r eval=TRUE}
p=ggplot(fridays) +
geom_point(aes(x=year,y=births,colour=factor(date_of_month))) +
theme(legend.position = "bottom") +
transition_components(time=date)+
shadow_trail(distance = 0.01, size = 0.3)
animate(p, nframes = 20, 10,duration=10)
```
It looks like between 1994 and 1995 there were consistently less babies born on Fridays 13th, but then this fashion went away. Was that just by chance?!
## Example with babynames
The `babynames` packages is one of the great packages developed thanks to the effort of another #OzUnconf18 team https://github.com/ropenscilabs/ozbabynames!
Below I am showing another example that uses `transition_components()` in combination with other fun animations like:
* `shadow_trail()` which allows you to customise the way in which your observations leave a trace of themselves once they move on with their transitions.
* Within `shadow_trail()`, the argument `distance` lets you specify the distance between each trace left. I noticed that it does not work with a very small distance (0.001 wasn't working). It has something to do with the fact that `distance` is used as a denominator at some steps and probably it gets too small.
* The argument `size` works like in the normal `ggplot()` (e.g. size of dots) and it specifies the size of trace left.
```{r}
# install_github("ropenscilabs/ozbabynames")
library(ozbabynames)
p=ggplot(ozbabynames[ozbabynames$name %in% c("Michael","James"),]) +
geom_point(aes(x=year,y=count,colour=name)) +
theme_bw() +
transition_components(time=year)+
shadow_trail(distance = 0.1, size = 2)
p
```
* Let's increase the `distance`
```{r}
p=ggplot(ozbabynames[ozbabynames$name %in% c("Michael","James"),]) +
geom_point(aes(x=year,y=count,colour=name)) +
transition_components(time=year)+
shadow_trail(distance = 2, size = 2)
p
```
* `distance` too small. The code below will throw the error:
> Error in seq.default(1, params$nframes, by = params$distance) :
invalid '(to - from)/by'
```{r eval=FALSE}
p=ggplot(ozbabynames[ozbabynames$name %in% c("Michael","James"),]) +
geom_point(aes(x=year,y=count,colour=name)) +
transition_components(time=year)+
shadow_trail(distance = 0.001, size = 2)
p
```