Sochi Olympic Medals

February 9, 2014
By

(This article was first published on TRinker's R Blog » R, and kindly contributed to R-bloggers)

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

plot of chunk unnamed-chunk-3

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

 


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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)