Skip to content

Instantly share code, notes, and snippets.

@OTStats
Created February 3, 2024 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OTStats/672d9c57da067c5eb85ce8d80a6efeb9 to your computer and use it in GitHub Desktop.
Save OTStats/672d9c57da067c5eb85ce8d80a6efeb9 to your computer and use it in GitHub Desktop.
# -- Created by Owen Thompson (@OTStats)
# 2024-02-01
# -- Load libraries
library(tidyverse)
library(ggpath)
# -- Import data
# Data downloaded on 2/2/24 from Moneypuck.com
df <- read_csv("Downloads/shots_2023 5.csv")
# Data transformation
shot_df <-
df %>%
transmute(
game_id,
period,
team = homeTeamCode,
opp = awayTeamCode,
team_goals = homeTeamGoals,
opp_goals = awayTeamGoals,
time
) %>%
bind_rows(
df %>%
transmute(
game_id,
period,
opp = homeTeamCode,
team = awayTeamCode,
opp_goals = homeTeamGoals,
team_goals = awayTeamGoals,
time
)
)
raw_game_goals_df <- shot_df %>%
group_by(game_id, team, opp) %>%
filter(max(time) == time) %>%
distinct() %>%
group_by(team) %>%
mutate(game_number = row_number(),
goal_diff = team_goals - opp_goals,
cum_goal_diff = cumsum(goal_diff))
# Set starting
team_start_df <-
raw_game_goals_df %>%
distinct(team) %>%
transmute(team,
game_number = 0,
cum_goal_diff = 0)
game_goals_df <- bind_rows(team_start_df, game_goals_df) %>%
arrange(team, game_number)
# -- Pull in NHL logos
# Courtesy of Ivo Villanueva CSV
# Need to manually adjust some team abbrevation in order to join tables
data_git_nhl <- read.csv("https://raw.githubusercontent.com/IvoVillanueva/NHL/main/dataNHL.csv")
logos_df <-
data_git_nhl %>%
transmute(
team = str_to_upper(url_abr),
logo = espn_logo,
primary
) %>%
distinct() %>%
add_row(team = "SEA") %>%
mutate(
team =
case_when(
team == "TB" ~ "TBL",
team == "VGS" ~ "VGK",
team == "LA" ~ "LAK",
team == "NJ" ~ "NJD",
team == "SJ" ~ "SJS",
team == "SEA" ~ "SEA",
TRUE ~ team
)
) %>%
mutate(logo = case_when(
team == "VGK" ~ "https://a.espncdn.com/i/teamlogos/nhl/500/vgs.png",
team == "SEA" ~ "https://a.espncdn.com/i/teamlogos/nhl/500/sea.png",
TRUE ~ logo))
# # Validate logos for each team
# shot_df %>%
# distinct(team) %>%
# left_join(logos_df)
# Set logo URLs to most recent game number
plot_image_df <-
game_goals_df %>%
group_by(team) %>%
filter(max(game_number) == game_number) %>%
left_join(logos_df)
# -- Plot image
game_goals_df %>%
left_join(logos_df) %>%
ggplot(aes(x = game_number, y = cum_goal_diff, group = team)) +
geom_hline(yintercept = 0) +
geom_line(aes(color = primary), linewidth = 1) +
scale_color_identity() +
geom_from_path(
data = plot_image_df,
aes(x = game_number, y = cum_goal_diff, path = logo), width = 0.03
) +
labs(
title = "Cumulative Goal Differential by Team",
subtitle = "2024 Regular Season | Data as of 2/1/24",
x = "Game Number",
y = "Cumulative Goal Differential",
caption = "Viz: @OTStats | Data: Moneypuck.com"
) +
scale_x_continuous(minor_breaks = NULL) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, size = 12),
axis.title = element_text(face = "bold")
)
ggplot2::ggsave(filename = "20240201 Cumulative Goal Differential by NHL Team.png", width = 5, height = 3, dpi = "retina")
# # -- Alternative viz 1: use {gghighlight} to highlight specific teams or highlight by facet
# library(gghighlight)
# game_goals_df %>%
# ggplot(aes(x = game_number, y = cum_goal_diff, group = team)) +
# geom_line() +
# geom_from_path(
# data = plot_image_df,
# aes(x = game_number, y = cum_goal_diff, path = logo), width = 0.03
# ) +
# gghighlight() +
# facet_wrap(~team)
# # -- Alternative viz 2: {gghighlight} to spotlight particular teams
# game_goals_df %>%
# left_join(logos_df) %>%
# ggplot(aes(x = game_number, y = cum_goal_diff, group = team)) +
# geom_line(aes(color = primary)) +
# geom_from_path(
# data = plot_image_df,
# aes(x = game_number, y = cum_goal_diff, path = logo), width = 0.03
# ) +
# gghighlight(max(cum_goal_diff) > 25) +
# scale_color_identity()
@OTStats
Copy link
Author

OTStats commented Feb 3, 2024

image

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