Purpose

Creating an animated plot of mobile phone adoption in Africa.

Data source: Our World in Data - via TidyTuesday project from the R for Data Science foundation.

Reading in data

mobile <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-10/mobile.csv')
## 
## -- Column specification --------------------------------------------------------
## cols(
##   entity = col_character(),
##   code = col_character(),
##   year = col_double(),
##   total_pop = col_double(),
##   gdp_per_cap = col_double(),
##   mobile_subs = col_double(),
##   continent = col_character()
## )
landline <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-10/landline.csv')
## 
## -- Column specification --------------------------------------------------------
## cols(
##   entity = col_character(),
##   code = col_character(),
##   year = col_double(),
##   total_pop = col_double(),
##   gdp_per_cap = col_double(),
##   landline_subs = col_double(),
##   continent = col_character()
## )

Data prep and joining two datasets together

mobile <- mobile %>% 
  rename(subscriptions = mobile_subs) %>% 
  mutate(type = "Mobile")

landline <- landline %>% 
  rename(subscriptions = landline_subs) %>% 
  mutate(type = "Landline")

# bind landline and mobile data together
df <- bind_rows(mobile, landline)

Exploratory plot

# how have mean subscriptions increased over time?
df %>% 
  group_by(year, continent) %>% 
  summarise(mean_subs = mean(subscriptions, na.rm = T)) %>% 
  ungroup() %>% 
  ggplot(aes(year, mean_subs, group = continent, colour = continent)) +
  geom_line() +
  labs(title = "How have phone subscriptions increased over time?",
       x = "Year",
       y = "Subscriptions per 100 people",
       colour = "Continent")
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
## Warning: Removed 10 row(s) containing missing values (geom_path).

Arranging data for animated plot of African countries

# select african countries
ranked_by_year <- df %>% 
  filter(type == "Mobile",
         continent == "Africa")
  
# fill in missing values downwards - so that the missing value takes the previous year's value
ranked_by_year <- ranked_by_year %>% arrange(entity, year) %>% 
  fill(total_pop) %>% 
  fill(gdp_per_cap) %>% 
  fill(subscriptions) %>% 
  group_by(year) %>% 
  arrange(year, - subscriptions) %>% 
  mutate(rank = 1:n()) %>%
  ungroup() %>% 
  # filter out coutnries that only appear in the series later
  filter(!entity %in% c("South Sudan", "Mayotte", "Reunion", "Western Sahara"))

Static bubble chart

library(gganimate)
options(gganimate.nframes = 375)

bubble <- ranked_by_year %>% 
  mutate(total_pop = total_pop / 100000) %>% 
  drop_na() %>% 
  ggplot(aes(gdp_per_cap, subscriptions, label = entity, colour = entity)) +
  # geom_smooth(aes(group = 1), colour = "Darkred", alpha = .3) +
  # stat_smooth(se = FALSE, size = 0.5, color = "black", linetype="dotted") +
  geom_point(aes(size = total_pop), alpha = .7) +
  geom_text(cex = 4, check_overlap = T, colour = "black") +
  facet_wrap(~ year) +
  scale_x_continuous(labels = scales::dollar_format()) +
  scale_y_continuous(labels = scales::comma_format()) +
  scale_color_viridis_d(option = "A") +
  scale_size(range = c(.5, 24), name= "Population (M)") +
  theme(legend.position = "none") +
  facet_null() 

# display all in one panel
bubble

Animating chart with gganimate

# bub_anim <- bubble +  
#   ease_aes('linear') +
#   transition_states(year, state_length = c(rep(.4, 27), 4.2), wrap = F) +
#   labs(title = "Evolution of GDP and Mobile Subscriptions in Africa. \nYear: {as.integer(closest_state)}",
#        subtitle = "1990-2017",
#        x = "GDP per capita (in constant US$)",
#        y = "Mobile subscriptions per 100 people",
#        caption = "Note: Bubble size represents population\n Source: Our World in Data\nGraphic: Jonathan Jayes")
# 
# # display chart
# bub_anim

Exporting animated chart

# a <- animate(bub_anim, renderer = ffmpeg_renderer(),  width = 800, height = 450, duration = 15)
# 
# anim_save(a, filename = "data/animation_v8.mp4", path = "C:/Users/User/Documents/Recon/Tidy Tuesday")