Graphing football world cup shots with rCharts

[This article was first published on Rcrastinate, 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.

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 their blog: Rcrastinate.

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.

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)