Plotting World Bank Data with R

June 8, 2010

(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:

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).
plotWorldBank= function (country=’US’,
# Construct the URL
url = paste(‘’,

# Print URL for reference

# 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))

# Plot the data
par(las=2,mar=c(4, 8, 1, 2) + 0.1)
barplot(t(rev(values)), main=paste(countryName ,indicator),
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 their blog: R-Chart. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)