Using R to Access and Analyze World Bank Data by Duane Edwards

One benefit of using R over many statistical software is its ability to access the World Bank database by means of the API and to analyse the data therein without the need to download the data onto your local computer.

To carry out this analysis in R, however, it is necessary to have the requisite package installed. The package used in R is the WDI package. After installing this package, we must explicitly call this package using the library function.

> library(WDI)

I will also be doing some plotting, so I will load the ggplot2 package also

> library(ggplot2)

After loading  these packages, it might be necessary to do a search of the World Bank database to get the codes associated with the indicators we want to plot. Let's plot the GDP for Guyana, Trinidad and Barbados. First of all, let's search the database for GDP.


This code will return all of the indicators associated with GDP. My search returned 148 instances, only the first ten of which are given below:

  [1,] "BG.GSR.NFSV.GD.ZS"             
  [2,] "BM.KLT.DINV.GD.ZS"             
  [3,] "BN.CAB.XOKA.GD.ZS"             
  [4,] "BN.CUR.GDPM.ZS"                
  [5,] "BN.GSR.FCTY.CD.ZS"             
  [6,] "BN.KLT.DINV.CD.ZS"             
  [7,] "BN.KLT.PRVT.GD.ZS"             
  [8,] "BN.TRF.CURR.CD.ZS"             
  [9,] "BNCABFUNDCD_"                  
 [10,] "BX.KLT.DINV.WD.GD.ZS" 

  [1,] "Trade in services (% of GDP)"                                                                                                              
  [2,] "Foreign direct investment, net outflows (% of GDP)"                                                                                        
  [3,] "Current account balance (% of GDP)"                                                                                                        
  [4,] "Current account balance excluding net official capital grants (% of GDP)"                                                                  
  [5,] "Net income (% of GDP)"                                                                                                                     
  [6,] "Foreign direct investment (% of GDP)"                                                                                                      
  [7,] "Private capital flows, total (% of GDP)"                                                                                                   
  [8,] "Net current transfers (% of GDP)"                                                                                                          
  [9,] "Current Account Balance, %GDP"                                                                                                             
 [10,] "Foreign direct investment, net inflows (% of GDP)" 

I chose number 82, which is  indicator: [82,] "NY.GDP.MKTP.CD", name: [82,] "GDP (current US$)" . Now with this information at hand, we could plot the GDP of any amount of countries over any period between 1960 and 2015. I choose to compare the GDP of Guyana, Trinidad and Barbados, three Caribbean countries during the period 2000 to 2015.

> gdp_data <- WDI(indicator="NY.GDP.MKTP.CD", country=c('GY', 'TT', 'BB'), start=2000, end='2015')

Now that all the information is loaded into the 'gdp_data' object, we could use this object to plot a line graph using the ggplot2 package.

> ggplot(gdp_data, aes(year, NY.GDP.MKTP.CD, color=country)) + geom_line() + xlab('Year') + ylab('GDP (Current US$)')

The above code will produce the following graph:

See entire code below:


> WDIsearch('gdp')
> gdp_data <- WDI(indicator="NY.GDP.MKTP.CD", country=c('GY', 'TT', 'BB'), start=2000, end='2015')
> ggplot(gdp_data, aes(year, NY.GDP.MKTP.CD, color=country)) + geom_line() + xlab('Year') + ylab('GDP (Current US$)')