R Markdown

This is an R Markdown document.

Install packages

#install.packages("tidyverse", type = "binary")
#install.packages("ggrepel", type = "binary")
#install.packages("ggimage", type = "binary")
#install.packages("nflfastR", type = "binary")

Load packages

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggrepel)
library(ggimage)
library(nflfastR)
options(scipen = 9999)

Load data

This will load the full play by play for the 2019 season

data <- load_pbp(2021)
df <- data %>%
  select(play_id, game_id, home_team, away_team,season_type,week,posteam,posteam_type,defteam,side_of_field,rush,pass,play_type,yardline_100,game_date, game_seconds_remaining,game_half,qtr,desc,play_type,yards_gained,pass_length,pass_location,air_yards,td_team,td_player_name,td_player_name,total_home_score,total_away_score, posteam_score,defteam_score,ep,epa,total_home_epa,total_away_epa,total_home_rush_epa,total_away_rush_epa,total_home_pass_epa,total_away_pass_epa,incomplete_pass,interception,fumble_forced,fumble_out_of_bounds,sack,touchdown,pass_touchdown,rush_touchdown,fumble, complete_pass,passer_player_name,passing_yards,receiver_player_name,receiving_yards,rusher_player_name,rushing_yards,season,stadium,play_type_nfl,away_score,home_score, location, result, total,play,qb_epa)
head(df)
## # A tibble: 6 × 63
##   play_id game_id     home_team away_team season_type  week posteam posteam_type
##     <dbl> <chr>       <chr>     <chr>     <chr>       <int> <chr>   <chr>       
## 1       1 2021_01_AR… TEN       ARI       REG             1 <NA>    <NA>        
## 2      40 2021_01_AR… TEN       ARI       REG             1 TEN     home        
## 3      55 2021_01_AR… TEN       ARI       REG             1 TEN     home        
## 4      76 2021_01_AR… TEN       ARI       REG             1 TEN     home        
## 5     100 2021_01_AR… TEN       ARI       REG             1 TEN     home        
## 6     122 2021_01_AR… TEN       ARI       REG             1 TEN     home        
## # … with 55 more variables: defteam <chr>, side_of_field <chr>, rush <dbl>,
## #   pass <dbl>, play_type <chr>, yardline_100 <dbl>, game_date <chr>,
## #   game_seconds_remaining <dbl>, game_half <chr>, qtr <dbl>, desc <chr>,
## #   yards_gained <dbl>, pass_length <chr>, pass_location <chr>,
## #   air_yards <dbl>, td_team <chr>, td_player_name <chr>,
## #   total_home_score <dbl>, total_away_score <dbl>, posteam_score <dbl>,
## #   defteam_score <dbl>, ep <dbl>, epa <dbl>, total_home_epa <dbl>, …
dim(df)
## [1] 40196    63
names(df)
##  [1] "play_id"                "game_id"                "home_team"             
##  [4] "away_team"              "season_type"            "week"                  
##  [7] "posteam"                "posteam_type"           "defteam"               
## [10] "side_of_field"          "rush"                   "pass"                  
## [13] "play_type"              "yardline_100"           "game_date"             
## [16] "game_seconds_remaining" "game_half"              "qtr"                   
## [19] "desc"                   "yards_gained"           "pass_length"           
## [22] "pass_location"          "air_yards"              "td_team"               
## [25] "td_player_name"         "total_home_score"       "total_away_score"      
## [28] "posteam_score"          "defteam_score"          "ep"                    
## [31] "epa"                    "total_home_epa"         "total_away_epa"        
## [34] "total_home_rush_epa"    "total_away_rush_epa"    "total_home_pass_epa"   
## [37] "total_away_pass_epa"    "incomplete_pass"        "interception"          
## [40] "fumble_forced"          "fumble_out_of_bounds"   "sack"                  
## [43] "touchdown"              "pass_touchdown"         "rush_touchdown"        
## [46] "fumble"                 "complete_pass"          "passer_player_name"    
## [49] "passing_yards"          "receiver_player_name"   "receiving_yards"       
## [52] "rusher_player_name"     "rushing_yards"          "season"                
## [55] "stadium"                "play_type_nfl"          "away_score"            
## [58] "home_score"             "location"               "result"                
## [61] "total"                  "play"                   "qb_epa"
df_dallas <- df %>%
    filter(posteam == "DAL") %>%
#  select (game_id, game_date, home_team, away_team) 
  filter(game_id == '2021_01_DAL_TB')
head(df_dallas)
## # A tibble: 6 × 63
##   play_id game_id     home_team away_team season_type  week posteam posteam_type
##     <dbl> <chr>       <chr>     <chr>     <chr>       <int> <chr>   <chr>       
## 1     137 2021_01_DA… TB        DAL       REG             1 DAL     away        
## 2     166 2021_01_DA… TB        DAL       REG             1 DAL     away        
## 3     187 2021_01_DA… TB        DAL       REG             1 DAL     away        
## 4     211 2021_01_DA… TB        DAL       REG             1 DAL     away        
## 5     232 2021_01_DA… TB        DAL       REG             1 DAL     away        
## 6     253 2021_01_DA… TB        DAL       REG             1 DAL     away        
## # … with 55 more variables: defteam <chr>, side_of_field <chr>, rush <dbl>,
## #   pass <dbl>, play_type <chr>, yardline_100 <dbl>, game_date <chr>,
## #   game_seconds_remaining <dbl>, game_half <chr>, qtr <dbl>, desc <chr>,
## #   yards_gained <dbl>, pass_length <chr>, pass_location <chr>,
## #   air_yards <dbl>, td_team <chr>, td_player_name <chr>,
## #   total_home_score <dbl>, total_away_score <dbl>, posteam_score <dbl>,
## #   defteam_score <dbl>, ep <dbl>, epa <dbl>, total_home_epa <dbl>, …
dim(df_dallas)
## [1] 104  63
glimpse(df_dallas)
## Rows: 104
## Columns: 63
## $ play_id                <dbl> 137, 166, 187, 211, 232, 253, 282, 304, 326, 34…
## $ game_id                <chr> "2021_01_DAL_TB", "2021_01_DAL_TB", "2021_01_DA…
## $ home_team              <chr> "TB", "TB", "TB", "TB", "TB", "TB", "TB", "TB",…
## $ away_team              <chr> "DAL", "DAL", "DAL", "DAL", "DAL", "DAL", "DAL"…
## $ season_type            <chr> "REG", "REG", "REG", "REG", "REG", "REG", "REG"…
## $ week                   <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ posteam                <chr> "DAL", "DAL", "DAL", "DAL", "DAL", "DAL", "DAL"…
## $ posteam_type           <chr> "away", "away", "away", "away", "away", "away",…
## $ defteam                <chr> "TB", "TB", "TB", "TB", "TB", "TB", "TB", "TB",…
## $ side_of_field          <chr> "DAL", "DAL", "DAL", "DAL", "DAL", "DAL", "TB",…
## $ rush                   <dbl> 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ pass                   <dbl> 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1,…
## $ play_type              <chr> "pass", "run", "pass", "run", "run", "pass", "p…
## $ yardline_100           <dbl> 98, 70, 66, 61, 59, 55, 39, 39, 39, 44, 44, 35,…
## $ game_date              <chr> "2021-09-09", "2021-09-09", "2021-09-09", "2021…
## $ game_seconds_remaining <dbl> 3498, 3471, 3443, 3408, 3364, 3331, 3296, 3289,…
## $ game_half              <chr> "Half1", "Half1", "Half1", "Half1", "Half1", "H…
## $ qtr                    <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ desc                   <chr> "(13:18) (Shotgun) 4-D.Prescott pass deep left …
## $ yards_gained           <dbl> 28, 4, 5, 2, 4, 16, 0, 0, 0, 0, 0, 0, 11, 8, 12…
## $ pass_length            <chr> "deep", NA, "short", NA, NA, "short", "deep", "…
## $ pass_location          <chr> "left", NA, "middle", NA, NA, "right", "left", …
## $ air_yards              <dbl> 23, NA, 5, NA, NA, -3, 20, 5, NA, 14, NA, NA, -…
## $ td_team                <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ td_player_name         <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ total_home_score       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7,…
## $ total_away_score       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ posteam_score          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ defteam_score          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7,…
## $ ep                     <dbl> -0.8813698, 1.1836110, 1.0815493, 1.4221726, 1.…
## $ epa                    <dbl> 2.06498082, -0.10206172, 0.34062327, 0.45731990…
## $ total_home_epa         <dbl> -2.6577088, -2.5556471, -2.8962704, -3.3535903,…
## $ total_away_epa         <dbl> 2.6577088, 2.5556471, 2.8962704, 3.3535903, 3.2…
## $ total_home_rush_epa    <dbl> -0.13028923, -0.02822751, -0.02822751, -0.48554…
## $ total_away_rush_epa    <dbl> 0.13028923, 0.02822751, 0.02822751, 0.48554741,…
## $ total_home_pass_epa    <dbl> -3.8035100, -3.8035100, -4.1441332, -4.1441332,…
## $ total_away_pass_epa    <dbl> 3.8035100, 3.8035100, 4.1441332, 4.1441332, 4.1…
## $ incomplete_pass        <dbl> 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0,…
## $ interception           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ fumble_forced          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ fumble_out_of_bounds   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ sack                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ touchdown              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ pass_touchdown         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ rush_touchdown         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ fumble                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ complete_pass          <dbl> 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,…
## $ passer_player_name     <chr> "D.Prescott", NA, "D.Prescott", NA, NA, "D.Pres…
## $ passing_yards          <dbl> 28, NA, 5, NA, NA, 16, NA, NA, NA, NA, NA, NA, …
## $ receiver_player_name   <chr> "A.Cooper", NA, "D.Schultz", NA, NA, "D.Schultz…
## $ receiving_yards        <dbl> 28, NA, 5, NA, NA, 16, NA, NA, NA, NA, NA, NA, …
## $ rusher_player_name     <chr> NA, "E.Elliott", NA, "E.Elliott", "E.Elliott", …
## $ rushing_yards          <dbl> NA, 4, NA, 2, 4, NA, NA, NA, NA, NA, NA, NA, NA…
## $ season                 <int> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,…
## $ stadium                <chr> "Raymond James Stadium", "Raymond James Stadium…
## $ play_type_nfl          <chr> "PASS", "RUSH", "PASS", "RUSH", "RUSH", "PASS",…
## $ away_score             <int> 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,…
## $ home_score             <int> 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,…
## $ location               <chr> "Home", "Home", "Home", "Home", "Home", "Home",…
## $ result                 <int> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,…
## $ total                  <int> 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,…
## $ play                   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,…
## $ qb_epa                 <dbl> 2.06498082, -0.10206172, 0.34062327, 0.45731990…