Site icon R-bloggers

Sochi Olympic Medals

[This article was first published on TRinker's R Blog » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 = T)

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", "Bronze", "Silver", "Gold", "Total")
    raw <- raw[order(raw[, "Total"]), ]
    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")

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

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


To leave a comment for the author, please follow the link and comment on their blog: TRinker's R Blog » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.