Background

This is the first of the series, “Fun with Animations in R”. The inspiration for these visualizations have been taken from https://ourworldindata.org/technology-adoption and Hans Rosling’s remarkable Gapminder Viz.

Data Source: Our World In data website

Category: Mobile Phone Subscriptions vs GDP

Let’s dive in!

Here’s the data!

data = read.table(
  'mobile-phone-subscriptions-vs-gdp-per-capita.csv',
  sep = ',',
  header = T
)
data <- na.omit(data)
head(data)
##         Entity Code Year Mobile_subs GDP_per_capita
## 57 Afghanistan  AFG 2002   0.1137402       1063.636
## 58 Afghanistan  AFG 2003   0.8671203       1099.195
## 59 Afghanistan  AFG 2004   2.4876675       1062.249
## 60 Afghanistan  AFG 2005   4.7864452       1136.123
## 61 Afghanistan  AFG 2006   9.7336044       1161.125
## 62 Afghanistan  AFG 2007  17.5381616       1284.775
##    Total.population..Gapminder.
## 57                     24639841
## 58                     25678639
## 59                     26693486
## 60                     27614718
## 61                     28420974
## 62                     29145841

Animation 1: Racing bar chart on Top 10 GDP per capita vs Year

#Since data is large, we filter countries based on top 10 criterion on GDP
gdp <- data %>%
  group_by(Year) %>%
  mutate(rank = min_rank(desc(GDP_per_capita)),GDP_label = paste0(" ", round(GDP_per_capita))) %>% 
  filter(rank <= 10) %>%
  ungroup()
gdp
## # A tibble: 240 x 8
##    Entity Code   Year Mobile_subs GDP_per_capita Total.populatio~  rank
##    <fct>  <fct> <int>       <dbl>          <dbl>            <int> <int>
##  1 Bahra~ BHR    1990        1.04         35113.           492891    10
##  2 Bahra~ BHR    1991        1.44         37997.           506685     9
##  3 Bahra~ BHR    1992        1.85         39506.           519696     9
##  4 Bahra~ BHR    1993        2.12         43499.           532362     8
##  5 Bahra~ BHR    1994        3.21         42334.           545329     9
##  6 Bahra~ BHR    1995        4.90         42897.           559069    10
##  7 Bahra~ BHR    1996        6.93         43505.           574914    10
##  8 Bahra~ BHR    1998       15.0          44315.           611237    10
##  9 Bermu~ BMU    1990        1.83         40553.            59795     7
## 10 Bermu~ BMU    1991        2.35         39322.            60150     8
## # ... with 230 more rows, and 1 more variable: GDP_label <chr>
p <-
  ggplot(gdp,
         aes(
           rank,
           group = Entity,
           fill = Entity,
           color = Entity
         )) +
  geom_tile(
    aes(
      y = GDP_per_capita / 2,
      height = GDP_per_capita,
      width = 0.7
    ),
    alpha = 0.8,
    color = NA,
    size = 4
  ) +
  geom_text(
    aes(y = 0, label = paste(Entity, " ")),
    vjust = 0.2,
    hjust = 1,
    size = 6
  ) +
  geom_text(aes(y = GDP_per_capita, label = GDP_label, hjust = 0)) +
  coord_flip(clip = 'off', expand = F) +
  scale_x_reverse() +
  guides(color = F) +
  labs(
    title = "{closest_state} GDP per capita by Country",
    x = NULL,
    y = NULL,
    caption = "Sources: Open Data World"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(
      hjust = 0.5,
      size = 28,
      colour = "blue"
    ),
    plot.caption = element_text(size = 10, colour = "#000000"),
    # Remove grid lines and axes ticks
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y  = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_blank(),
    plot.margin = margin(1, 1, 1, 4, "cm")
  ) +
  # Turn off legend
  theme(legend.position = "none") + 
  #Set transition
  transition_states(Year, transition_length = 4, state_length = 1) +
  ease_aes('cubic-in-out')


animate(p, fps = 20, duration = 20, width = 800,height = 600)

anim_save("GDP.gif", p)

Animation 2: Bubble Chart inspired from Hans Rosling Gapminder Ted Talk

# Make a ggplot, but add frame=year: one image per year
q <-
  ggplot(
    data,
    aes(
      GDP_per_capita,
      Mobile_subs,
      size = Total.population..Gapminder.,
      color = Entity,
      frame = Year,
      ids = Entity
    )
  ) +
  geom_point() +
  scale_x_log10() +
  theme_bw() +
  labs(x = 'GDP per capita', y = 'Mobile Subscriptions per capita')

#animate(q, fps = 10, duration = 10, width = 800,height = 600)

figure <-
  ggplotly(q) %>% animation_opts(500, easing = "cubic-in-out", redraw = FALSE) %>%  animation_button(
    x = 1,
    xanchor = "right",
    y = 0,
    yanchor = "bottom"
  ) %>%
  animation_slider(currentvalue = list(prefix = "YEAR ", font = list(color =
                                                                       "blue")))

figure

How about trend chart?

Let’s do it!

r <- ggplot(gdp,
            aes(factor(Year), Mobile_subs, group = Entity, color = Entity)) +
  geom_line(size = 1) +
  scale_x_discrete(position = "top") +
  scale_y_continuous() +
  labs(x = NULL, y = "Mobile Subscriptions per GDP", title = 'Rise of Mobile Subscriptions in Top 10 Countries (w.r.t GDP)') +
  theme(legend.position = "none") +
  theme_minimal() +
  theme(plot.title = element_text(
    hjust = 0.5,
    size = 24,
    colour = "blue"
  )) +
  geom_point(color = 'black', size = 2) +
  transition_reveal(Year) +
  coord_cartesian(clip = 'off') +
  ease_aes('cubic-in-out')


animate(r, fps = 20, nframes = 200, width = 900, height = 600, end_pause = 20)

That’s it for now. See ya!