Skip to content

Instantly share code, notes, and snippets.

@christophergandrud
Created October 14, 2011 15:26
Show Gist options
  • Save christophergandrud/1287427 to your computer and use it in GitHub Desktop.
Save christophergandrud/1287427 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
debtWB <- WDI(country="all", indicator= c("DT.DOD.DPNG.CD",
"DT.DOD.DECT.GN.ZS", "DT.DOD.DSTC.ZS",
"PX.REX.REER", "NY.GDP.PCAP.CD",
"FB.AST.NPER.ZS"),
start=1980, end=2010)
## Rename variables
debtWB <-rename.vars(debtWB, from="DT.DOD.DPNG.CD", to="private.external.debt.LT", info=FALSE)
debtWB <- rename.vars(debtWB, from="DT.DOD.DECT.GN.ZS", to="total.ext.debt.per.gni", info=FALSE)
debtWB <- rename.vars(debtWB, from="DT.DOD.DSTC.ZS", to="short.term.debt.per.ext", info=FALSE)
debtWB <- rename.vars(debtWB, from="PX.REX.REER", to="forex.2005.baseline", info=FALSE)
debtWB <- rename.vars(debtWB, from="NY.GDP.PCAP.CD", to="gdp.per.capita.current.USD", info=FALSE)
debtWB <- rename.vars(debtWB, from="FB.AST.NPER.ZS", to="nonperforming.loans.per.total", info=FALSE)
## 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