This post uses pro-football (American) boxscore data from 1966 through 2013 and generates few interactive charts using rCharts, ggplot2 and shiny. It also provided a first time exposure to the power of dplyr. Data for these charts were scraped from the excellent reference site, pro-football-reference.com, using a function written in R. (This site has been used previously by other bloggers as a source for their data as well. See here and here for two examples.) Rest of this post has been created using slidify. The code for this post and relevant data are available at github. The code for the shiny application can be found here on github as well. (shiny is amazing, thanks R-Studio team and a big thanks to Ramnath Vaidyanathan for his support on rCharts).
When one clicks on any of the years for NFL Boxscores from this page at pro-football-reference.com , they are taken to a page that provides data on weekly games played for that year. Data include Week number, Day of game, Date, winning team, losing team, whether the losing team was at home ground, points of winner, points of loser (tie if both were same), net yards gained by winner, net yards gained by loser, turnovers by winner, and turnover by loser. The address of the page follows a specific pattern. For example:
- URL for data in 1999 is : http://www.pro-football-reference.com/years/1999/games.htm
- URL for data in 2013 is: http://www.pro-football-reference.com/years/2013/games.htm
There are 3 parts to the URL.
- A constant first part: “http://www.pro-football-reference.com/years/”
- A variable second part denoting the year, and
- A constant third part: “/games.htm”
The earliest year for which data are available happens to be 1922; it can be arrived at by consecutively clicking the link to the previous year from every year’s page. The strategy for scraping is to do the following.
- Initialize an empty dataframe with the same number of columns as data from the website.
- Use a function written for this post to retrieve data from a typical page and append data to our empty dataframe, and
- Iterate step 2 for years between 1922 through 2013. Although the function extracts data from all years, our analyses will be restricted to the years beyond 1966 (the first season with a Super Bowl)
Let us now verify if we have exactly 48 Super Bowls in our data.
Okay, 48 Super Bowls. We are on track. The Week column also has values of “Week”, “NA”, and ““(blank). Let us clean data further. Putting dplyr to work.
Few more changes required to the data frame.
- Removing a column named blank because there are no entries at all (structure of the website).
- Converting columns of points, yards and turnovers to numeric. They are currently factor variables.
- Reorder factor levels of Week to make them chronological.
Let us add 7 more columns to our data. Columns for point difference between winner and loser, difference in turnovers between winner and loser, and lastly, difference in net yards gained between winner and loser. We also add 3 columns for total points, total turnovers, and total yards gained in the game. The last column, Details, will come in handy to provide some information regarding teams that played the game and their points.
A quick check again - When did the first Super Bowl happen and in which year did the latest Super Bowl happen? How many Super Bowls?
There have been 48 Super Bowls (all this trouble to find this?). The catch is that there’s a lag of a year in the data, i.e., the Year column indicates year of the football season. The Super Bowl for that season occured the following calendar year. This is something to keep in mind when following through with rest of the analysis.
How many times did different teams that played in the Super Bowl win and lose?
We address this question by creating two single column data frames, one of only the winning teams and the other of only the losing team. Then we add another column to both these data frame with a dummy indicator for a win or a loss. These are then merged into a single data frame for an nvd3 plot using rCharts.
Hovering over the bars should provide details of different teams. The stacked bars provide a count of the number of their wins (green) and losses (in red). The combined height is the number of appearances.You can also unstack the bars by clicking the “Grouped” circle/dot (top left).
Statistics for key variables for all Super Bowls
The following nvd3 chart, via rCharts, begins by plotting only the difference in points across all the Super Bowls. Additional lines can be plotted (or removed) by clicking on the legend above the chart. Also note that the smaller chart below the main chart has a sliding horizontal axis, which will magnify the main chart in the time-window specified. You can get details of which team won and which team lost by hovering over the lines.
Have total points, total yards gained, total turnovers and differences between winners and losers with respect to points, yards gained, and turnovers changed over years since 1966? Data for a year are averaged across all weeks for the season, including the Super Bowl.
As with the earlier chart, this begins by plotting only the mean difference in points across all years. Additional lines can be plotted (or removed) by clicking on the legend above the chart. Yet again, the smaller chart below the main chart has a sliding horizontal axis, which will magnify the main chart in the time-window specified.
Have different statistics changed across different weeks of a season, including the Super Bowl? Data for a week is averaged across all years since Super Bowl began in 1966.
In the following interactive highchart via rCharts, you can use the mouse to draw a square/rectangle in the chart area to zoom that section. You can also click on the text of the legend to make that specific line disappear (or reappear).
Lastly, an interactive shiny application for data from 1966 which does the following.
- Plots mean of different statistics by week for different years using ggplot2
- Interactive scatterplots for every week and year with information on the teams playing and outcome of the game using nvd3 (via rCharts).
Before we work on the app, we need to do a bit more processing of the data. For one, we will save the weeklystatsdf data frame. We also create the following data frame (named ‘weekgpyr’). The latter will be used for generating plots for weekly and annual means and our weeklystatsdf data frame will be used to generate interactive scatterplots.
The code of this app can be found with the code for the entire post on github (see the links provided at the beginning of the post).
#<iframe height="850" width="100%" src="http://patilv.shinyapps.io/nflshiny/"></iframe>