library(nflfastR)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(dplyr)
library(RColorBrewer)
library(gridExtra)
##
## Attaching package: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
pbp <- nflfastR::load_pbp(2018:2020)
Q4 <- pbp%>%
filter(qtr == 4)%>%
filter(play_type == "pass"| play_type == "run")
ggplot(data = Q4)+
geom_bin2d(aes(x = yards_gained, y = ydstogo))+
scale_color_viridis_c()+
scale_x_continuous(name = "Yards Gained")+
scale_y_continuous(name = "Yards to go")

g1 <- pbp%>%
filter(play_type == "pass"| play_type == "run")%>%
filter((home_team == "CIN"&away_team == "PIT")
|(home_team == "PIT"&away_team == "CIN"))%>%
ggplot()+
geom_bar(aes(x = posteam, fill = play_type), position = "dodge")+
scale_fill_manual(name = "Play Type", values = c("orange", "black"))+
scale_x_discrete(name = "Team")+
scale_y_continuous(name = "Play Type")
g2 <- pbp%>%
filter(play_type == "pass"| play_type == "run")%>%
filter((home_team == "BAL"&away_team == "CIN")
|(home_team == "CIN"&away_team == "BAL"))%>%
ggplot()+
geom_bar(aes(x = fct_relevel(posteam, "CIN", "BAL"), fill = play_type), position = "dodge")+
scale_fill_manual(name = "Play Type", values = c("orange", "purple"))+
scale_x_discrete(name = "Team")+
scale_y_continuous(name = "Play Type")
g3 <- pbp%>%
filter(play_type == "pass"| play_type == "run")%>%
filter((home_team == "CLE"& away_team == "CIN")
|(home_team == "CIN"& away_team == "CLE"))%>%
ggplot()+
geom_bar(aes(x = posteam, fill = play_type), position = "dodge")+
scale_fill_manual(name = "Play Type", values = c("orange", "brown"))+
scale_x_discrete(name = "Team")+
scale_y_continuous(name = "Play Type")
grid.arrange(g1, g2, g3, ncol = 3)
