This week was important to me, because I have started a new Master’s degree in Ecological Modelling. I will do a lot stats, modelling and data visualisation so it was important to me to pay a tribute to someone that really inspired me: Hans Roseling.
A quick recap onto his biography: Hans Roseling was a Swedish MD and more important to me, a statistician (in fact he was an epidemiologist, but they are doing a lot of stats) and a sword swallower. He started a foundation which aims to help people to a better understanding of statistics.
One day during my BSc, I have found one his Ted talk titled: The best stats you’ve ever seen. This talk changed the way that I am looking at the world. It has made me more optimistic about future of our species, even if we have a tremendous number of challenges to face off. If you have not watched one of his talks, stop everything and watch it!
His conference totaly blowed my mind. If you liked it too, you should watch one of his documentary: Don’t Panic - The Truth about Population.
Since this, I know that I want to use stats to better understand our world. Sure, I still passionate with marine sciences. That’s why I want to use statistical methods to get insight into how the marine communities work.
Thus, I am starting, a new Master’s degree. That’s why, I would like to pay a tribute to Hans Roseling. What a better way than using some of his data from Gapminder and making cool animated graphics?
o let’s go! I have already tidy a new dataset based on the brilliant Jennifer Bryan gapminder R package. It will be the occasion to show off some gganimate
features!
First of all, I load some convinient libraries and my own version of gapminder
dataset.
library(tidyverse)
library(gganimate)
theme_set(theme_bw())
load("gapminder_data.RData")
glimpse(gapminder)
## Observations: 39,928
## Variables: 8
## $ country <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Afghani…
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia,…
## $ year <dbl> 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809,…
## $ fertility <dbl> 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,…
## $ childMort <dbl> 469, 469, 469, 469, 469, 469, 470, 470, 470, 470, 470, 470,…
## $ lifeExp <dbl> 28.2, 28.2, 28.2, 28.2, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1,…
## $ pop <dbl> 3280000, 3280000, 3280000, 3280000, 3280000, 3280000, 32800…
## $ gdpPercap <dbl> 603, 603, 603, 603, 603, 603, 603, 603, 603, 603, 604, 604,…
This new dataset has two new variables:
- childMort
the number of deaths of children under 5 years per 1,000 births;
- fertility
the number of children per woman.
Let’s do some animated plots! First, I want to plot one of the most famous animated plots, let’s start with life expectancy against the income.
p_1 <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_color_manual(name = "Continent", values = gapminder::continent_colors) +
scale_x_log10() +
scale_size(name = "Population") +
labs(x = "log10($ GDP per capita)",
y = "Life expectancy",
title = paste("Life expectancy ~ log10($ GPD per capita) - Year:", "{round(frame_time, 0)}")) +
transition_time(year)
animate(p_1, fps = 5)
For animated the plot, I just use the gganimate::transition_time()
which to create a frame for each value of the gapminder$year
variable. Moreover, this function grant me access to a new varibale frame_time
which allow me to know which value of gapminder$year
is currently print. I use this new variable in the ggplot2::title()
function. Then, I use gganimate::animate()
which use as first argument a gganim
object and I can set the number of frame per second to animate the whole.
Now, what’s about the the fertility against the income?
p_2 <- ggplot(gapminder, aes(x = gdpPercap, y = fertility)) +
geom_point(aes(color = continent, size = pop)) +
scale_color_manual(name = "Continent", values = gapminder::continent_colors) +
scale_size(name = "Population") +(name = "Population") +
scale_x_log10() +
labs(x = "log10($ GDP per capita)",
y = "# Children per woman",
title = paste("Fertily ~ log10($ GDP per capita) - Year:", "{round(frame_time, 0)}")) +
transition_time(year)
animate(p_1, fps = 5)
Fantastic! The fertility keep decreasing and decreasing! Are we still expecting the Population Bomb? Now let’s look at the child mortality as function of GDP.
p_3 <- ggplot(gapminder, aes(x = gdpPercap, y = childMort)) +
geom_point(aes(color = continent, size = pop)) +
scale_color_manual(name = "Continent", values = gapminder::continent_colors) +
scale_size(name = "Population") +
scale_x_log10() +
scale_y_log10() +
labs(x = "log10($ GDP per capita)",
y = "log10(Child mortality)",
title = paste("log10(Child mortality) ~ log10($ GDP per capita) - Year:", "{round(frame_time, 0)}")) +
transition_time(year)
animate(p_3, fps = 5)
More and more plots are possible. With gganimate
, few lines are enough to animate data and thanks Hans Roseling too!