Graphing football world cup shots with rCharts

July 20, 2014
By

(This article was first published on Rcrastinate, and kindly contributed to R-bloggers)

So, the FIFA World Cup is over. Germany won. Yay!

Now with so much less football matches to watch, I tried the awesome new R package rCharts by Ramnath Vaidyanathan. In this case, I used the capability to generate NVD3 charts. Check it out...

You can mouse over the data points (players) to get the name of the player and information about how many of his shots were on target. You can switch on/off all countries and magnify portions of the plot by moving your mouse around (activate the option 'magnify' to do so).




The code is really simple (that's why rCharts is so awesome). The tooltips, however, took me a while. I only supplied the code to generate the chart. I suppose you have a dataframe shots that looks like this:

               name country games minutes shots shots_goal shots_alu
1     Karim BENZEMA     fra     5     450    32         25         1
2    Angel DI MARIA     arg     5     423    25         21         1
3 CRISTIANO RONALDO     por     3     270    23         14         1
4      Lionel MESSI     arg     7     693    22         10         0
5   Xherdan SHAQIRI     sui     4     387    21         16         0
6      Arjen ROBBEN     ned     7     690    20         19         0
.
.


library(rCharts)
pl <- nPlot(x = "minutes", y = "shots", data = shots, group = "country", type = "scatterChart")
pl$params$width <- 600
pl$params$height <- 600

pl$xAxis(axisLabel = 'Minutes played')
pl$yAxis(axisLabel = 'Shots')

pl$chart(tooltipContent = "#! function (a, b, c, data) { 
  return data.point.name + ', ' + data.point.shots_goal + ' shots on goal.'
} !#")

pl$save("fifa.html", standalone=T)


To leave a comment for the author, please follow the link and comment on his blog: Rcrastinate.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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.