Your browser does not support the video tag.
In this short blog post I show you how you can use the {gganimate} package to create animations
from {ggplot2} graphs with data from UNU-WIDER.
WIID data
Just before Christmas, UNU-WIDER released a new edition of their World Income Inequality Database:
*NEW #DATA*We’ve just released a new version of the World Income Inequality Database.WIID4 includes #data from 7 new countries, now totalling 189, and reaches the year 2017. All data is freely available for download on our website: https://t.co/XFxuLvyKTC pic.twitter.com/rCf9eXN8D5— UNU-WIDER (@UNUWIDER) December 21, 2018
The data is available in Excel and STATA formats, and I thought it was a great opportunity to
release it as an R package. You can install it with:
devtools::install_github(&quot;b-rodrigues/wiid4&quot;)
Here a short description of the data, taken from UNU-WIDER’s website:
&quot;The World Income Inequality Database (WIID) presents information on income inequality for
developed, developing, and transition countries. It provides the most comprehensive set of income
inequality statistics available and can be downloaded for free.
WIID4, released in December 2018, covers 189 countries (including historical entities), with over
11,000 data points in total. With the current version, the latest observations now reach the year 2017.&quot;
It was also a good opportunity to play around with the {gganimate} package. This package
makes it possible to create animations and is an extension to {ggplot2}. Read more about it
here.
Preparing the data
To create a smooth animation, I need to have a cylindrical panel data set; meaning that for each
country in the data set, there are no missing years. I also chose to focus on certain variables
only; net income, all the population of the country (instead of just focusing on the economically
active for instance) as well as all the country itself (and not just the rural areas).
On this link you
can find a codebook (pdf warning), so you can understand the filters I defined below better.
Let’s first load the packages, data and perform the necessary transformations:
library(wiid4)
library(tidyverse)
library(ggrepel)
library(gganimate)
library(brotools)
small_wiid4 %
mutate(eu = as.character(eu)) %__%
mutate(eu = case_when(eu == &quot;1&quot; ~ &quot;EU member state&quot;,
eu == &quot;0&quot; ~ &quot;Non-EU member state&quot;)) %__%
filter(resource == 1, popcovr == 1, areacovr == 1, scale == 2) %__%
group_by(country) %__%
group_by(country, year) %__%
filter(quality_score == max(quality_score)) %__%
filter(source == min(source)) %__%
filter(!is.na(bottom5)) %__%
group_by(country) %__%
mutate(flag = ifelse(all(seq(2004, 2016) %in% year), 1, 0)) %__%
filter(flag == 1, year __ 2003) %__%
mutate(year = lubridate::ymd(paste0(year, &quot;-01-01&quot;)))
For some country and some years, there are several sources of data with varying quality. I only
keep the highest quality sources with:
group_by(country, year) %__%
filter(quality_score == max(quality_score)) %__%
If there are different sources of equal quality, I give priority to the sources that are the most
comparable across country (Luxembourg Income Study, LIS data) to less comparable sources with
(at least that’s my understanding of the source variable):
filter(source == min(source)) %__%
I then remove missing data with:
filter(!is.na(bottom5)) %__%
bottom5 and top5 give the share of income that is controlled by the bottom 5% and top 5%
respectively. These are the variables that I want to plot.
Finally I keep the years 2004 to 2016, without any interruption with the following line:
mutate(flag = ifelse(all(seq(2004, 2016) %in% year), 1, 0)) %__%
filter(flag == 1, year __ 2003) %__%
ifelse(all(seq(2004, 2016) %in% year), 1, 0)) creates a flag that equals 1 only if the years
2004 to 2016 are present in the data without any interruption. Then I only keep the data from 2004
on and only where the flag variable equals 1.
In the end, I ended up only with European countries. It would have been interesting to have countries
from other continents, but apparently only European countries provide data in an annual basis.
Creating the animation
To create the animation I first started by creating a static ggplot showing what I wanted;
a scatter plot of the income by bottom and top 5%. The size of the bubbles should be proportional
to the GDP of the country (another variable provided in the data). Once the plot looked how I wanted
I added the lines that are specific to {gganimate}:
labs(title = 'Year: {frame_time}', x = 'Top 5', y = 'Bottom 5') +
transition_time(year) +
ease_aes('linear')
I took this from {gganimate}’s README.
animation