
For those who are addicted to R and haven’t the time to click the mouse on a web browser you too can still be informed about the results of the 2014 Sochi Winter Olympics. I was inspired by a SO response around the London games a couple years back.
Packages to Load
packs <- c("knitr", "ggplot2", "XML", "reshape2", "rCharts")
lapply(packs, require, character.only = TRUE)
The Script
olympics <- function(theurl = "http://www.sochi2014.com/en/medal-standings", include.zero = FALSE) { invisible(lapply(c('ggplot2', 'XML', 'reshape2') , require, character.only=TRUE)) ## Grab Data, Clean and Reshape raw <- readHTMLTable(theurl, header=FALSE, colClasses = c(rep("factor", 2), rep("numeric", 4))) raw <- as.data.frame(raw)[, -1] colnames(raw) <- c("Country", "Gold", "Silver", "Bronze", "Total") raw <- with(raw, raw[order(Total, Gold, Silver, Bronze), ]) if (!include.zero) { raw <- raw[raw[, "Total"] != 0, ] } raw[, "Country"] <- factor(raw[, "Country"], levels = raw[, "Country"]) rownames(raw) <- NULL mdat <- melt(raw, value.name = "Count", variable.name = "Place", id.var = "Country") mdat[, "Place"] <- factor(mdat[, "Place"], levels=c("Gold", "Silver", "Bronze", "Total")) ## Plot the Data plot1 <- ggplot(mdat, aes(x = Count, y = Country, colour = Place)) + geom_point() + facet_grid(.~Place) + theme_bw()+ scale_colour_manual(values=c("#CC6600", "#999999", "#FFCC33", "#000000")) print(plot1) return(invisible(raw)) }
The Visual Results
x <- olympics()
As a Data Table
dTable(x)$show('inline', include_assets = TRUE, cdn = TRUE)
WordPress and Data Table don’t play well together so you’ll need to run the code to see it in action.
Discussion
I have chosen a dot plot to display the data because it’s easy to quickly get a sense of the data yet be able to compare relatively easily. Dot plots take advantage of the powerful pre-attentive attribute of distance (The most powerful way to visually convey quantitative information). Stephen Few gives his two cents about dot plots here.
I’m lazy but this would be a fun Shiny app to build. [EDIT @Ty Henkaline answers the call for a Shiny appย and provides the ui.R and server.R]
Thanks to @Ramnath for help implementing the chart as a jQuery DataTable.
*Created using the reports package
@tylerrinker Nice work! Here is a short script to visualize it using rCharts and NVD3 https://t.co/x3BiCKLgVX
โ Ramnath Vaidyanathan (@ramnath_vaidya) February 10, 2014
@tylerrinker Kudos for demo of how to use #rstats to pull real-time medal standings for #Olympics2014 at #Sochi! http://t.co/F4nJqFYQ8Y
— Ty Henkaline (@tyhenkaline) February 10, 2014
Hi Tyler,
thanks for the code.
Btw – there is an error in the plot – for example – Poland have one gold medal, not bronze.
Nice script however bronze and gold are switched, also the order should not reflect total but factor in gold vs silver and bronze ๐ but that is easy enough to implement
@nowgis and @Bosbeetle, both of you caught the same error. Thanks for the feedback. I promise it wasn’t an attempt to give the U.S. more gold medals ๐
Here you go: http://glimmer.rstudio.com/tchenkaline/Sochi_Medals/.
@Ty nice work. Any plans to share the code with others in a blog post. If not I’d encourage you to share the ui.R and server.R files via a link.
I was in the process when you commented. See below.
Thanks for sharing your code! It makes for a nice, timely little app.
And here’s the RShiny code:
UI: https://gist.github.com/tyhenkaline/8919255
Server: https://gist.github.com/tyhenkaline/8919274
Thanks for sharing ๐
@tylerrinker & @Ty: thanks for sharing! Again learned a lot by visiting an R-blog.
And after tomorrow, the scores will look even better! Go NL, go!!! ๐
Pingback: Sochi Olympic Medals | Patient 2 Earn
Pingback: Winter Olympic Medal Standings, presented by R | Patient 2 Earn
Pingback: Live Google Spreadsheet For Keeping Track Of Sochi Medals — rud.is
Pingback: Live Google Spreadsheet For Keeping Track Of Sochi Medals | Patient 2 Earn
Pingback: Sochi Medal Graphs - Graphically Speaking
Pingback: Add a background png image to ggplot2 | julianhi's Blog
I’m a bit late to this, but have you thought about other ways of visualising this data? For example, I’ve previously dabbled with looking at treemaps as ways of representing medal counts – eg using html5 libraries http://blog.ouseful.info/2012/08/06/london-olympics-2012-medal-tables-at-a-glance/ or in R http://blog.ouseful.info/2012/08/08/olympic-medals-visualising-ots-using-an-r-treemap/ (though the latter sketch suffers from some really bad layout problems…)
@Tony Thanks for your comment. My mind tended to go to the dotplot with this because it taps into the most powerful pre-attentive attribute (distance/proximity) for comparing quantities. It would be pretty simple to implement other methods of visualization (particularly in R) but I would challenge that the ordered dotplot allows for the easiest comparison across countries and medals with the least loss (there’s usually a trade off between loss of information and visual representation that allows for pattern recognition/comparison). The tree map definitely is prettier to look at but the ability to gain information is diminished. This is indicated by the need for 2 pre-attentive attributes to represent the same information (color and 2-D space). Color is a poorer choice for representing quantitative values if you have better options available. Additionally, while the human eye is great at detecting proximity or length comparison, it does a poorer job with the 2-D space. Most of the conclusions I’ve stated here are a product of Stephen Few’s work; particularly Now You See It and Show Me Numbers.
Agreed – but the tree map offers summed containment, as well as allowing you to compare individual contribution from medal types. (Stacked bars do this more clearly, I suppose.) That said, I keep on coming back to tree maps as a useful way of representing wholes that are made of parts that are themselves made of parts. Yes, the area perception can be hard to guage, but you can order the arrangement of items within a treemap to make it clear which of the first level areas is the largest.)
The ability to morph treemaps by rearranging hierarchy layers also support casual conversations with the data, which I also try to advocate.
My sort looks like it has a jitter in it…