I was looking for how to scrape tables from a website other than using python and R. I found an easy way to scrape data using Google spreadsheet as described in Tony’s blog. After randomly downloading some world economic data I wondered how to represent them using R or some other visualization tools.
This led me to learn rworldmap, which is a package for visualising global data, concentrating on data referenced by country codes or gridded at half degree resolution. The tricky part of this package is to join your data to the world map. You have to specify the name of column containing your country identiers (nameJoinColumn) and the type of code used (joinCode). If you only have country names rather than codes use joinCode=”NAME”, you can expect more mismatches because there is greater variation in what a single country may be named. Country codes could be obtained from here.
First, I tried to plot the World countries debt as a percentage of their GDP for the year 2011.
library(rworldmap) countryData = read.table("your_data.txt",sep="\t",header=T) #Join the data to the map. Make sure 'nameJoinColumn' has the ISO3 country codes sPDF = joinCountryData2Map( countryData, joinCode = "ISO3", nameJoinColumn = "ISOV3",verbose=TRUE) par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i") mapCountryData( sPDF, nameColumnToPlot="Percentage_of_GDP",colourPalette="heat",mapTitle='World Countries debt as a percentage of GDP for 2011')
mapByRegion( countryData, nameDataColumn="Percentage_of_GDP", joinCode="ISO3", nameJoinColumn="ISOV3", regionType="Stern", FUN="mean", mapTitle='World Regions debt as a percentage of GDP for 2011')
#creating a user defined colour palette op = palette(c('green','yellow','orange','red')) #find quartile breaks cutVector = quantile(sPDF@data[["Percentage_of_GDP"]],na.rm=TRUE) #classify the data to a factor sPDF@data[["Debt_categories"]] = cut(sPDF@data[["Percentage_of_GDP"]], cutVector, include.lowest=TRUE ) #rename the categories levels(sPDF@data[["Debt_categories"]]) = c('low', 'med', 'high', 'very high') #mapping mapCountryData(sPDF, nameColumnToPlot='Debt_categories', catMethod='categorical', mapTitle='World Countries debt as a percentage of GDP', colourPalette='palette', oceanCol='lightblue', missingCountryCol='white')
Data Source: Wikipedia and World Bank