Skip to content

Instantly share code, notes, and snippets.

@abresler
Forked from christophergandrud/gist:1287427
Created August 3, 2012 17:27
Show Gist options
  • Save abresler/3249788 to your computer and use it in GitHub Desktop.
Save abresler/3249788 to your computer and use it in GitHub Desktop.
Google Motion Chart of World Bank External Debt Data
# Create Google Motion Chart from World Bank Finance Data
# Inspired by http://lamages.blogspot.com/2011/09/accessing-and-plotting-world-bank-data.html
# Written by Christopher Gandrud
# 15 October 2011
library(WDI)
library(gregmisc)
library(googleVis)
## Collect indicator variables based on World Bank variable Key
## Indicators to collect
# External debt stocks, private non-guaranteed (Current USD) long-term: DT.DOD.DPNG.CD
# External debt as a % of GNI: DT.DOD.DECT.GN.ZS
# Short-term debt (% of total external debt): DT.DOD.DSTC.ZS
# Real effective exchange rate index (2005 = 100): PX.REX.REER
# GDP per capita (current USD): NY.GDP.PCAP.CD
# Bank nonperforming loans to total gross loans: FB.AST.NPER.ZS
private <- WDI(country="all", indicator=("DT.DOD.DPNG.CD"), start=1980, end=2010)
debt.gni <- WDI(country="all", indicator=("DT.DOD.DECT.GN.ZS"), start=1980, end=2010)
short <- WDI(country="all", indicator=("DT.DOD.DSTC.ZS"), start=1980, end=2010)
forex <- WDI(country="all", indicator=("PX.REX.REER"), start=1980, end=2010)
gdp <- WDI(country="all", indicator=("NY.GDP.PCAP.CD"), start=1980, end=2010)
npl <- WDI(country="all", indicator=("FB.AST.NPER.ZS"), start=1980, end=2010)
## Rename variables
private <-rename.vars(private, from="DT.DOD.DPNG.CD", to="private.external.debt.LT", info=FALSE)
debt.gni <- rename.vars(debt.gni, from="DT.DOD.DECT.GN.ZS", to="total.ext.debt.per.gni", info=FALSE)
short <- rename.vars(short, from="DT.DOD.DSTC.ZS", to="short.term.debt.per.ext", info=FALSE)
forex <- rename.vars(forex, from="PX.REX.REER", to="forex.2005.baseline", info=FALSE)
gdp <- rename.vars(gdp, from="NY.GDP.PCAP.CD", to="gdp.per.capita.current.USD", info=FALSE)
npl <- rename.vars(npl, from="FB.AST.NPER.ZS", to="nonperforming.loans.per.total", info=FALSE)
## Merge the data sets
debtWB <- merge(private, debt.gni)
debtWB <- merge(debtWB, short)
debtWB <- merge(debtWB, forex)
debtWB <- merge(debtWB, gdp)
debtWB <- merge(debtWB, npl)
## Remove iso code
debtWB <- remove.vars(debtWB, names="iso2c", info=FALSE)
## Drop Nicaragua & Uganda (extreme forex outliers)
debtWB <- subset(debtWB, country!="Uganda" & country!="Nicaragua")
## Create Google motion chart
debtWB.motion <- gvisMotionChart(debtWB, idvar="country", timevar="year",
options=list(width=700, height=600))
## View motion chart
plot(debtWB.motion)
## Create html version that can be inserted into an existing webpage with an <iframe>
print(debtWB.motion, 'chart', "~/external.debt.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment