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
/
example_heart_pumping.Rmd
77 lines (65 loc) · 2.37 KB
/
example_heart_pumping.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
---
title: "Heart Pumping"
author: "Emi Tanaka"
date: "22/11/2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Note that you will need to install `transformr` from thomasp85 or else you will run into the following error:
> Error in transform_path(all_frames, next_state, ease, params$transition_length[i], :
transformr is required to tween paths and lines
For more info, see this relevant github [issue](https://github.com/thomasp85/gganimate/issues/190).
```{r install pkgs, eval=FALSE}
## install from Github
devtools::install_github("jespermaag/gganatogram")
devtools::install_github("thomasp85/transformr") # to avoid error in in transition_states demonstration
```
```{r, message=FALSE}
library(gganimate)
library(gganatogram)
library(dplyr)
body <- hgMale_list$human_male_outline %>%
mutate(index=1:nrow(.)) %>%
group_by(group) %>%
summarise(x=NA, y=NA, index=0) %>%
bind_rows(., hgMale_list$human_male_outline) %>%
arrange(group, index) %>%
select(x, y)
heart1 <- heart2 <- hgMale_list$heart[-1,]
heart2 <- heart2 %>%
mutate(x=1.3*(x - mean(x)) + mean(x),
y=1.3*(y - mean(y)) + mean(y),
state=2)
heart1 <- heart1 %>%
mutate(x=0.8*(x - mean(x)) + mean(x),
y=0.8*(y - mean(y)) + mean(y),
state=1)
heart <- rbind(heart1, heart2) %>% select(x, y, state)
```
```{r transition_states, eval=TRUE, warning=FALSE, message=FALSE}
ga <- ggplot(data=body, aes(x, -y)) +
geom_path() +
geom_polygon(fill="yellow", colour="black") +
geom_path(data=heart, aes(x, -y)) +
geom_polygon(data=heart, aes(x, -y), fill="red") +
theme_void() +
ease_aes('quadratic-in-out') +
transition_states(state,
transition_length=3,
state_length=1)
animate(ga, nframes=10, width=350)
```
`transition_manual` is another alternative that achieves a similar animation. I also learnt that setting the nframes argument of the `animate` function to a small number makes the heart beat faster than a larger number.
```{r transition_manual, warning=FALSE, message=FALSE}
ga <- ggplot(data=body, aes(x, -y)) +
geom_path() +
geom_polygon(fill="yellow", colour="black") +
geom_path(data=heart, aes(x, -y)) +
geom_polygon(data=heart, aes(x, -y), fill="red") +
theme_void() +
ease_aes('quadratic-in-out') +
transition_manual(state)
animate(ga, nframes=5, width=350)
```