forked from jennybc/ggplot2-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapminder-gganimate.rmd
61 lines (46 loc) · 1.6 KB
/
gapminder-gganimate.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
---
title: "Animate the Gapminder data set using gganimate"
author: "Shaun Jackman"
date: "2016-04-19"
---
Inspired by Hans Rosling's TED Talk:
[The best stats you've ever seen](https://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen).
The code is taken nearly verbatim from the [gganimate example](https://github.com/dgrtwo/gganimate#gganimate-create-easy-animations-with-ggplot2).
# Install packages
```{r install-packages, eval=FALSE}
install.packages(c("devtools", "dplyr", "ggplot2", "readr"))
library(devtools)
devtools::install_github("dgrtwo/gganimate")
```
Note that [gganimate](https://github.com/dgrtwo/gganimate) requires [ImageMagick](http://www.imagemagick.org/script/index.php). To install it, use [Homebrew](http://brew.sh) on a Mac and [Linuxbrew](http://linuxbrew.sh) or `apt-get` or `yum` on Linux:
```sh
brew install imagemagick
sudo apt-get install imagemagick
sudo yum install imagemagick
```
# Load packages
```{r load-packages, message=FALSE}
library(dplyr)
library(gganimate)
library(ggplot2)
library(readr)
```
# Read the Gapminder data
```{r read-gapminder-data}
gapminder <- read_tsv("gapminderDataFiveYear.tsv")
glimpse(gapminder)
```
# Plot the Gapminder data
```{r plot-gapminder-data}
gapminder_plot <- ggplot(gapminder) +
aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop,
frame = year) +
geom_point(alpha = 0.4) +
scale_x_log10()
gapminder_plot
```
# Animate the Gapminder data
```{r animate-gapminder-data, message=FALSE}
gg_animate(gapminder_plot, filename = "gapminder-gganimate.gif")
```
![Animated Gapminder figure](gapminder-gganimate.gif)