Sochi Olympic Medals

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()

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


Advertisement

About tylerrinker

Data Scientist, open-source developer , #rstats enthusiast, #dataviz geek, and #nlp buff
This entry was posted in data, ggplot2, reshape, Uncategorized, visualization and tagged , , , , . Bookmark the permalink.

19 Responses to Sochi Olympic Medals

  1. nowgis says:

    Hi Tyler,
    thanks for the code.
    Btw – there is an error in the plot – for example – Poland have one gold medal, not bronze.

  2. Bosbeetle says:

    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

  3. tylerrinker says:

    @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 ๐Ÿ™‚

  4. Pingback: Sochi Olympic Medals | Patient 2 Earn

  5. Pingback: Winter Olympic Medal Standings, presented by R | Patient 2 Earn

  6. Pingback: Live Google Spreadsheet For Keeping Track Of Sochi Medals — rud.is

  7. Pingback: Live Google Spreadsheet For Keeping Track Of Sochi Medals | Patient 2 Earn

  8. Pingback: Sochi Medal Graphs - Graphically Speaking

  9. Pingback: Add a background png image to ggplot2 | julianhi's Blog

  10. Tony Hirst says:

    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…)

    • tylerrinker says:

      @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.

      • Tony Hirst says:

        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.

  11. Ray says:

    My sort looks like it has a jitter in it…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s