Plotting World Bank Data with R

[This article was first published on R-Chart, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)