In previous posts I demonstrated R plots created using World Bank Data through their API. The following is a much nicer example of what is possible. Many thanks to Vincent Arel-Bundock for sharing his work to make the World Bank Development Indicator API data available as an R package! He also provided comments in a previous post that show great examples of the use of the API (the results of which are pictured above).In only 4 lines of code, the graph above is created with data straight from the World Bank. This example requires the ggplot2 library as well as the new WDI library. WDI is used to return a data frame that is passed to ggplot to produce the graph.
DF <- WDI(country=c("US","CA","MX"), indicator="NY.GDP.MKTP.KD.ZG", start=1990, end=2008)
ggplot(DF, aes(year, NY.GDP.MKTP.KD.ZG, color=country))+geom_line(stat=”identity”)+theme_bw()+xlab(“Year”)+opts(title=”Annual GDP Growth rate (%)”)+ylab(“”)
There is a reference manual that describes the use of ggplot2 in greater detail. Oh – one other note. I generally save charts using the png command. This did not work with ggplot2, I needed to create the chart in R and then executed the following command: