-
Notifications
You must be signed in to change notification settings - Fork 0
/
scramble-drill.R
304 lines (259 loc) · 11.3 KB
/
scramble-drill.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
library(tidyverse)
library(gganimate)
library(cowplot)
library(repr)
library(gt)
library(fontawesome)
library(ggimage)
options(repr.plot.width=15, repr.plot.height = 10)
df_games <- read_csv("~/nfl-big-data-bowl-2021/games.csv",
col_types = cols())
df_plays <- read_csv("~/nfl-big-data-bowl-2021/plays.csv",
col_types = cols())
df_players <- read_csv("~/nfl-big-data-bowl-2021/players.csv",
col_types = cols())
weeks <- seq(1, 17)
#blank dataframe to store tracking data
df_tracking <- data.frame()
#iterating through all weeks
for(w in weeks){
#temperory dataframe used for reading week for given iteration
df_tracking_temp <- read_csv(paste0("~/nfl-big-data-bowl-2021/week",w,".csv"),
col_types = cols())
#storing temporary dataframe in full season dataframe
df_tracking <- bind_rows(df_tracking_temp, df_tracking)
}
df_merged <- inner_join(df_games,
df_plays,
by = c("gameId" = "gameId"))
df_merged <- inner_join(df_merged,
df_tracking,
by = c("gameId" = "gameId",
"playId" = "playId"))
passArivalEvents <- c('pass_outcome_caught',
'pass_arrived',
'pass_outcome_incomplete',
'pass_outcome_interception',
'pass_outcome_touchdown')
df_distanceToFootball <- df_merged %>%
#determining side of ball
mutate(
sideOfBall = ifelse(((team == "home") & (possessionTeam == homeTeamAbbr)) | ((team == "away") & (possessionTeam == visitorTeamAbbr)), "offense", "defense"),
defensiveTeam = ifelse(possessionTeam == homeTeamAbbr,visitorTeamAbbr, homeTeamAbbr))
nfl_dtf <- df_distanceToFootball %>%
#grouping by game, play and frame
group_by(gameId, playId, frameId) %>%
#checking if football reading is in frame
mutate(footballInPlay = sum(displayName == "Football") > 0) %>%
#using only frames with football marked; some plays its missing
filter(footballInPlay) %>%
#adding x and y location of football as columns
mutate(xFootball = x[displayName == "Football"],
yFootball = y[displayName == "Football"]) %>%
#ungrouping
ungroup() %>%
#grouping by game and play
group_by(gameId, playId) %>%
#selecting frames that contain pass arrival events
filter(event %in% passArivalEvents) %>%
#selecting first frame with in case there are multiple
filter(frameId == min(frameId)) %>%
#calculating distance to football
mutate(
distToFootballAtBallArrival = sqrt((x - xFootball) ^ 2 +
(y - yFootball) ^ 2)
)
# Create dataset with scramble plays only
scramble_drill <- nfl_dtf %>%
filter(typeDropback == "SCRAMBLE_ROLLOUT_LEFT" | typeDropback == "SCRAMBLE_ROLLOUT_RIGHT") %>%
filter(sideOfBall == "defense") %>%
filter(passResult != "S") %>%
filter(displayName != "Football") %>%
filter(distToFootballAtBallArrival <= 2.5) %>%
select(playDescription, epa, typeDropback, passResult, displayName, distToFootballAtBallArrival, defensiveTeam) %>%
group_by(defensiveTeam)
headshot_links <- c("https://a.espncdn.com/i/headshots/nfl/players/full/4038538.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/11366.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/2510863.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/17435.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/15612.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/12527.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3061106.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/2982870.png",
"https://a.espncdn.com/i/headshots/nfl/players/full/3925358.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3051388.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/15806.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/14297.png",
"https://a.espncdn.com/i/headshots/nfl/players/full/13975.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/15812.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3052101.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3895429.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/12691.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/2578378.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3054847.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/2970694.png")
#find closest defenders on scramble drills
closest_defenders <- scramble_drill %>%
select(playId, defensiveTeam, displayName, distToFootballAtBallArrival) %>%
group_by(displayName) %>%
filter(n() >= 3) %>%
summarize(MeanDist = mean(distToFootballAtBallArrival, na.rm = TRUE)) %>%
mutate(AvgDist = round(MeanDist, digits = 2)) %>%
arrange(AvgDist) %>%
mutate(rank = dense_rank(AvgDist)) %>%
head(n = 20) %>%
mutate(head = headshot_links) %>%
select(rank, displayName, head, AvgDist)
# Table for Closest Defenders
closest_defenders %>%
gt() %>%
cols_align(align = "center",
columns = vars(AvgDist)) %>%
tab_options(
data_row.padding = px(2)
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_column_labels(TRUE)
) %>%
tab_header(
title = md("Closest Defenders on Average"),
subtitle = "On scramble plays"
) %>%
cols_label(
rank = "RK",
displayName = "Name",
head = "",
AvgDist = "Distance"
) %>%
opt_all_caps() %>%
tab_options(
table.background.color = "white",
column_labels.background.color = "purple",
#row.striping.background_color = "#e0e0e0"
) %>%
opt_row_striping() %>%
opt_table_font(
font = list(
google_font("Lato"),
default_fonts()
)
) %>%
text_transform(
locations = cells_body(columns = vars(head)),
fn = function(x){
gt::web_image(x)
}
) %>%
data_color(
columns = vars(AvgDist),
colors = scales::col_numeric(
palette = c("#3fc1c9", "white"),
domain = NULL
)
)
# filter Scramble plays by team + add logos
team_scramble_stats <- scramble_drill %>%
select(playId, defensiveTeam, displayName, epa) %>%
group_by(defensiveTeam) %>%
summarize(Mean = mean(epa, na.rm=TRUE)) %>%
mutate(EPA = round(Mean, digits = 2)) %>%
arrange(Mean) %>%
mutate(logo = c("https://a.espncdn.com/i/teamlogos/nfl/500/lac.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/atl.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/det.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/mia.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/phi.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/hou.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/ten.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/sea.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/chi.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/cle.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/tb.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/no.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/min.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/gb.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/den.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/ari.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/nyj.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/ne.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/buf.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/jax.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/cin.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/was.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/dal.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/ind.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/oak.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/kc.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/pit.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/la.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/sf.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/bal.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/car.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/nyg.png"))
asp_ratio <- 1.12
# Plot team stats
team_scramble_stats %>%
ggplot(aes(x = reorder(defensiveTeam, -Mean),
y = Mean)) +
geom_bar(stat = 'identity',
fill = ifelse(team_scramble_stats$Mean < 0, "lightgreen", "firebrick2")) +
coord_flip() +
ggtitle("Average EPA on Scramble Drills by Team") +
theme_bw() +
theme(text = element_text(size=16)) +
geom_text(aes(label = EPA),
nudge_y = ifelse(team_scramble_stats$Mean < 0, 0.13, -0.13)) +
ggthemes::theme_fivethirtyeight() +
theme(axis.title = element_text()) +
xlab('Team') +
ylab("Average EPA") +
geom_image(aes(image = logo),
size = 0.04,
by = "width",
asp = asp_ratio) +
theme(aspect.ratio = 1/asp_ratio)
# filter by player
player_scramble_stats <- scramble_drill %>%
select(playId, defensiveTeam, displayName, epa, distToFootballAtBallArrival) %>%
group_by(displayName) %>%
filter(n() >= 3) %>%
summarize(Mean = mean(epa, na.rm = TRUE)) %>%
mutate(EPA = round(Mean, digits = 2))
# worst 20 players
bad_scramble_stats <- player_scramble_stats %>%
arrange(desc(Mean)) %>%
head(n = 20)
bad_scramble_stats %>%
ggplot(aes(x = reorder(displayName, Mean),
y = Mean)) +
geom_bar(stat = 'identity',
color = "red",
fill = "pink") +
geom_text(aes(label = EPA),
nudge_y = -0.06) +
coord_flip() +
ggtitle("20 Worst Players in Scramble Drill") +
ggthemes::theme_fivethirtyeight() +
theme(axis.title = element_text()) +
ylab('EPA') +
xlab('Player')
# best 20 players
good_scramble_stats <- player_scramble_stats %>%
arrange(desc(-Mean)) %>%
head(n = 20)
good_scramble_stats %>%
ggplot(aes(x = reorder(displayName, -Mean),
y = abs(Mean))) +
geom_bar(stat = 'identity',
color = "darkgreen",
fill = "lightgreen") +
geom_text(aes(label = EPA),
nudge_y = -0.1) +
coord_flip() +
ggtitle("20 Best Players in Scramble Drill") +
ggthemes::theme_fivethirtyeight() +
theme(axis.title = element_text()) +
ylab('EPA') +
xlab('Player')
save.image("scramble_data.RData")