view code
library(tidyverse)
library(nflreadr)
knitr::opts_chunk$set(message = F, warning = F)
options(nflreadr.verbose = F)
theme_set(theme_classic())
df = load_pbp(seasons = most_recent_season())
df_igami = df |>
filter(desc == "END GAME") |>
select(game_id, total_home_score, total_away_score, home_team, away_team) |>
transmute(game_id, home_team, away_team,
win_score = case_when(total_home_score > total_away_score ~ total_home_score,
total_home_score < total_away_score ~ total_away_score,
total_home_score == total_away_score ~ total_home_score),
lose_score = case_when(total_home_score > total_away_score ~ total_away_score,
total_home_score < total_away_score ~ total_home_score,
total_home_score == total_away_score ~ total_away_score)) |>
mutate(score = paste0(win_score, "-", lose_score))
scores22 = df_igami$score
max_win = max(df_igami$win_score)
max_lose = max(df_igami$lose_score)
fig = crossing(win_score = 0:max_win,
lose_score = 0:max_lose) |>
mutate(score = paste0(win_score, "-", lose_score),
happened = ifelse(score %in% scores22, "yes", "no"),
possible = ifelse(win_score < lose_score, "no", "yes"),
possible = ifelse(score %in% c("1-0", "1-1", "2-1", "3-1", "4-1", "5-1", "7-1"), "no", possible),
color = case_when(happened == "yes" ~ "occurred",
happened == "no" & possible == "yes" ~ "not occurred",
possible == "no" ~ "not possible"),
win_score = factor(win_score, levels = 0:max_win),
lose_score = factor(lose_score, levels = 0:max_lose)) |>
ggplot(aes(win_score, lose_score)) +
geom_point(aes(col = color), shape = "square", size = 2.5) +
scale_color_manual(values = c("grey95", "black", "springgreen4")) +
labs(x = "winning score",
y = "losing score",
title = "2022-2023 scorigami") +
theme(plot.title = element_text(hjust = 0.5),
legend.position = "none",
axis.text = element_text(size = 7.5))
plotly::ggplotly(fig)