Skip to content

Instantly share code, notes, and snippets.

@OTStats
Created January 31, 2025 03:49
Show Gist options
  • Save OTStats/504aba55836b7b84a565a153459c1a09 to your computer and use it in GitHub Desktop.
Save OTStats/504aba55836b7b84a565a153459c1a09 to your computer and use it in GitHub Desktop.
# --
library(tidyverse)
library(ggpattern)
# -- Motivation:
# https://x.com/joedgallagher/status/1436300186188386307
df2 <- read_csv("Downloads/shots_2024 3.csv") %>%
janitor::clean_names()
top_scorers <- df2 %>%
filter(goal == 1) %>%
count(shooter_name, sort = T) %>%
slice(1:9) %>% select(1)
# 1 Leon Draisaitl 31
# 2 Brayden Point 25
# 3 Mikko Rantanen 25
# 4 William Nylander 24
# 5 Kirill Kaprizov 23
# 6 Kyle Connor 23
# 7 Mark Scheifele 23
# 8 Sam Reinhart 23
# 9 Cole Caufield 22
# 10 Jake Guentzel 21
df_goal_spell <-
df2 %>%
# filter(shooter_name %in% c("Tomas Hertl", "Mikko Rantanen")) %>%
inner_join(top_scorers) %>%
select(
shooter_name,
id,
game_id,
xg = x_goal,
goal
) %>%
arrange(game_id, id) %>%
group_by(shooter_name) %>%
mutate(shooter_id = row_number(),
goal_spell = cumsum(goal),
new_xg = if_else(goal == 1, 0, xg)) %>%
group_by(shooter_name, goal_spell) %>%
mutate(goal_spell_xg = cumsum(xg)) %>%
ungroup()
df_goal_spell_for_viz <-
df_goal_spell %>%
group_by(shooter_name, goal_spell) %>%
filter(min(shooter_id) == shooter_id) %>%
mutate(goal_spell_xg = 0,
shooter_id = shooter_id - 1) %>%
bind_rows(df_goal_spell) %>%
arrange(shooter_id)
# mutate(shooter_spell = str_c(shooter_id, '-', goal_spell))
df_goal_spell_for_viz %>%
# filter(shooter_name == "William Nylander") %>%
ggplot(aes(x = shooter_id,
y = goal_spell_xg,
group = goal_spell)) +
geom_area_pattern(pattern = "gradient",
fill = "yellow",
pattern_fill = "yellow",
pattern_fill2 = "red",
pattern_res = 400) +
labs(
x = "Cumulative Shots",
y = "Cumulative xG",
title = "xG accrued without scoring",
subtitle = "NHL 2025",
caption = "Created by @OTStats\nData from @MoneyPuck.com"
) +
scale_y_continuous(expand = c(0, 0)) +
# geom_step() +
# geom_area_pattern(pattern_fill = "red", pattern_fill2 = "yellow") +
facet_wrap(.~shooter_name, scales = "free_x")
# ----- attempt for geom_step
df_goal_spell_for_viz %>%
group_by(shooter_spell) %>%
ggplot(
# aes(x = shooter_id,
# y = goal_spell_xg,
# group = goal_spell
# )
) +
geom_rect_pattern(aes(
xmin = min(shooter_id),
xmax = max(shooter_id),
ymin = 0, ymax = goal_spell_xg,
group = shooter_spell
),
pattern = "gradient",
fill = "yellow",
pattern_fill = "yellow",
pattern_fill2 = "red",
pattern_res = 400) +
facet_wrap(.~shooter_name, scales = "free_x")
# geom_rect(aes(xmin = ctime, xmax = dplyr::lead(ctime),
# ymin = n1, ymax = n2),
# --- Accidental aRt
df_goal_spell %>%
group_by(shooter_name, goal_spell) %>%
filter(min(shooter_id) == shooter_id) %>%
mutate(goal_spell_xg = 0,
shooter_id = shooter_id - 1) %>%
bind_rows(df_goal_spell) %>%
arrange(shooter_id) %>%
mutate(shooter_spell = str_c(shooter_id, '-', goal_spell)) %>%
ggplot(aes(x = shooter_id,
y = goal_spell_xg,
group = goal_spell,
alpha = goal_spell_xg)) +
geom_area_pattern(pattern = "gradient",
fill = "yellow",
pattern_fill = "yellow",
pattern_fill2 = "red",
pattern_res = 600,
show.legend = "none") +
theme_void()
# geom_area_pattern(pattern_fill = "red", pattern_fill2 = "yellow") +
@OTStats
Copy link
Author

OTStats commented Jan 31, 2025

20250119 nhl goal drought

This was originally motivated by a post I saw a few years back from Joe Gallagher (@joedgallagher) on Twitter:

image

@OTStats
Copy link
Author

OTStats commented Jan 31, 2025

Accidental aRt when I was messing around with the outcome:
20250119 goal droughts accidental aRt

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