Skip to content

Instantly share code, notes, and snippets.

@dreidpath
Created July 12, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreidpath/1793ac50cc4bbdca44d8a3bf480fa5cf to your computer and use it in GitHub Desktop.
Save dreidpath/1793ac50cc4bbdca44d8a3bf480fa5cf to your computer and use it in GitHub Desktop.
Quick time series plot of World Bank Life Expectancy and Infant Mortality Rate data downloaded using the wbstats package
# This is a quick script to create two time series plots using World Bank data downloaded using the wbstats package
# and plotted using ggplot2.
# The first plot uses Malaysia data only, and is a time series plot showing Life Expectancy at Birth changes
# in the whole population and for males and females separately.
# The second plot uses data for a handful of Southeast Asian countries and Australia and shows changes in the
# Infant Mortality Rate over time.
library(wbstats)
library(tidyverse)
library(dplyr)
library(ggplot2)
# Download World Bank data for life expectancy (total, female, male)
myDT <- wb(indicator = c("SP.DYN.LE00.IN",
"SP.DYN.LE00.FE.IN",
"SP.DYN.LE00.MA.IN")
)
myDT$date <- as.integer(myDT$date) # Convert year string to integer
myDT %>% filter(iso2c == "MY") %>% # Filter Malaysia data
ggplot(aes(x = date, y = value, color = indicatorID )) +
geom_line() +
xlab("Year") +
ylab("Life Expectancy at Birth") +
scale_colour_discrete(name ="Group",
labels=c("Females", "Total Pop.", "Male"))
# Download World Bank data for infant mortality rate and compare countries
myDT <- wb(indicator = c("SP.DYN.IMRT.IN")
)
myDT$date <- as.integer(myDT$date)
myDT %>% filter(iso2c == "MY" | iso2c == "AU" | iso2c == "ID" | iso2c == "TH"|
iso2c == "PH") %>%
ggplot(aes(x = date, y = value, color = country )) +
geom_line() +
xlab("Year") +
ylab("Infant Mortality Rate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment