Skip to content

Instantly share code, notes, and snippets.

@tanho63
Last active September 29, 2023 18:20
Show Gist options
  • Save tanho63/f7ad960a1f2d1779f694f9b6289b1619 to your computer and use it in GitHub Desktop.
Save tanho63/f7ad960a1f2d1779f694f9b6289b1619 to your computer and use it in GitHub Desktop.
rb situation usage plot
rb_opps <- data.table::rbindlist(
list(
nflreadr::load_ff_opportunity(stat_type = "pbp_rush"),
nflreadr::load_ff_opportunity(stat_type = "pbp_pass")
),
fill = TRUE) |>
dplyr::filter(position == "RB" | receiver_position == "RB", week == 3, posteam == "MIA") |>
dplyr::transmute(
game_id,
player_name = dplyr::coalesce(receiver_full_name, full_name),
player_position = dplyr::coalesce(receiver_position, position),
team = posteam,
fixed_drive,
game_seconds_remaining,
half_seconds_remaining,
vegas_wp,
down,
ydstogo,
yardline_100,
xfp =
tidyr::replace_na(rush_yards_exp, 0) * 0.1 +
tidyr::replace_na(yardline_100 - yardline_exp, 0) * 0.1 +
tidyr::replace_na(pass_completion_exp, 0) * 1 +
tidyr::replace_na(rushing_td_exp, 0) * 6 +
tidyr::replace_na(pass_touchdown_exp, 0) * 6 +
0
,
opp_type = dplyr::case_when(
yardline_100 <= 5 ~ "Inside 5 Yards",
half_seconds_remaining <= 60 * 2 ~ "Two Minute Drill",
down %in% c(3,4) & ydstogo >= 5 ~ "3rd/4th down and 5+ ytg",
TRUE ~ "Normal Opp"
)
) |>
dplyr::mutate(
player_name = forcats::fct_reorder(player_name, xfp, .fun = sum) |> forcats::fct_rev(),
opp_type = forcats::fct_relevel(
opp_type,
"3rd/4th down and 5+ ytg",
"Two Minute Drill",
"Inside 5 Yards",
"Normal Opp"
) |>
forcats::fct_rev()
)
rb_opps |>
ggplot2::ggplot(
ggplot2::aes(
x = game_seconds_remaining,
y = vegas_wp,
color = player_name,
shape = opp_type,
size = xfp
)
) +
# slight jitter in case size causes overlaps
ggplot2::geom_jitter(alpha = 0.8) +
# add lines for the start of every possession
ggplot2::geom_vline(
ggplot2::aes(xintercept = game_seconds_remaining),
linetype = 2,
alpha = 0.2,
data = rb_opps |>
dplyr::slice_max(game_seconds_remaining, by = fixed_drive) |>
dplyr::mutate(game_seconds_remaining = game_seconds_remaining + 10)
) +
ggplot2::theme_minimal() +
ggplot2::scale_x_reverse() +
ggplot2::scale_y_continuous(labels = scales::percent_format()) +
ggplot2::scale_color_brewer(palette = "Dark2") +
ggplot2::labs(
title = "RB Situational Usage",
subtitle = "MIA Week 3",
size = "xFP",
color = "Player",
shape = "Opportunity Type"
)
@tanho63
Copy link
Author

tanho63 commented Sep 29, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment