Recently I have been experimenting with R’s data visualization capabilities and I wanted to test how the maps plotted with rworldmap could be made interactive through the manipulate package, part of RStudio. Interactivity can add a lot to our understanding of complex data sets, where variations happens along multiple dimensions. To get inspired on the topic you can watch this great Ted Talk by Hans Rosling: Stats that reshape your world-view.
With only few lines of R code I was able to obtain an interactive map of the world population where the displayed Year (from 1950 to 2010) can be selected through a slider.
The source data set I utilized comes from the United Nations. The link is in the source code below. I used the first Sheet in the downloaded Excel named ESTIMATES. Before saving it to CSV, I had to set the formatting of all number cells to general as the original number formatting contained spaces and, surprisingly enough, they were left in after the conversion. This confused R very much!
Without further due, here is the script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#### INTERACTIVE MAP OF WORLD POPULATION 1950-2010 #### #### Marco Ghislanzoni, 2013 #### http://marcoghislanzoni.com # Load the rworldmap library library(rworldmap) # And the manipulate library library(manipulate) # Read world population data by country 1950 – 2010 # Source: WPP2012_POP_F01_1_TOTAL_POPULATION_BOTH_SEXES.xls # Downloaded from: http://esa.un.org/wpp/Excel-Data/population.htm # Converted to CSV in Excel # Assumes the file is in the current working directory data <– read.csv(“WPP2012_POP_F01_1_TOTAL_POPULATION_BOTH_SEXES.csv”, header=TRUE) # Convert country code to chr. Originally is an int. data$Country.code <– as.character(data$Country.code) # Join the world population data with the world map, matching by country code # Note: not all countries are matched! data.map <– joinCountryData2Map(data, joinCode = “UN”, nameJoinColumn = “Country.code”) # Plot a sample map for 2010’s population data # mapCountryData(data.map, nameColumnToPlot=”X2010″) # Plot an interactive map where you can change the displayed Year from 1950 to 2010 with a slider manipulate(mapCountryData(data.map, nameColumnToPlot=paste(“X”, Year, sep=“”), catMethod=c(0,10, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9)), Year=slider(1950,2010)) ### END OF SOURCE CODE #### |
The charts still need some aesthetical improvement (e.g. title, legend) but the concept works fairly well. It is impressive to quickly scroll from 1950 to 2010 (you can use the right and left arrow keys for that once the focus is on the slider) to see in which countries and areas the population has been growing faster.
Next I think I am going to try some bubble charts along the inspiration provided by Rosling. Till next time!