April 30, 2019

nflscrapR is amazing

Scraping NFL data

Note: An original version of this post had issues induced by overtime games. There is a better way to handle all of this that I learned from a brief analysis of a tie game between Cleveland and Pittsburgh in Week One.

The nflscrapR package is designed to make data on NFL games more easily available. To install the package, we need to grab it from github. The package provides play by play data available for entire NFL seasons. Numerous fascinating applications of this data have come out of the Sloan Conference and other places that do cutting edge sports analytics.

devtools::install_github(repo = "maksimhorowitz/nflscrapR")

The github page for nflscrapR is quite informative.

Getting Some Data

Following the guide to the package on GitHub, let me try their example. It works, but I will save off the data to github to avoid spamming the host.

library(nflscrapR)
all_2018_games <- scrape_game_ids(2018) # Default is regular season

Gives me a list of all games. I saved them to an .rds file and stored them on Gitub. To make it browsable, I will use kable and the like.

library(tidyverse)
library(nflscrapR)
library(RCurl)
library(kableExtra)
library(gganimate)
library(ggrepel)
library(ggthemes)
library(RColorBrewer)
library(janitor)
all_2018_games <-readRDS(url("https://github.com/robertwwalker/academic-mymod/raw/master/data/NFLGames2018.rds"))
all_2018_games %>%  kable()  %>%
  kable_styling() %>%
  scroll_box(width = "100%", height = "300px")
type game_id home_team away_team week season state_of_game game_url home_score away_score
reg 2018090600 PHI ATL 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090600/2018090600_gtd.json 18 12
reg 2018090900 BAL BUF 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090900/2018090900_gtd.json 47 3
reg 2018090907 NYG JAX 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090907/2018090907_gtd.json 15 20
reg 2018090906 NO TB 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090906/2018090906_gtd.json 40 48
reg 2018090905 NE HOU 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090905/2018090905_gtd.json 27 20
reg 2018090904 MIN SF 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090904/2018090904_gtd.json 24 16
reg 2018090903 MIA TEN 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090903/2018090903_gtd.json 27 20
reg 2018090902 IND CIN 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090902/2018090902_gtd.json 23 34
reg 2018090901 CLE PIT 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090901/2018090901_gtd.json 21 21
reg 2018090908 LAC KC 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090908/2018090908_gtd.json 28 38
reg 2018090911 DEN SEA 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090911/2018090911_gtd.json 27 24
reg 2018090910 CAR DAL 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090910/2018090910_gtd.json 16 8
reg 2018090909 ARI WAS 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090909/2018090909_gtd.json 6 24
reg 2018090912 GB CHI 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018090912/2018090912_gtd.json 24 23
reg 2018091000 DET NYJ 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018091000/2018091000_gtd.json 17 48
reg 2018091001 OAK LA 1 2018 POST http://www.nfl.com/liveupdate/game-center/2018091001/2018091001_gtd.json 13 33
reg 2018091300 CIN BAL 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091300/2018091300_gtd.json 34 23
reg 2018091600 ATL CAR 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091600/2018091600_gtd.json 31 24
reg 2018091608 WAS IND 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091608/2018091608_gtd.json 9 21
reg 2018091607 TEN HOU 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091607/2018091607_gtd.json 20 17
reg 2018091606 TB PHI 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091606/2018091606_gtd.json 27 21
reg 2018091605 PIT KC 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091605/2018091605_gtd.json 37 42
reg 2018091604 NYJ MIA 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091604/2018091604_gtd.json 12 20
reg 2018091601 BUF LAC 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091601/2018091601_gtd.json 20 31
reg 2018091602 GB MIN 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091602/2018091602_gtd.json 29 29
reg 2018091603 NO CLE 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091603/2018091603_gtd.json 21 18
reg 2018091610 SF DET 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091610/2018091610_gtd.json 30 27
reg 2018091609 LA ARI 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091609/2018091609_gtd.json 34 0
reg 2018091612 JAX NE 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091612/2018091612_gtd.json 31 20
reg 2018091611 DEN OAK 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091611/2018091611_gtd.json 20 19
reg 2018091613 DAL NYG 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091613/2018091613_gtd.json 20 13
reg 2018091700 CHI SEA 2 2018 POST http://www.nfl.com/liveupdate/game-center/2018091700/2018091700_gtd.json 24 17
reg 2018092000 CLE NYJ 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092000/2018092000_gtd.json 21 17
reg 2018092300 ATL NO 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092300/2018092300_gtd.json 37 43
reg 2018092309 WAS GB 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092309/2018092309_gtd.json 31 17
reg 2018092308 PHI IND 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092308/2018092308_gtd.json 20 16
reg 2018092307 MIN BUF 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092307/2018092307_gtd.json 6 27
reg 2018092306 MIA OAK 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092306/2018092306_gtd.json 28 20
reg 2018092301 BAL DEN 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092301/2018092301_gtd.json 27 14
reg 2018092302 CAR CIN 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092302/2018092302_gtd.json 31 21
reg 2018092303 HOU NYG 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092303/2018092303_gtd.json 22 27
reg 2018092304 JAX TEN 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092304/2018092304_gtd.json 6 9
reg 2018092305 KC SF 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092305/2018092305_gtd.json 38 27
reg 2018092310 LA LAC 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092310/2018092310_gtd.json 35 23
reg 2018092312 SEA DAL 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092312/2018092312_gtd.json 24 13
reg 2018092311 ARI CHI 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092311/2018092311_gtd.json 14 16
reg 2018092313 DET NE 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092313/2018092313_gtd.json 26 10
reg 2018092400 TB PIT 3 2018 POST http://www.nfl.com/liveupdate/game-center/2018092400/2018092400_gtd.json 27 30
reg 2018092700 LA MIN 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018092700/2018092700_gtd.json 38 31
reg 2018093005 JAX NYJ 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093005/2018093005_gtd.json 31 12
reg 2018093006 NE MIA 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093006/2018093006_gtd.json 38 7
reg 2018093007 TEN PHI 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093007/2018093007_gtd.json 26 23
reg 2018093004 IND HOU 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093004/2018093004_gtd.json 34 37
reg 2018093003 GB BUF 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093003/2018093003_gtd.json 22 0
reg 2018093002 DAL DET 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093002/2018093002_gtd.json 26 24
reg 2018093001 CHI TB 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093001/2018093001_gtd.json 48 10
reg 2018093000 ATL CIN 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093000/2018093000_gtd.json 36 37
reg 2018093008 ARI SEA 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093008/2018093008_gtd.json 17 20
reg 2018093009 OAK CLE 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093009/2018093009_gtd.json 45 42
reg 2018093011 NYG NO 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093011/2018093011_gtd.json 18 33
reg 2018093010 LAC SF 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093010/2018093010_gtd.json 29 27
reg 2018093012 PIT BAL 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018093012/2018093012_gtd.json 14 26
reg 2018100100 DEN KC 4 2018 POST http://www.nfl.com/liveupdate/game-center/2018100100/2018100100_gtd.json 23 27
reg 2018100400 NE IND 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100400/2018100400_gtd.json 38 24
reg 2018100700 BUF TEN 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100700/2018100700_gtd.json 13 12
reg 2018100707 PIT ATL 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100707/2018100707_gtd.json 41 17
reg 2018100706 NYJ DEN 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100706/2018100706_gtd.json 34 16
reg 2018100705 KC JAX 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100705/2018100705_gtd.json 30 14
reg 2018100704 DET GB 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100704/2018100704_gtd.json 31 23
reg 2018100703 CLE BAL 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100703/2018100703_gtd.json 12 9
reg 2018100701 CAR NYG 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100701/2018100701_gtd.json 33 31
reg 2018100702 CIN MIA 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100702/2018100702_gtd.json 27 17
reg 2018100708 LAC OAK 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100708/2018100708_gtd.json 26 10
reg 2018100710 SF ARI 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100710/2018100710_gtd.json 18 28
reg 2018100709 PHI MIN 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100709/2018100709_gtd.json 21 23
reg 2018100711 SEA LA 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100711/2018100711_gtd.json 31 33
reg 2018100712 HOU DAL 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100712/2018100712_gtd.json 19 16
reg 2018100800 NO WAS 5 2018 POST http://www.nfl.com/liveupdate/game-center/2018100800/2018100800_gtd.json 43 19
reg 2018101100 NYG PHI 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101100/2018101100_gtd.json 13 34
reg 2018101400 ATL TB 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101400/2018101400_gtd.json 34 29
reg 2018101408 WAS CAR 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101408/2018101408_gtd.json 23 17
reg 2018101407 OAK SEA 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101407/2018101407_gtd.json 3 27
reg 2018101406 NYJ IND 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101406/2018101406_gtd.json 42 34
reg 2018101405 MIN ARI 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101405/2018101405_gtd.json 27 17
reg 2018101401 CIN PIT 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101401/2018101401_gtd.json 21 28
reg 2018101402 CLE LAC 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101402/2018101402_gtd.json 14 38
reg 2018101403 HOU BUF 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101403/2018101403_gtd.json 20 13
reg 2018101404 MIA CHI 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101404/2018101404_gtd.json 31 28
reg 2018101409 DEN LA 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101409/2018101409_gtd.json 20 23
reg 2018101411 TEN BAL 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101411/2018101411_gtd.json 0 21
reg 2018101410 DAL JAX 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101410/2018101410_gtd.json 40 7
reg 2018101412 NE KC 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101412/2018101412_gtd.json 43 40
reg 2018101500 GB SF 6 2018 POST http://www.nfl.com/liveupdate/game-center/2018101500/2018101500_gtd.json 33 30
reg 2018101800 ARI DEN 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018101800/2018101800_gtd.json 10 45
reg 2018102100 LAC TEN 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102100/2018102100_gtd.json 20 19
reg 2018102104 JAX HOU 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102104/2018102104_gtd.json 7 20
reg 2018102108 PHI CAR 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102108/2018102108_gtd.json 17 21
reg 2018102107 NYJ MIN 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102107/2018102107_gtd.json 17 37
reg 2018102102 CHI NE 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102102/2018102102_gtd.json 31 38
reg 2018102103 IND BUF 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102103/2018102103_gtd.json 37 5
reg 2018102109 TB CLE 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102109/2018102109_gtd.json 26 23
reg 2018102106 MIA DET 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102106/2018102106_gtd.json 21 32
reg 2018102101 BAL NO 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102101/2018102101_gtd.json 23 24
reg 2018102110 WAS DAL 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102110/2018102110_gtd.json 20 17
reg 2018102111 SF LA 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102111/2018102111_gtd.json 10 39
reg 2018102105 KC CIN 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102105/2018102105_gtd.json 45 10
reg 2018102200 ATL NYG 7 2018 POST http://www.nfl.com/liveupdate/game-center/2018102200/2018102200_gtd.json 23 20
reg 2018102500 HOU MIA 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102500/2018102500_gtd.json 42 23
reg 2018102800 JAX PHI 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102800/2018102800_gtd.json 18 24
reg 2018102805 KC DEN 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102805/2018102805_gtd.json 30 23
reg 2018102807 PIT CLE 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102807/2018102807_gtd.json 33 18
reg 2018102806 NYG WAS 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102806/2018102806_gtd.json 13 20
reg 2018102804 DET SEA 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102804/2018102804_gtd.json 14 28
reg 2018102803 CIN TB 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102803/2018102803_gtd.json 37 34
reg 2018102802 CHI NYJ 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102802/2018102802_gtd.json 24 10
reg 2018102801 CAR BAL 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102801/2018102801_gtd.json 36 21
reg 2018102808 OAK IND 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102808/2018102808_gtd.json 28 42
reg 2018102809 ARI SF 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102809/2018102809_gtd.json 18 15
reg 2018102810 LA GB 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102810/2018102810_gtd.json 29 27
reg 2018102811 MIN NO 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102811/2018102811_gtd.json 20 30
reg 2018102900 BUF NE 8 2018 POST http://www.nfl.com/liveupdate/game-center/2018102900/2018102900_gtd.json 6 25
reg 2018110100 SF OAK 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110100/2018110100_gtd.json 34 3
reg 2018110401 BUF CHI 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110401/2018110401_gtd.json 9 41
reg 2018110402 CAR TB 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110402/2018110402_gtd.json 42 28
reg 2018110403 CLE KC 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110403/2018110403_gtd.json 21 37
reg 2018110404 MIA NYJ 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110404/2018110404_gtd.json 13 6
reg 2018110400 BAL PIT 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110400/2018110400_gtd.json 16 23
reg 2018110405 MIN DET 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110405/2018110405_gtd.json 24 9
reg 2018110406 WAS ATL 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110406/2018110406_gtd.json 14 38
reg 2018110407 DEN HOU 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110407/2018110407_gtd.json 17 19
reg 2018110408 SEA LAC 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110408/2018110408_gtd.json 17 25
reg 2018110409 NO LA 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110409/2018110409_gtd.json 45 35
reg 2018110410 NE GB 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110410/2018110410_gtd.json 31 17
reg 2018110500 DAL TEN 9 2018 POST http://www.nfl.com/liveupdate/game-center/2018110500/2018110500_gtd.json 14 28
reg 2018110800 PIT CAR 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018110800/2018110800_gtd.json 52 21
reg 2018111101 CIN NO 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111101/2018111101_gtd.json 14 51
reg 2018111102 CLE ATL 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111102/2018111102_gtd.json 28 16
reg 2018111100 CHI DET 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111100/2018111100_gtd.json 34 22
reg 2018111105 KC ARI 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111105/2018111105_gtd.json 26 14
reg 2018111108 TEN NE 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111108/2018111108_gtd.json 34 10
reg 2018111107 TB WAS 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111107/2018111107_gtd.json 3 16
reg 2018111106 NYJ BUF 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111106/2018111106_gtd.json 10 41
reg 2018111104 IND JAX 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111104/2018111104_gtd.json 29 26
reg 2018111109 OAK LAC 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111109/2018111109_gtd.json 6 20
reg 2018111110 LA SEA 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111110/2018111110_gtd.json 36 31
reg 2018111103 GB MIA 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111103/2018111103_gtd.json 31 12
reg 2018111111 PHI DAL 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111111/2018111111_gtd.json 20 27
reg 2018111200 SF NYG 10 2018 POST http://www.nfl.com/liveupdate/game-center/2018111200/2018111200_gtd.json 23 27
reg 2018111500 SEA GB 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111500/2018111500_gtd.json 27 24
reg 2018111801 BAL CIN 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111801/2018111801_gtd.json 24 21
reg 2018111800 ATL DAL 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111800/2018111800_gtd.json 19 22
reg 2018111805 NYG TB 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111805/2018111805_gtd.json 38 35
reg 2018111810 JAX PIT 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111810/2018111810_gtd.json 16 20
reg 2018111806 WAS HOU 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111806/2018111806_gtd.json 21 23
reg 2018111804 IND TEN 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111804/2018111804_gtd.json 38 10
reg 2018111803 DET CAR 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111803/2018111803_gtd.json 20 19
reg 2018111808 LAC DEN 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111808/2018111808_gtd.json 22 23
reg 2018111807 ARI OAK 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111807/2018111807_gtd.json 21 23
reg 2018111809 NO PHI 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111809/2018111809_gtd.json 48 7
reg 2018111802 CHI MIN 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111802/2018111802_gtd.json 25 20
reg 2018111900 LA KC 11 2018 POST http://www.nfl.com/liveupdate/game-center/2018111900/2018111900_gtd.json 54 51
reg 2018112200 DET CHI 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112200/2018112200_gtd.json 16 23
reg 2018112201 DAL WAS 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112201/2018112201_gtd.json 31 23
reg 2018112202 NO ATL 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112202/2018112202_gtd.json 31 17
reg 2018112503 CIN CLE 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112503/2018112503_gtd.json 20 35
reg 2018112507 TB SF 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112507/2018112507_gtd.json 27 9
reg 2018112501 BUF JAX 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112501/2018112501_gtd.json 24 21
reg 2018112500 BAL OAK 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112500/2018112500_gtd.json 34 17
reg 2018112502 CAR SEA 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112502/2018112502_gtd.json 27 30
reg 2018112505 NYJ NE 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112505/2018112505_gtd.json 13 27
reg 2018112506 PHI NYG 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112506/2018112506_gtd.json 25 22
reg 2018112508 LAC ARI 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112508/2018112508_gtd.json 45 10
reg 2018112504 IND MIA 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112504/2018112504_gtd.json 27 24
reg 2018112509 DEN PIT 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112509/2018112509_gtd.json 24 17
reg 2018112510 MIN GB 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112510/2018112510_gtd.json 24 17
reg 2018112600 HOU TEN 12 2018 POST http://www.nfl.com/liveupdate/game-center/2018112600/2018112600_gtd.json 34 17
reg 2018112900 DAL NO 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018112900/2018112900_gtd.json 13 10
reg 2018120200 ATL BAL 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120200/2018120200_gtd.json 16 26
reg 2018120209 TB CAR 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120209/2018120209_gtd.json 24 17
reg 2018120207 NYG CHI 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120207/2018120207_gtd.json 30 27
reg 2018120206 MIA BUF 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120206/2018120206_gtd.json 21 17
reg 2018120205 JAX IND 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120205/2018120205_gtd.json 6 0
reg 2018120204 HOU CLE 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120204/2018120204_gtd.json 29 13
reg 2018120201 CIN DEN 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120201/2018120201_gtd.json 10 24
reg 2018120202 DET LA 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120202/2018120202_gtd.json 16 30
reg 2018120203 GB ARI 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120203/2018120203_gtd.json 17 20
reg 2018120210 OAK KC 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120210/2018120210_gtd.json 33 40
reg 2018120211 TEN NYJ 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120211/2018120211_gtd.json 26 22
reg 2018120213 SEA SF 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120213/2018120213_gtd.json 43 16
reg 2018120212 NE MIN 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120212/2018120212_gtd.json 24 10
reg 2018120208 PIT LAC 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120208/2018120208_gtd.json 30 33
reg 2018120300 PHI WAS 13 2018 POST http://www.nfl.com/liveupdate/game-center/2018120300/2018120300_gtd.json 28 13
reg 2018120600 TEN JAX 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120600/2018120600_gtd.json 30 9
reg 2018120900 BUF NYJ 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120900/2018120900_gtd.json 23 27
reg 2018120908 WAS NYG 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120908/2018120908_gtd.json 16 40
reg 2018120907 TB NO 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120907/2018120907_gtd.json 14 28
reg 2018120906 MIA NE 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120906/2018120906_gtd.json 34 33
reg 2018120905 KC BAL 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120905/2018120905_gtd.json 27 24
reg 2018120904 HOU IND 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120904/2018120904_gtd.json 21 24
reg 2018120903 GB ATL 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120903/2018120903_gtd.json 34 20
reg 2018120902 CLE CAR 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120902/2018120902_gtd.json 26 20
reg 2018120910 SF DEN 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120910/2018120910_gtd.json 20 14
reg 2018120909 LAC CIN 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120909/2018120909_gtd.json 26 21
reg 2018120911 ARI DET 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120911/2018120911_gtd.json 3 17
reg 2018120913 OAK PIT 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120913/2018120913_gtd.json 24 21
reg 2018120912 DAL PHI 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120912/2018120912_gtd.json 29 23
reg 2018120901 CHI LA 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018120901/2018120901_gtd.json 15 6
reg 2018121000 SEA MIN 14 2018 POST http://www.nfl.com/liveupdate/game-center/2018121000/2018121000_gtd.json 21 7
reg 2018121300 KC LAC 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121300/2018121300_gtd.json 28 29
reg 2018121500 NYJ HOU 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121500/2018121500_gtd.json 22 29
reg 2018121501 DEN CLE 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121501/2018121501_gtd.json 16 17
reg 2018121603 CHI GB 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121603/2018121603_gtd.json 24 17
reg 2018121602 BUF DET 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121602/2018121602_gtd.json 14 13
reg 2018121601 BAL TB 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121601/2018121601_gtd.json 20 12
reg 2018121600 ATL ARI 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121600/2018121600_gtd.json 40 14
reg 2018121604 CIN OAK 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121604/2018121604_gtd.json 30 16
reg 2018121608 NYG TEN 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121608/2018121608_gtd.json 0 17
reg 2018121607 MIN MIA 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121607/2018121607_gtd.json 41 17
reg 2018121606 JAX WAS 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121606/2018121606_gtd.json 13 16
reg 2018121605 IND DAL 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121605/2018121605_gtd.json 23 0
reg 2018121609 SF SEA 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121609/2018121609_gtd.json 26 23
reg 2018121610 PIT NE 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121610/2018121610_gtd.json 17 10
reg 2018121611 LA PHI 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121611/2018121611_gtd.json 23 30
reg 2018121700 CAR NO 15 2018 POST http://www.nfl.com/liveupdate/game-center/2018121700/2018121700_gtd.json 9 12
reg 2018122200 TEN WAS 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122200/2018122200_gtd.json 25 16
reg 2018122201 LAC BAL 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122201/2018122201_gtd.json 10 22
reg 2018122305 CLE CIN 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122305/2018122305_gtd.json 26 18
reg 2018122306 DAL TB 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122306/2018122306_gtd.json 27 20
reg 2018122307 DET MIN 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122307/2018122307_gtd.json 9 27
reg 2018122308 NE BUF 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122308/2018122308_gtd.json 24 12
reg 2018122309 NYJ GB 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122309/2018122309_gtd.json 38 44
reg 2018122310 PHI HOU 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122310/2018122310_gtd.json 32 30
reg 2018122304 CAR ATL 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122304/2018122304_gtd.json 10 24
reg 2018122300 IND NYG 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122300/2018122300_gtd.json 28 27
reg 2018122302 MIA JAX 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122302/2018122302_gtd.json 7 17
reg 2018122311 ARI LA 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122311/2018122311_gtd.json 9 31
reg 2018122312 SF CHI 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122312/2018122312_gtd.json 9 14
reg 2018122313 NO PIT 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122313/2018122313_gtd.json 31 28
reg 2018122314 SEA KC 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122314/2018122314_gtd.json 38 31
reg 2018122400 OAK DEN 16 2018 POST http://www.nfl.com/liveupdate/game-center/2018122400/2018122400_gtd.json 27 14
reg 2018123001 BUF MIA 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123001/2018123001_gtd.json 42 17
reg 2018123010 TB ATL 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123010/2018123010_gtd.json 32 34
reg 2018123008 NYG DAL 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123008/2018123008_gtd.json 35 36
reg 2018123007 NO CAR 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123007/2018123007_gtd.json 14 33
reg 2018123006 NE NYJ 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123006/2018123006_gtd.json 38 3
reg 2018123003 HOU JAX 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123003/2018123003_gtd.json 20 3
reg 2018123002 GB DET 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123002/2018123002_gtd.json 0 31
reg 2018123012 WAS PHI 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123012/2018123012_gtd.json 0 24
reg 2018123013 DEN LAC 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123013/2018123013_gtd.json 9 23
reg 2018123004 KC OAK 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123004/2018123004_gtd.json 35 3
reg 2018123005 MIN CHI 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123005/2018123005_gtd.json 10 24
reg 2018123000 BAL CLE 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123000/2018123000_gtd.json 26 24
reg 2018123014 LA SF 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123014/2018123014_gtd.json 48 32
reg 2018123009 PIT CIN 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123009/2018123009_gtd.json 16 13
reg 2018123015 SEA ARI 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123015/2018123015_gtd.json 27 24
reg 2018123011 TEN IND 17 2018 POST http://www.nfl.com/liveupdate/game-center/2018123011/2018123011_gtd.json 17 33

That’s all the regular season games in the 2018 season. They suggest that it is straightforward to grab an entire season of play by play data.

full_season_2018 <- scrape_season_play_by_play(2018, "reg")
saveRDS(full_season_2018, file="../../../data/2018NFLSeason.rds")

That gets the data though it took over an hour to acquire it all and it threw two error messages. I do not yet know if they are conseequential. My goal here is to use this package and the ability to plot the win probability charts to try to summarise an entire cowboys season.

full_season_2018 <- readRDS(url("https://github.com/robertwwalker/academic-mymod/raw/master/data/2018NFLSeason.rds")) 

What can I do with it?

When I paid attention the NFL, I followed the Dallas Cowboys. Let’s isolate their data.

dal_season <- full_season_2018 %>% filter(home_team=="DAL" | away_team=="DAL")
dal_season %>%  head()
## # A tibble: 6 x 256
##   play_id game_id home_team away_team posteam posteam_type defteam side_of_field
##   <chr>   <chr>   <chr>     <chr>     <chr>   <chr>        <chr>   <chr>        
## 1 36      201809… CAR       DAL       DAL     away         CAR     CAR          
## 2 51      201809… CAR       DAL       DAL     away         CAR     DAL          
## 3 72      201809… CAR       DAL       DAL     away         CAR     DAL          
## 4 107     201809… CAR       DAL       DAL     away         CAR     DAL          
## 5 128     201809… CAR       DAL       DAL     away         CAR     DAL          
## 6 151     201809… CAR       DAL       DAL     away         CAR     DAL          
## # … with 248 more variables: yardline_100 <dbl>, game_date <date>,
## #   quarter_seconds_remaining <dbl>, half_seconds_remaining <dbl>,
## #   game_seconds_remaining <dbl>, game_half <chr>, quarter_end <dbl>,
## #   drive <int>, sp <dbl>, qtr <dbl>, down <fct>, goal_to_go <dbl>, time <chr>,
## #   yrdln <chr>, ydstogo <dbl>, ydsnet <dbl>, desc <chr>, play_type <chr>,
## #   yards_gained <dbl>, shotgun <dbl>, no_huddle <dbl>, qb_dropback <dbl>,
## #   qb_kneel <dbl>, qb_spike <dbl>, qb_scramble <dbl>, pass_length <chr>,
## #   pass_location <chr>, air_yards <dbl>, yards_after_catch <dbl>,
## #   run_location <chr>, run_gap <chr>, field_goal_result <chr>,
## #   kick_distance <dbl>, extra_point_result <chr>, two_point_conv_result <chr>,
## #   home_timeouts_remaining <dbl>, away_timeouts_remaining <dbl>,
## #   timeout <dbl>, timeout_team <chr>, td_team <chr>,
## #   posteam_timeouts_remaining <dbl>, defteam_timeouts_remaining <dbl>,
## #   total_home_score <dbl>, total_away_score <dbl>, posteam_score <dbl>,
## #   defteam_score <dbl>, score_differential <dbl>, posteam_score_post <dbl>,
## #   defteam_score_post <dbl>, score_differential_post <dbl>,
## #   no_score_prob <dbl>, opp_fg_prob <dbl>, opp_safety_prob <dbl>,
## #   opp_td_prob <dbl>, fg_prob <dbl>, safety_prob <dbl>, td_prob <dbl>,
## #   extra_point_prob <dbl>, two_point_conversion_prob <dbl>, ep <dbl>,
## #   epa <dbl>, total_home_epa <dbl>, total_away_epa <dbl>,
## #   total_home_rush_epa <dbl>, total_away_rush_epa <dbl>,
## #   total_home_pass_epa <dbl>, total_away_pass_epa <dbl>, air_epa <dbl>,
## #   yac_epa <dbl>, comp_air_epa <dbl>, comp_yac_epa <dbl>,
## #   total_home_comp_air_epa <dbl>, total_away_comp_air_epa <dbl>,
## #   total_home_comp_yac_epa <dbl>, total_away_comp_yac_epa <dbl>,
## #   total_home_raw_air_epa <dbl>, total_away_raw_air_epa <dbl>,
## #   total_home_raw_yac_epa <dbl>, total_away_raw_yac_epa <dbl>, wp <dbl>,
## #   def_wp <dbl>, home_wp <dbl>, away_wp <dbl>, wpa <dbl>, home_wp_post <dbl>,
## #   away_wp_post <dbl>, total_home_rush_wpa <dbl>, total_away_rush_wpa <dbl>,
## #   total_home_pass_wpa <dbl>, total_away_pass_wpa <dbl>, air_wpa <dbl>,
## #   yac_wpa <dbl>, comp_air_wpa <dbl>, comp_yac_wpa <dbl>,
## #   total_home_comp_air_wpa <dbl>, total_away_comp_air_wpa <dbl>,
## #   total_home_comp_yac_wpa <dbl>, total_away_comp_yac_wpa <dbl>,
## #   total_home_raw_air_wpa <dbl>, total_away_raw_air_wpa <dbl>, …
#  kable()  %>%
#  kable_styling() %>%
#  scroll_box(width = "100%", height = "200px")

Dallas Season

Now I want to try and build a plot of all of Dallas Cowboys games for the season. Here are the steps. First, I am going to make a table that contains all of the names and colors for all of the game IDs. Even though I only need those for Dallas, building a shiny app for this would mean that I could select by teams above and make it extensible. At the end, I will only need those for Dallas so I will separate them off.

all_2018_games %>%  select(home_team, home_score,  away_team, away_score) %>% head()
##   home_team home_score away_team away_score
## 1       PHI         18       ATL         12
## 2       BAL         47       BUF          3
## 3       NYG         15       JAX         20
## 4        NO         40        TB         48
## 5        NE         27       HOU         20
## 6       MIN         24        SF         16

Cool.

ntable <- all_2018_games %>% select(game_id, home_team, away_team)
ntableH <- ntable
ntableH <- ntableH %>% left_join(nflteams, by = c("home_team" = "abbr"))
names(ntableH) <- paste0("Home_",names(ntableH), sep="")
ntableH <- ntableH %>% rename(., game_id = Home_game_id, home_team = Home_home_team, away_team = Home_away_team)
ntableA <- ntable
ntableA <- ntableA %>% left_join(nflteams, by = c("away_team" = "abbr"))
names(ntableA) <- paste0("Away_",names(ntableA), sep="")
ntableA <- ntableA %>% rename(., game_id = Away_game_id, home_team = Away_home_team, away_team = Away_away_team)
My.NFL.Table <- ntableH %>% inner_join(ntableA)
## Joining, by = c("game_id", "home_team", "away_team")
My.NFL.Table <- My.NFL.Table %>% left_join(all_2018_games)
## Joining, by = c("game_id", "home_team", "away_team")
Dallas.Table <- My.NFL.Table %>% filter(home_team=="DAL" | away_team=="DAL")
Dallas.Table %>% head()
##      game_id home_team away_team         Home_team Home_primary Home_secondary
## 1 2018090910       CAR       DAL Carolina Panthers      #0085ca        #000000
## 2 2018091613       DAL       NYG    Dallas Cowboys      #002244        #b0b7bc
## 3 2018092312       SEA       DAL  Seattle Seahawks      #002244        #69be28
## 4 2018093002       DAL       DET    Dallas Cowboys      #002244        #b0b7bc
## 5 2018100712       HOU       DAL    Houston Texans      #03202f        #a71930
## 6 2018101410       DAL       JAX    Dallas Cowboys      #002244        #b0b7bc
##   Home_tertiary Home_quaternary Home_division            Away_team Away_primary
## 1       #bfc0bf         #0085ca     NFC South       Dallas Cowboys      #002244
## 2       #acc0c6         #a5acaf      NFC East      New York Giants      #0b2265
## 3       #a5acaf         #001532      NFC West       Dallas Cowboys      #002244
## 4       #acc0c6         #a5acaf      NFC East        Detroit Lions      #005a8b
## 5       #00071c         #a30d2d     AFC South       Dallas Cowboys      #002244
## 6       #acc0c6         #a5acaf      NFC East Jacksonville Jaguars      #000000
##   Away_secondary Away_tertiary Away_quaternary Away_division type week season
## 1        #b0b7bc       #acc0c6         #a5acaf      NFC East  reg    1   2018
## 2        #a71930       #a5acaf         #012352      NFC East  reg    2   2018
## 3        #b0b7bc       #acc0c6         #a5acaf      NFC East  reg    3   2018
## 4        #b0b7bc       #000000         #004e89     NFC North  reg    4   2018
## 5        #b0b7bc       #acc0c6         #a5acaf      NFC East  reg    5   2018
## 6        #006778       #9f792c         #d7a22a     AFC South  reg    6   2018
##   state_of_game
## 1          POST
## 2          POST
## 3          POST
## 4          POST
## 5          POST
## 6          POST
##                                                                   game_url
## 1 http://www.nfl.com/liveupdate/game-center/2018090910/2018090910_gtd.json
## 2 http://www.nfl.com/liveupdate/game-center/2018091613/2018091613_gtd.json
## 3 http://www.nfl.com/liveupdate/game-center/2018092312/2018092312_gtd.json
## 4 http://www.nfl.com/liveupdate/game-center/2018093002/2018093002_gtd.json
## 5 http://www.nfl.com/liveupdate/game-center/2018100712/2018100712_gtd.json
## 6 http://www.nfl.com/liveupdate/game-center/2018101410/2018101410_gtd.json
##   home_score away_score
## 1         16          8
## 2         20         13
## 3         24         13
## 4         26         24
## 5         19         16
## 6         40          7

One nice little bit of data recovery here, the season schedule and Dallas’s result can be seen below.

Dallas.Table %>% select(game_id,Home_team,home_score, Away_team, away_score)
##       game_id           Home_team home_score            Away_team away_score
## 1  2018090910   Carolina Panthers         16       Dallas Cowboys          8
## 2  2018091613      Dallas Cowboys         20      New York Giants         13
## 3  2018092312    Seattle Seahawks         24       Dallas Cowboys         13
## 4  2018093002      Dallas Cowboys         26        Detroit Lions         24
## 5  2018100712      Houston Texans         19       Dallas Cowboys         16
## 6  2018101410      Dallas Cowboys         40 Jacksonville Jaguars          7
## 7  2018102110 Washington Redskins         20       Dallas Cowboys         17
## 8  2018110500      Dallas Cowboys         14     Tennessee Titans         28
## 9  2018111111 Philadelphia Eagles         20       Dallas Cowboys         27
## 10 2018111800     Atlanta Falcons         19       Dallas Cowboys         22
## 11 2018112201      Dallas Cowboys         31  Washington Redskins         23
## 12 2018112900      Dallas Cowboys         13   New Orleans Saints         10
## 13 2018120912      Dallas Cowboys         29  Philadelphia Eagles         23
## 14 2018121605  Indianapolis Colts         23       Dallas Cowboys          0
## 15 2018122306      Dallas Cowboys         27 Tampa Bay Buccaneers         20
## 16 2018123008     New York Giants         35       Dallas Cowboys         36

Season Won-Loss Records

To derive a season won-loss record for each team, I need to first declare the outcome of each game using the scores and then summarise them over teams. I will also have to pass them on because they do not have this in the nflteams data in nflscrapR.

WLTabs <- all_2018_games %>% mutate(home_win = (home_score > away_score), away_win = (away_score > home_score)) %>% select(game_id, home_team, away_team, home_win, away_win) %>% gather("home_team", "away_team", key="HA", value="abbr")
WLTabs$Wins <- WLTabs$home_win
WLTabs[WLTabs$HA=="away_team","Wins"] <- WLTabs[WLTabs$HA=="away_team","away_win"]
WinsTab <- WLTabs %>% group_by(abbr) %>% summarise(Wins = sum(Wins==TRUE))

Now I want to put together a plot of a season. The way to make it modular is to choose a team and program the rest as a function with that as input. The hack that I undertook is recalibrating the time remaining to take account of the mess of overtime games.

season_animator <- function(teamName, data1=full_season_2018, data2=all_2018_games, nflteams = nflscrapR::nflteams) {
  # This creates the team color vector
  NFL.colors <- nflteams %>% select(primary)
  NFL.colors <- as.vector(NFL.colors$primary)
  names(NFL.colors) <- nflteams$abbr
  NFL.colorsS <- nflteams %>% select(secondary)
  NFL.colorsS <- as.vector(NFL.colorsS$secondary)
  names(NFL.colorsS) <- nflteams$abbr
  # Now create the title and wins
  WLTabs <- all_2018_games %>% mutate(home_win = (home_score > away_score), away_win = (away_score > home_score)) %>% select(game_id, home_team, away_team, home_win, away_win) %>% gather("home_team", "away_team", key="HA", value="abbr")
  WLTabs$Wins <- WLTabs$home_win
  WLTabs[WLTabs$HA=="away_team","Wins"] <- WLTabs[WLTabs$HA=="away_team","away_win"]
  WinsTab <- WLTabs %>% group_by(abbr) %>% summarise(Wins = sum(Wins==TRUE))
  My.Wins <- WinsTab %>% filter(abbr==teamName) %>% select(Wins) %>% as.integer()
  title.ST <- nflteams %>% filter(abbr==teamName) %>% select(team) %>% as.character()
  title.My <- paste0("2018 ",title.ST,": ",My.Wins," Wins")
  ntable <- all_2018_games %>% select(game_id, home_team, away_team)
  ntableH <- ntable
  ntableH <- ntableH %>% left_join(nflteams, by = c("home_team" = "abbr"))
  names(ntableH) <- paste0("Home_",names(ntableH), sep="")
  ntableH <- ntableH %>% rename(., game_id = Home_game_id, home_team = Home_home_team, away_team = Home_away_team)
  ntableA <- ntable
  ntableA <- ntableA %>% left_join(nflteams, by = c("away_team" = "abbr"))
  names(ntableA) <- paste0("Away_",names(ntableA), sep="")
  ntableA <- ntableA %>% rename(., game_id = Away_game_id, home_team = Away_home_team, away_team = Away_away_team)
  My.NFL.Table <- ntableH %>% inner_join(ntableA)
  My.NFL.Table <- My.NFL.Table %>% left_join(all_2018_games)
  # Select off the team dataset
  team_season <- full_season_2018 %>% filter(home_team==teamName | away_team==teamName)
  # Fix GSR
  team_season[team_season$game_half=="Overtime","game_seconds_remaining"] <- -1*(600 - team_season[team_season$game_half=="Overtime","game_seconds_remaining"])
  team_wp <- team_season %>% 
    filter(!is.na(home_wp),!is.na(away_wp)) %>% 
    unite(GIDGSR, game_seconds_remaining, game_id, sep=":")
  team_wp <- team_wp %>% 
    dplyr::select(GIDGSR,
                  home_wp,
                  away_wp)  %>%
    gather(team, wpa, -GIDGSR) %>% 
    separate(., GIDGSR, c("GSR", "game_id"), sep=":") %>% 
    mutate(game_seconds_remaining = as.integer(GSR))
  team_plt <- team_wp %>% left_join(My.NFL.Table)
  team_plt <- team_plt %>% left_join(data2)
  team_plt[team_plt$team=="home_wp","team"] <- team_plt[team_plt$team=="home_wp","home_team"]
  team_plt[team_plt$team=="away_wp","team"] <- team_plt[team_plt$team=="away_wp","away_team"]
  team_plt$dateG <- substring(team_plt$game_id, 5, 8)
  team_plt$titleS <- with(team_plt, paste0(dateG,": ",Home_team," (",home_score,") v. ", Away_team ," (",away_score,")"))
  Mini.GSR <- team_plt %>% group_by(game_id, team) %>% slice( n() ) %>% ungroup() %>% mutate(labs1 = team) %>%  select(game_id, team, game_seconds_remaining, labs1)
  team_plt <- team_plt %>% left_join(Mini.GSR)
  team_plt <- team_plt %>% mutate(team2 = team)
  p <- team_plt %>% ggplot() + aes(x = game_seconds_remaining, y = wpa, color = team, fill = team2, label = labs1) +
    geom_line(size = 1.5, na.rm = TRUE) +
    geom_label(size=5, nudge_x = 50, hjust=1.5, fill="white", na.rm=TRUE) +
    geom_point(shape = 21, size = 1.25, stroke = 0, na.rm=TRUE) +
    scale_color_manual(values = NFL.colors, guide=FALSE) +
    scale_fill_manual(values = NFL.colorsS, guide=FALSE) +
    geom_hline(yintercept = 0.5, color = "gray", linetype = "dashed") +
    #  scale_color_viridis_d(guide=FALSE) +
    scale_x_reverse(breaks = seq(-600, 3600, 300)) + 
    geom_vline(xintercept = 900, linetype = "dotted", colour = "black") + 
    geom_vline(xintercept = 1800, linetype = "dashed", colour = "black") + 
    geom_vline(xintercept = 2700, linetype = "dotted", colour = "black") + 
    geom_vline(xintercept = 0, linetype = "dashed", colour = "black") + 
    geom_vline(xintercept = -600, linetype = "solid", colour = "black") + 
    labs(
      x = "Time Remaining (seconds)",
      y = "Win Probability",
      title=title.My,
      subtitle = "{closest_state}",
      caption = "Data from nflscrapR"
    ) + theme_bw() + theme(text=element_text(size=9, family = "serif"), panel.background = element_rect(fill = "#d1e0e0")) + transition_states(titleS, transition_length=8, state_length = 15)
  animate(p, nframes=450)
}
season_animator("DAL")

I decided it was time to experiment with shiny apps again and so I built one out of this. Check it out!. Overtime games make things a bit messy. I finally managed to fix that, both above and in the Shiny by taking game time remaining and turning the overtime seconds into negatives; that made sense given the flipped scale.

Run and Pass?

What does Dallas do with the ball?

p <- dal_season %>%  filter(posteam=="DAL") %>% ggplot(aes(x=play_type, fill=play_type)) + geom_bar() + scale_fill_viridis_d(guide=FALSE) + transition_states(game_id) + labs(title = "{closest_state}", x="Type of Play") + theme_economist_white()
animate(p)

That’s kinda cool. Now I want to clean that up. I do not really want to look at most of the types of possession plays; no play, pass, and run seem interesting, though. Let’s try that out. I also want to change up the display a little bit. Instead of looking at it as a series of graphics, I want the bars to represent the percents from the previous type table and then to show the whole season as it arrives.

dal_RP <- dal_season  %>% filter(posteam=="DAL" & play_type %in% c("no_play","run","pass")) %>% tabyl(game_date, play_type)
dal_RP <-  dal_RP %>% gather("play_type", "count", pass,run,no_play)
p <- dal_RP %>% ggplot(aes(x=game_date, y=count, color=play_type)) + geom_line() + scale_color_viridis_d() +  labs(title = "{frame_along}", y="Number of Plays", color="Play Type", x="Game Date") + theme_economist_white() + geom_point(aes(group=seq_along(game_date), size=1.5, alpha=0.5)) + guides(size=FALSE, alpha=FALSE) + transition_reveal(game_date) 
animate(p)

I think that more or less gets what I want. This might also be a cool case for a simple plotly line plot with a hover for the game and details.