Plotting World Bank Data with R

June 8, 2010
By

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

Copy and paste the code listed in this post below, and you can create a plot of World Bank data using R with a call like the following:

plotWorldBank('USA','NY.GDP.MKTP.KD.ZG')

There is a wealth of economic and demographic available at the World Bank. They are also interested in developers using their data to produce applications. You can see their site for a full description and terms of use for their APIs. The following R function produces a simple bar plot of indicator data for a selected time period for a given country (and produced the chart above).

library('XML')

plotWorldBank= function (country='US',
indicator='NY.GDP.MKTP.CD',
start_year=2002,
end_year=2008,
color='blue'
){
# Construct the URL
url = paste('http://open.worldbank.org/countries/',
country,
'/indicators/',
indicator,
'?date=',
start_year,
':',
end_year,
sep='')

# Print URL for reference
print(url)

# Parse the XML
doc = xmlTreeParse(url, useInternal = TRUE)

# Extract the relevant values
indicator = xmlValue(getNodeSet(doc, "//wb:indicator")[[1]])
countryName = xmlValue(getNodeSet(doc, "//wb:country ")[[1]])
values = sapply(getNodeSet(doc, "//wb:value") , function(el) xmlValue(el))
dates = sapply(getNodeSet(doc, "//wb:date") , function(el) xmlValue(el))
names(values)=dates

# Plot the data
par(las=2,mar=c(4, 8, 1, 2) + 0.1)
barplot(t(rev(values)), main=paste(countryName ,indicator),
col=color)
}

Parameter Usage

The parameter values must be in the format specified by the world bank API. A few examples of these three character codes:

AUS Australia
BMU Bermuda
CHE Switzerland
GBR United Kingdom
USA United States

The list of county codes(in XML) can be found on pages starting here.

Likewise, special indicator codes are used to select the data set in question. These indicators cover quite a large variety of subjects including economic, energy, environmental, and social interests.

A few examples:

NY.GDP.MKTP.CD GDP (current US$)
NY.GDP.MKTP.KD.ZG GDP growth (annual %)
AG.CON.FERT.MT Fertilizer consumption (metric tons)
IT.NET.BBND Fixed broadband Internet subscribers
SH.XPD.TOTL.ZS Health expenditure, total (% of GDP)
ST.INT.ARVL International tourism, number of arrivals

The list of indicators (in XML) can be found here.



To leave a comment for the author, please follow the link and comment on his blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.