In London Olympics 2012 Medal Tables At A Glance? I posted some treemap visualisations of the Olympics medal tables generated using a Google Visualisation Chart treemap component. I thought it might be worth posting a quick R generated example too, using the off-the-shelf/straight out of CRAN treemap component. (If you want to play along, download the data as CSV from here.)
The original data looks like this:
but ideally we want it to look like this:
I posted a quick recipe showing how to do this sort of reshaping in Google Refine, but in R it’s even easier – just melt the Gold, Silver and Bronze columns into a pair of columns…
Here’s the full code to do the reshaping and generate a simple treemap:
#load in the data from a file odata = read.csv("~/Downloads/nbc_olympic_medalscrape.csv") #Reshape the data require(reshape) odatar=melt(odata,id=c('cc','ccevent','Event')) #And generate the treemap in the simplest possible way require(treemap) tmPlot(odatar, index=c("cc", "Event","variable"), vSize="value", vColor='value', type="value")
And here’s the treemap, with country blocks ordered in this case by total medal haul:
(To view the countries ordered according to number of Golds, a quick fix would be to order hierarchy with the medal type shown at the highest level of the tree: index=c("variable","cc", "Event").)
Generating variant views (I described six variants in the original post) is easy enough – just tweak the order of the elements of the index setting. (I should have named the melt created columns something more sensible than the default, shouldn’t I? Note that the vSize and vColor value value (sic) refers to the column name that identifies the medalType column. The type value says use the numerical value…. (i.e. it’s literal – it doesn’t refer to a column name…)
Out of the can – simples enough… So what might we be able to do with a little bit more treatment? Examples via the comments, please ;-)
Tony – I noticed your post and the reference to melt. Here’s a start at a VBA and Google Apps script version of it. Thanks for the tip. http://ramblings.mcpher.com/Home/excelquirks/json/rmelt