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
/
view_follow.Rmd
69 lines (49 loc) · 1.77 KB
/
view_follow.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
---
title: "view_follow"
author: "Danielle Navarro"
date: "22/11/2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gapminder)
library(gganimate)
```
## Creating the animation
Based on the `gapminder` animation from the `gganimate` GitHub page, let's plot GDP against life expectancy for the African nations and animate them by year:
```{r, initialanimation, cache=TRUE}
pic <- gapminder %>% filter(continent == "Africa") %>%
ggplot(aes(
x = log10(gdpPercap), y = 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))
anim <- pic +
labs(
title = 'Year: {frame_time}',
x = 'GDP Per Capita (log10)',
y = 'Life Expectancy') +
transition_time(year) +
ease_aes('linear')
anim %>% animate(type = "cairo")
```
This shows a general pattern of GDP and life expectancy both rising.
## Allowing the view to follow the data
In some instances we may not want to fix the axes, and instead have the plot zoom in and out based on the natural scale of the data at each frame. The `view_follow()` function allows this:
```{r, viewfollow1, cache = TRUE}
anim2 <- anim + view_follow()
anim2 %>% animate(type = "cairo")
```
The `fixed_x` and `fixed_y` arguments allow us to keep one of the axes fixed while the other one zooms in and out:
```{r, viewfollow2, cache = TRUE}
anim3 <- anim + view_follow(fixed_x = TRUE)
anim3 %>% animate(type = "cairo")
```
If the plot includes shadows, the view is calculated in a way that includes the shadows:
```{r, viewfollow3, cache = TRUE}
anim4 <- anim + view_follow() + shadow_wake(.1, wrap = FALSE)
anim4 %>% animate(type = "cairo", detail= 5)
```