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
/
ease_aes.Rmd
198 lines (143 loc) · 4.87 KB
/
ease_aes.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
---
title: "ease_aes - a walkthrough"
author: "Sarah Romanes"
date: "22 November 2018"
output: github_document
editor_options:
chunk_output_type: console
---
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(gganimate)
library(tidyverse)
library(gridExtra)
```
# What does `ease_aes()` do?
> ease_aes() defines how different aesthetics should be eased during transitions.
I like to think of ease describing the *acceleration* of our transition. Should it have a constant speed? Should it speed up or slow down? Or should it bounce around? We can control this using this component of `gganimate`.
There are **two** components to the `ease_aes` function, which are drawn from the `tweenr` package. They are:
* The name of the easing function to display: `ease`
* And the modifier for said `ease` function, in the form of `-in`, `-out`, or `-in-out`.
Each `ease_aes()` command requires the ease function AND the modifier, *except* for the special case of the `linear function`.
## The `ease` function
The ease function controls the *shape* of the acceleration. For example, `linear` will produce a constant speed for the transition, whereas `bounce` will provide a bouncing effect!
A list of the easing functions can be found [here](https://www.rdocumentation.org/packages/tweenr/versions/0.1.5/topics/display_ease), and they can easily be visualised with the use of the `tweenr` package as follows:
```{r}
par(mfrow=c(1,2))
tweenr::display_ease('linear')
tweenr::display_ease('bounce-in')
```
**Constant rate of change**
```{r}
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 3,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('linear')
```
**Bouncing**
```{r}
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 3,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('bounce-out')
```
# The affect of the modifiers
## Motivation
For the `linear` ease function, it is clear the rate of change is constant - so there is only one possible linear combination:
```{r}
tweenr::display_ease('linear')
```
However, for the `quadratic` function, we have two options:
```{r}
par(mfrow=c(1,2))
tweenr::display_ease('quadratic-in')
tweenr::display_ease('quadratic-out')
```
It is clear that the role of the `-in` and `-out` modifiers is to change which *reflection* on the line *y=x* our function is on. This has the affect of changing concavity, or more pratically, the type of acceleration. In the `quadratic-in` example, our transition state is *speeding up* as time progresses. However, in `quadratic-out`, our transition state is *slowing down* as time progresses.
**Speeding up**
```{r}
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 3,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('quadratic-in')
```
**Slowing down**
```{r}
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 3,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('quadratic-out')
```
### The `-in-out` modifier
The `in-out` modifier essentially appends the two accelerations together, speeding up and then slowing down. We can visualise this with the quadratic function as follows:
```{r}
tweenr::display_ease('quadratic-in-out')
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 3,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('quadratic-in-out')
```
# A more interesting example
We can use the `ggimage` package to add in the image of a car and visualise it transitioning from state (one side of the screen to the other) and changing the way it gets there using `ease_aes()`.
We first set up our static background:
```{r}
library(ggimage)
ggdat <- expand.grid(x=0.75 * c(0:14),
y=3* c(0:10),
fill=NA)
fill <- rep(NA, nrow(ggdat))
for(i in 1:nrow(ggdat)){
if(ggdat$y[i]<=12){
fill[i] <- "#009900"
}
else if(ggdat$y[i]==15){
fill[i] <- "#72777f"
} else
{fill[i] <- "#4e86e0"}
}
ggdat$fill <- fill
ga <- ggplot(ggdat, aes(x, y)) +
geom_tile(aes(fill=I(fill)), height=6, width=0.75, colour="white", size=1.2) +theme_void()
ga
```
And now we add a `bounce` function with `-in-out` modifier - creating the feeling of struggling to parallel park!
```{r}
dta <- data.frame(y = c(15, 15),
x = c(0, 10.5),
tstep = c("a" , "b"),
image = rep("images/car.png",2))
ga1 <- ga +geom_image(data=dta, aes(x,y, image=image), size=0.2)
ga1 + transition_states(tstep,
transition_length=1,
state_length = 1) +
ease_aes('bounce-in-out')
```