2 min read

Africas GDP over time

library(countrycode)
library(WDI)
library(magick)
library(tidyverse)
library(ggmap)
library(maps)
library(mapdata)
library(magrittr)
library(gganimate)
library(viridis)

Getting the contries in Africa

We also need to fix some of the names slightly

countries = countrycode::codelist_panel
africa_countries = countries %>% 
  filter(continent == 'Africa') %>% 
  select(country.name.en, iso2c) %>%
  unique.data.frame() %>%
  rename(Country_Name = country.name.en,
         Country_Code = iso2c) %>%
  mutate(Country_Name = if_else(Country_Name == "Congo - Brazzaville", 'Republic of the Congo', Country_Name),
         Country_Name = if_else(Country_Name == "Congo - Kinshasa",'Democratic Republic of the Congo', Country_Name),
         Country_Name = if_else(Country_Name == "Côte d’Ivoire",'Ivory Coast', Country_Name),
         Country_Name = if_else(Country_Name == "Réunion",'Reunion', Country_Name),
         Country_Name = if_else(Country_Name == "St. Helena",'Saint Helena', Country_Name),
         Country_Name = if_else(Country_Name ==  "São Tomé & Príncipe",'Sao Tome and Principe', Country_Name))

Pulling the GDP info

africa_gdp = WDI(indicator='NY.GDP.PCAP.KD', 
                 country=africa_countries$Country_Code, 
                 start=1940, 
                 end=2018) %>%
  rename(Country_Code = iso2c,
         GDP = NY.GDP.PCAP.KD) %>%
  select(Country_Code, GDP, year)

Let’s look at the GDP as a function of time

ggplot(africa_gdp, aes(year, GDP, color=Country_Code)) + geom_line() + 
    xlab('Year') + ylab('GDP per capita')

Lets get the map of africa.

Join it all together

africa = africa_map %>% left_join(africa_countries) %>% left_join(africa_gdp)
## Joining, by = "Country_Name"
## Joining, by = "Country_Code"

Putting it all together, a movie of africas GDP over the last 80 years.

p = africa %>%
 ggplot(aes(x = long, 
            y = lat, 
            group = group, 
            fill = log10(GDP), 
            frame = year)) +
  coord_fixed(1.3) +
  geom_polygon() + 
  theme_minimal() + 
  scale_fill_viridis()

gganimate(p, '../../../static/img/africa.gif')
## Executing: 
## convert -loop 0 -delay 100 Rplot1.png Rplot2.png Rplot3.png
##     Rplot4.png Rplot5.png Rplot6.png Rplot7.png Rplot8.png
##     Rplot9.png Rplot10.png Rplot11.png Rplot12.png Rplot13.png
##     Rplot14.png Rplot15.png Rplot16.png Rplot17.png Rplot18.png
##     Rplot19.png Rplot20.png Rplot21.png Rplot22.png Rplot23.png
##     Rplot24.png Rplot25.png Rplot26.png Rplot27.png Rplot28.png
##     Rplot29.png Rplot30.png Rplot31.png Rplot32.png Rplot33.png
##     Rplot34.png Rplot35.png Rplot36.png Rplot37.png Rplot38.png
##     Rplot39.png Rplot40.png Rplot41.png Rplot42.png Rplot43.png
##     Rplot44.png Rplot45.png Rplot46.png Rplot47.png Rplot48.png
##     Rplot49.png Rplot50.png Rplot51.png Rplot52.png Rplot53.png
##     Rplot54.png Rplot55.png Rplot56.png Rplot57.png Rplot58.png
##     'africa.gif'
## Output at: africa.gif
# for(year in unique(sort(africa$year))){
# p = africa %>% filter(year == year) %>%
#  ggplot(aes(x = long, y = lat, group = group, fill = log10(GDP))) +
#   coord_fixed(1.3) +
#   geom_polygon() + 
#   theme_minimal() + 
#   scale_fill_viridis()
#   print(p)
# }
Image

Image