-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext_gen.Rmd
506 lines (423 loc) · 17.1 KB
/
next_gen.Rmd
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
---
title: "NFL Analytics: Next Gen Stats"
output: html_notebook
---
```{r setup, include=TRUE, warning=FALSE, message=FALSE}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
nflverse,
gt,
gtExtras,
gtUtils,
ggfx,
rvest,
httr,
jsonlite,
patchwork,
ggtext,
ggrepel,
ggimage,
dlookr,
showtext,
sysfonts,
janitor,
grid,
ggiraph,
glue,
here
)
sysfonts::font_add_google("Lexend")
showtext::showtext_auto()
```
```{r nfl_analytics_theme, include=TRUE}
nfl_analytics_theme <- function(..., base_size = 12) {
theme(
text = element_text(family = "Lexend", size = base_size, color = "black"),
axis.ticks = element_blank(),
axis.title = element_text(face = "bold"),
axis.text = element_text(face = "bold"),
plot.title.position = "plot",
plot.title = element_markdown(size = 16, vjust = .02, hjust = 0.5),
plot.subtitle = element_markdown(hjust = 0.5),
plot.caption = element_markdown(size = 8),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "#d0d0d0"),
panel.background = element_rect(fill = "#f7f7f7"),
plot.background = element_rect(fill = "#f7f7f7"),
panel.border = element_blank(),
legend.background = element_rect(color = "#F7F7F7"),
legend.key = element_rect(color = "#F7F7F7"),
legend.title = element_text(face = "bold"),
legend.title.align = 0.5,
strip.text = element_text(face = "bold"))
}
```
```{r teams, include=TRUE}
teams <- nflreadr::load_teams()
```
# Passing
```{r ngs_data_passing, include=TRUE}
ngs_data_passing <- nflreadr::load_nextgen_stats(seasons = 2024,
stat_type = "passing") %>%
# week 0 aggregates stats for you
# it looks like that aggregation requires a minimum number of attempts
# could always do that aggregation myself instead of waiting for NGS to do it
filter(week == 0)
names(ngs_data_passing)
```
```{r join_teams, include=TRUE}
ngs_data_passing <- ngs_data_passing %>%
left_join(teams, by = c("team_abbr" = "team_abbr"))
```
```{r warning=FALSE, message=FALSE}
ngs_data_passing %>%
ggplot(aes(x=avg_time_to_throw,
y=avg_intended_air_yards)) +
geom_hline(yintercept = mean(ngs_data_passing$avg_intended_air_yards),
color = "black", size = 0.8, linetype = "dashed") +
geom_vline(xintercept = mean(ngs_data_passing$avg_time_to_throw),
color = "black", size = 0.8, linetype = "dashed") +
geom_point(size = 3.5, color = ngs_data_passing$team_color) +
scale_x_continuous(breaks = scales::pretty_breaks(),
labels = scales::comma_format()) +
scale_y_continuous(breaks = scales::pretty_breaks(),
labels = scales::comma_format()) +
geom_text_repel(aes(label = player_display_name),
family = "Lexend", fontface = "bold", size = 3.5, box.padding = 0.45,) +
# theme_minimal() +
nfl_analytics_theme() +
xlab("Average Time to Throw") +
ylab("Average Intended Air Yards") +
labs(title = "**Avgerage Time to Throw vs. Average Intended Air Yards**")
```
## Pro Football Reference
```{r pfr_stats_pass, include=TRUE}
pfr_stats_pass <- nflreadr::load_pfr_advstats(seasons = 2024,
stat_type = "pass",
summary_level = "season")
pfr_stats_pass$team <- clean_team_abbrs(pfr_stats_pass$team)
pfr_stats_pass <- pfr_stats_pass %>%
left_join(teams, by = c("team" = "team_abbr"))
names(pfr_stats_pass)
```
```{r}
pfr_stats_pass_filtered <- pfr_stats_pass %>%
filter(pass_attempts >= 25)
```
```{r warning=FALSE, message=FALSE}
pfr_stats_pass_filtered %>%
ggplot(aes(x = bad_throw_pct,
y = drop_pct,
alpha = if_else(team == 'PIT', 'PIT', 'Other'))) +
geom_point(color = pfr_stats_pass_filtered$team_color, size = 3.5) +
geom_hline(yintercept = mean(pfr_stats_pass_filtered$drop_pct),
color = "black", linetype = "dashed", size = 0.8) +
geom_vline(xintercept = mean(pfr_stats_pass_filtered$bad_throw_pct),
color = "black", linetype = "dashed", size = 0.8) +
scale_x_continuous(breaks = scales::pretty_breaks()) +
scale_y_continuous(breaks = scales::pretty_breaks()) +
scale_alpha_manual(values = c('PIT' = 1,
'Other' = .2)) +
nfl_analytics_theme() +
geom_text_repel(aes(label = player),
family = "Lexend", fontface = "bold", size = 3.5) +
xlab("Bad Throw %") +
ylab("Drop %") +
labs(title = "**QB Bad Throws % vs. WR Drops %**",
subtitle = "2024 Regular Season<br>Justin Fields highlighted",
caption = "*Source: Pro Football Reference*") +
theme(legend.position = 'none')
```
```{r weekly_qbr, include=TRUE}
weekly_qbr <- nflreadr::load_espn_qbr(seasons = most_recent_season(),
summary_type = 'week')
```
# Rushing
```{r ngs_data_rushing, include=TRUE}
ngs_data_rushing <- nflreadr::load_nextgen_stats(seasons = 2024,
stat_type = "rushing") %>%
filter(week == 0) %>%
mutate(actual_rush_yards = expected_rush_yards + rush_yards_over_expected)
names(ngs_data_rushing)
```
```{r join_teams2, include=TRUE}
ngs_data_rushing <- ngs_data_rushing %>%
left_join(teams, by = c("team_abbr" = "team_abbr"))
```
```{r}
ngs_data_rushing %>%
arrange(-percent_attempts_gte_eight_defenders)
```
```{r warning=FALSE, message=FALSE}
ngs_data_rushing %>%
ggplot(aes(x = rush_yards_over_expected_per_att,
y = actual_rush_yards,
alpha = if_else(team_abbr == 'PIT', 'PIT', 'Other'))) +
geom_smooth(method = lm, se = FALSE,
color = "black",
size = 0.8,
linetype = "dashed") +
geom_point(size = 3.5, color = ngs_data_rushing$team_color) +
scale_x_continuous(breaks = scales::pretty_breaks(),
labels = scales::comma_format()) +
scale_y_continuous(breaks = scales::pretty_breaks(),
labels = scales::comma_format()) +
geom_text_repel(aes(label = player_display_name),
family = "Lexend",
fontface = "bold", size = 3.5) +
scale_alpha_manual(values = c('PIT' = 1,
'Other' = .2)) +
nfl_analytics_theme() +
xlab("Rush Yards Over Expected per Attempt") +
ylab("Actual Rush Yards") +
labs(title = "Rush Yards Over Expected per Attempt vs. Actual Rush Yards",
subtitle = "2024 Regular Season through Week 7") +
theme(legend.position = 'none')
```
## Pro Football Reference
```{r pfr_stats_rush, include=TRUE}
pfr_stats_rush <- nflreadr::load_pfr_advstats(seasons = 2024,
stat_type = "rush",
summary_level = "season")
pfr_stats_rush$tm <- clean_team_abbrs(pfr_stats_rush$tm)
pfr_stats_rush <- pfr_stats_rush %>%
left_join(teams, by = c("tm" = "team_abbr"))
names(pfr_stats_rush)
```
```{r pfr_stats_rush_filtered, include=TRUE}
pfr_stats_rush_filtered <- pfr_stats_rush %>%
filter(pos == 'RB',
att >= 40)
```
```{r}
pfr_stats_rush_filtered %>%
ggplot(aes(x = ybc, y = yac,
alpha = if_else(tm == 'PIT', 'PIT', 'Other'))) +
geom_hline(yintercept = mean(pfr_stats_rush_filtered$yac),
color = "black", linetype = "dashed", size = 0.8) +
geom_vline(xintercept = mean(pfr_stats_rush_filtered$ybc),
color = "black", linetype = "dashed", size = 0.8) +
geom_point(color = pfr_stats_rush_filtered$team_color, size = 3.5) +
scale_x_continuous(breaks = scales::pretty_breaks()) +
scale_y_continuous(breaks = scales::pretty_breaks()) +
nfl_analytics_theme() +
geom_text_repel(aes(label = player),
family = "Lexend", fontface = "bold", size = 3.5, box.padding = .5) +
scale_alpha_manual(values = c('PIT' = 1,
'Other' = .2)) +
labs(x='Yards Before Contact',
y='Yards After Contact',
title = "**Running Backs: Yards Before and After Contact**<br>Minimum 40 carries",
subtitle = "2024 Regular Season",
caption = "*Source: Pro Football Reference*") +
theme(legend.position = 'none')
```
```{r}
pfr_stats_rush_filtered %>%
ggplot(aes(x = ybc, y = yds)) +
geom_hline(yintercept = mean(pfr_stats_rush_filtered$yds),
color = "black", linetype = "dashed", size = 0.8) +
geom_vline(xintercept = mean(pfr_stats_rush_filtered$ybc),
color = "black", linetype = "dashed", size = 0.8) +
geom_point(color = pfr_stats_rush_filtered$team_color, size = 3.5) +
scale_x_continuous(breaks = scales::pretty_breaks()) +
scale_y_continuous(breaks = scales::pretty_breaks()) +
nfl_analytics_theme() +
geom_text_repel(aes(label = player),
family = "Lexend", fontface = "bold", size = 3.5, box.padding = .5) +
labs(x='Yards Before Contact',
y='Total Yards',
title = "**Running Backs: Total Yards vs. Yards Before Contact**<br>Minimum 40 carries",
subtitle = "2024 Regular Season",
caption = "*Source: Pro Football Reference*")
```
# Receiving
```{r pfr_stats_rec, include=TRUE}
pfr_stats_rec <- nflreadr::load_pfr_advstats(seasons = 2024, stat_type = "rec",
summary_level = "season")
pfr_stats_rec <- pfr_stats_rec %>%
left_join(teams, by = c("tm" = "team_abbr"))
names(pfr_stats_rec)
```
```{r pfr_stats_rec_filtered, include=TRUE}
pfr_stats_rec_filtered <- pfr_stats_rec %>%
filter(rec >= 20 |
tm == 'PIT' & rec > 5)
pfr_stats_rec_filtered_pit <- pfr_stats_rec %>%
filter(tm == 'PIT')
```
The below graphic compares the amount of yards at catch with the yards gained after catch.
```{r message=FALSE, warning=FALSE, echo=FALSE}
pfr_stats_rec_filtered %>%
ggplot(.,
aes(x=ybc,
y=yac,
alpha=if_else(tm == 'PIT', 'PIT', 'Other'))) +
geom_point(color = pfr_stats_rec_filtered$team_color, size=3.5) +
geom_text_repel(aes(label = player),
family='Lexend', fontface='bold', size=3.5, box.padding = .5) +
scale_alpha_manual(values = c('PIT' = 1,
'Other' = .2)) +
labs(x='Yards Before Catch',
y='Yards After Catch') +
nfl_analytics_theme() +
theme(legend.position = 'none')
```
Najee Harris has been a weapon (through Week 5) as a pass catcher out of the backfield. His average depth of target has been behind the line of scrimmage, and he's led the league in YAC per reception (the only player to exceed 10 yards after the catch per reception).
```{r warning=FALSE, message=FALSE}
pfr_stats_rec_filtered %>%
ggplot(.,
aes(x=adot,
y=yac_r,
alpha=if_else(tm == 'PIT', 'PIT', 'Other'))) +
geom_point(color=pfr_stats_rec_filtered$team_color, size=3.5) +
geom_text_repel(aes(label = player),
family='Lexend', fontface='bold', size=3.5, box.padding = .5) +
labs(x='Average Depth of Target',
y='Yards After Catch per Reception',
caption='*Source: Pro Football Reference*<br>Steelers: minimum 5 receptions<br>Rest of League: minimum 20 receptions') +
ggtitle('Receiving: Average Depth of Target vs. YAC per Reception',
subtitle = '2024 Season<br>Highlighting Steelers') +
nfl_analytics_theme() +
theme(legend.position = 'none')
```
When looking at all Steelers pass catchers, some elements stand out:
* Pittsburgh seems to use personnel at different levels -- which is probably also common around the league. Backs are typically catching the ball behind the line of scrimmage. Tight ends and slot options are averaging around 5 yards depth of target. Wide outs are averaging a depth of target beyond 10 yards.
* Connor Heyward is getting some greater depth in his targeting than other TEs (buoyed by his targets against the Cowboys). He's getting some switch and wheel opportunities that Fields has targeted.
* George Pickens needs to be put in some situations to create after the catch. In the 2023 season, Pickens averaged over 6 yards after catch. So far in 2024, he's averaging less than 3 yards after the catch.
```{r pit_rec_2024_plot}
pit_rec_2024_plot <- pfr_stats_rec_filtered_pit %>%
ggplot(.,
aes(x=adot,
y=yac_r,
alpha=rec))+
geom_point(color=pfr_stats_rec_filtered_pit$team_color, size=3.5) +
geom_text_repel(aes(label = player),
family='Lexend', fontface='bold', size=3.5, box.padding = .5) +
labs(x='Average Depth of Target',
y='Yards After Catch per Reception',
subtitle = '**2024 Season**',
caption='*Source: Pro Football Reference*<br>Color intensity by total receptions') +
nfl_analytics_theme() +
theme(legend.position = 'none')
```
```{r warning=FALSE, message=FALSE}
pfr_stats_rec_2023 <- nflreadr::load_pfr_advstats(seasons = 2023, stat_type = "rec",
summary_level = "season")
pit_rec_2023_plot <- pfr_stats_rec_2023 %>%
filter(tm == 'PIT') %>%
ggplot(.,
aes(x=adot,
y=yac_r,
alpha=rec))+
geom_point(color=pfr_stats_rec_filtered_pit$team_color, size=3.5) +
geom_text_repel(aes(label = player),
family='Lexend', fontface='bold', size=3.5, box.padding = .5) +
labs(x='Average Depth of Target',
y='Yards After Catch per Reception',
subtitle = '**2023 Season**',
caption='*Source: Pro Football Reference*<br>Color intensity by total receptions') +
nfl_analytics_theme() +
theme(legend.position = 'none')
```
```{r warning=FALSE, message=FALSE, fig.width=10}
pit_rec_2023_plot + pit_rec_2024_plot +
plot_annotation(title = '**Steelers Receiving**: Average Depth of Target vs. YAC per Reception',
theme(plot.title = element_markdown()))
```
```{r pfr_stats_rec_filtered_pit, include=TRUE}
pfr_stats_rec_filtered_pit %>%
ggplot(.,
aes(x=ybc,
y=yac,
alpha=rec)) +
geom_point(color = 'black', size=3.5) +
geom_text_repel(aes(label = player),
family='Lexend', fontface='bold', size=3.5, box.padding = .5) +
nfl_analytics_theme() +
theme(legend.position = 'none')
```
# Defensive stats
## Pro Football Reference
```{r pfr_stats_def, include=TRUE}
pfr_stats_def <- nflreadr::load_pfr_advstats(seasons = 2024,
stat_type = "def",
summary_level = "season")
pfr_stats_def <- pfr_stats_def %>%
left_join(teams, by = c("tm" = "team_abbr"))
names(pfr_stats_def)
```
```{r pfr_stats_def_filtered_cb, include=TRUE}
pfr_stats_def_filtered_cb <- pfr_stats_def %>%
filter(pos == 'CB',
tgt >= 20)
```
```{r}
pfr_stats_def_filtered_cb %>%
filter(tm == 'PIT')
```
```{r warning=FALSE, message=FALSE}
pfr_stats_def_filtered_cb %>%
ggplot(.,
aes(x=yds_cmp,
y=rat,
alpha=if_else(tm %in% c('PIT', 'WAS'), 'PIT/WAS', 'Other'))) +
geom_point(color=pfr_stats_def_filtered_cb$team_color, size=3.5) +
geom_text_repel(aes(label = player),
familiy='Lexend', fontface='bold', size=3.5, box.padding = .5) +
labs(y='QB rating allowed',
x='Yards per completion') +
scale_alpha_manual(values = c('PIT/WAS' = 1,
'Other' = .2)) +
nfl_analytics_theme() +
theme(legend.position = 'none')
```
# FTN Charting
```{r ftn_data, include=TRUE}
ftn_data <- nflreadr::load_ftn_charting(2024)
play_by_play_2024 <- load_pbp(seasons = most_recent_season())
```
```{r join_pbp_ftn, include=TRUE}
play_by_play_2024 <- play_by_play_2024 %>%
left_join(ftn_data, by = c("game_id" = "nflverse_game_id",
"play_id" = "nflverse_play_id"))
```
This is copied from the book:
```{r}
airyards_accu <- play_by_play_2024 %>%
filter(season_type == "REG") %>%
filter(complete_pass == 1 | incomplete_pass == 1, !is.na(down)) %>%
group_by(passer) %>%
mutate(total_passes = n()) %>%
filter(total_passes >= 25) %>%
ungroup() %>%
group_by(passer, posteam, air_yards) %>%
summarise(
total_att = n(),
total_catchable = sum(is_catchable_ball == "TRUE", na.rm = TRUE),
total_pct = total_catchable / total_att,
.groups = 'drop') %>%
filter(air_yards >= 0 & air_yards <= 25)
```
```{r}
airyards_accu <- airyards_accu %>%
left_join(teams, by = c("posteam" = "team_abbr"))
airyards_accu <- airyards_accu %>%
filter(passer %in% c("L.Jackson", "D.Watson", "J.Fields", "J.Burrow", "R.Wilson"))
```
```{r}
ggplot(data = airyards_accu, aes(x = air_yards, y = total_pct)) +
geom_smooth(se = FALSE, aes(color = posteam)) +
nflplotR::scale_color_nfl(type = "primary") +
scale_x_continuous(breaks = scales::pretty_breaks()) +
scale_y_continuous(breaks = scales::pretty_breaks(),
labels = scales::label_percent()) +
facet_wrap(vars(passer), scales = "free_y") +
xlab("Air Yards") +
ylab("Total % of Catchable Attempts") +
labs(title = "**Total % of Catchable Attempts Based on Air Yards**",
subtitle = "*Minimum 100 Attempts | Through Week 5 of 2023 Season*",
caption = "*Introduction to NFL Analytics With R*<br>**Bradley J. Congelio**") +
nfl_analytics_theme()
```