Motivating Example: Gapminder

Fertility vs Life Expectancy

In this demo we are going to learn about dynamic graphics by using the Gapminder data on Fertility. These data are discussed in the Hans Rosling TED Talk (start at 2:30) https://youtu.be/hVimVzgtD6w?t=149.

For more detail on how the data were wrangled please look at the article “How to build Animated Charts like Hans Rosling — doing it all in R” by Tristan Ganry https://towardsdatascience.com/how-to-build-animated-charts-like-hans-rosling-doing-it-all-in-r-570efc6ba382.

1) Start Simple: ggplot

library(tidyverse)

fert<-read_csv("https://raw.githubusercontent.com/kitadasmalley/FA2020_DataViz/main/data/gapminderFert.csv")

head(fert)
## # A tibble: 6 x 6
##   Country      year  life  fert   pop continent
##   <chr>       <dbl> <dbl> <dbl> <dbl> <chr>    
## 1 Afghanistan  1962  33.0  7.67   9.3 Asia     
## 2 Afghanistan  1963  33.5  7.67   9.5 Asia     
## 3 Afghanistan  1964  34.1  7.67   9.7 Asia     
## 4 Afghanistan  1965  34.6  7.67   9.9 Asia     
## 5 Afghanistan  1966  35.1  7.67  10.1 Asia     
## 6 Afghanistan  1967  35.7  7.67  10.4 Asia
p<- fert%>%
  filter(year == 2015)%>%
  ggplot(aes(fert, life, size = pop, color = continent)) +
  labs(x="Fertility Rate", y = "Life expectancy at birth (years)", 
       caption = "(Based on data from Hans Rosling - gapminder.com)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) +
  geom_point() 

p

2) Add some interaction: plotly

The plotly package can be used to created interactive graphics. Although it is an entirely different paradigm of programming graphics, it does have a very nice function, ggplotly that can be added on to ggplot objects.

Use the tooltip to learn more about each point

#install.packages("plotly")
library(plotly)

p<- fert%>%
  ggplot(aes(x=fert, y=life, size = pop, color = continent,frame = year)) +
  labs(x="Fertility Rate", y = "Life expectancy at birth (years)", 
       caption = "(Based on data from Hans Rosling - gapminder.com)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) +
  geom_point(aes(text=Country))

ggplotly(p)

3) Now let’s animate! gganimate

#install.packages("gganimate")
library(gganimate)
#install.packages("gifski")
library(gifski)

p1 <- ggplot(fert, aes(fert, life, size = pop, color = continent, frame = year)) +
  labs(x="Fertility Rate", y = "Life expectancy at birth (years)", 
       caption = "(Based on data from Hans Rosling - gapminder.com)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) +
  geom_point() +
  #scale_color_brewer(type = 'div', palette = 'Spectral') + 
  # gganimate code
  ggtitle("Year: {frame_time}") +
  transition_time(year) +
  ease_aes("linear") +
  enter_fade() +
  exit_fade()

animate(p1,fps = 4, width = 600, height = 400, renderer = gifski_renderer())

anim_save("output.gif")

Notice that this animation is a little choppy, because there is one observation every year. You can use the tweener package to interpolate and make smoother transitions https://www.r-bloggers.com/2017/05/create-smooth-animations-in-r-with-the-tweenr-package/.