library(hockeyR)
`%>%` <- magrittr::`%>%`
These functions scrape data from hockey-reference.com.
Grab every team’s win-loss record in any season going back to 1918 with the get_team_records()
function
get_team_records(1967) %>%
::arrange(-w) %>%
dplyr::select(team_name, team_abbr, season, overall, w, l, otl, st_points)
dplyr#> # A tibble: 6 x 8
#> team_name team_abbr season overall w l otl st_points
#> <chr> <chr> <chr> <chr> <int> <int> <int> <dbl>
#> 1 Chicago Black Hawks CBH 1966-67 41-17-12 41 17 12 94
#> 2 Montreal Canadiens MTL 1966-67 32-25-13 32 25 13 77
#> 3 Toronto Maple Leafs TOR 1966-67 32-27-11 32 27 11 75
#> 4 New York Rangers NYR 1966-67 30-28-12 30 28 12 72
#> 5 Detroit Red Wings DET 1966-67 27-39-4 27 39 4 58
#> 6 Boston Bruins BOS 1966-67 17-43-10 17 43 10 44
You can also get stats down to the player-level with get_player_stats_hr()
. This function defaults to the player’s career statistics, but you can enter a specific season or range of seasons as well. Note that the season references the year the specific season ended (ie the 2021-22 season should be entered as 2022)
get_player_stats_hr(player_name = "Wayne Gretzky", season = 1982) %>%
::select(player, age, season_full, tm, gp, g, a, pts)
dplyr#> # A tibble: 1 x 8
#> player age season_full tm gp g a pts
#> <chr> <int> <chr> <chr> <int> <int> <int> <int>
#> 1 Wayne Gretzky 21 1981-82 EDM 80 92 120 212
Ever wonder who the most prolific goal-scorer was to wear a specific number? Use get_jersey_players()
in conjunction with get_player_stats_hr()
to find out:
# get every player to wear the desired number
<- get_jersey_players(98)
df
# get their statistics from the year they wore that sweater
<- purrr::map2_dfr(
df2 .x = df$player,
.y = df$season,
~get_player_stats_hr(player_name = .x, season = .y)
)
# who had the most goals?
::arrange(df2, dplyr::desc(g)) %>%
dplyr::select(player, tm, season_full, gp, g, a, pts)
dplyr#> # A tibble: 12 x 7
#> player tm season_full gp g a pts
#> <chr> <chr> <chr> <int> <int> <int> <int>
#> 1 Jesse Puljujarvi EDM 2017-18 65 12 8 20
#> 2 Brian Lawton MNS 1983-84 58 10 21 31
#> 3 Mikhail Sergachev TBL 2019-20 70 10 24 34
#> 4 Mikhail Sergachev TBL 2017-18 79 9 31 40
#> 5 Mikhail Sergachev TBL 2018-19 75 6 26 32
#> 6 Brian Lawton MNS 1984-85 40 5 6 11
#> 7 Jesse Puljujarvi EDM 2018-19 46 4 5 9
#> 8 Mikhail Sergachev TBL 2020-21 56 4 26 30
#> 9 Victor Mete OTT 2020-21 14 1 1 2
#> 10 Jesse Puljujarvi EDM 2016-17 28 1 7 8
#> 11 Victor Mete MTL 2020-21 14 0 3 3
#> 12 Mikhail Sergachev TBL 2021-22 1 0 0 0
You can use the data to make plots with actual team colors and logos as well using the team_logos_colors
file included with the package.
# add colors & logos
<- df2 %>%
df3 ::group_by(player, season_full) %>%
dplyr# this part is just to get both of Mete's teams into one row
::summarize(
dplyrgp = sum(gp),
pts = sum(pts),
pts_gm = pts/gp,
tm = utils::tail(tm, n=1),
.groups = "drop"
%>%
) ::mutate(player_season = glue::glue("{player}\n{season_full}")) %>%
dplyr::left_join(team_logos_colors, by = c("tm" = "team_abbr"))
dplyr
# make a bar chart
%>%
df3 ::ggplot(ggplot2::aes(stats::reorder(player_season, -pts_gm), pts_gm)) +
ggplot2::geom_col(fill = df3$team_color1, color = df3$team_color2) +
ggplot2::geom_image(
ggimage::aes(y = pts_gm + .027, image = team_logo_espn),
ggplot2size = .07, asp = 1.5
+
) ::geom_text(ggplot2::aes(y = 0.01, label = player_season),
ggplot2color = "white", angle = 90, hjust = 0) +
::scale_y_continuous(breaks = scales::pretty_breaks()) +
ggplot2::theme(
ggplot2panel.background = ggplot2::element_rect(fill = "black"),
plot.background = ggplot2::element_rect(fill = "black"),
panel.grid.major.x = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.ticks.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_text(color = "white"),
title = ggplot2::element_text(color = "white")
+
) ::labs(x = NULL, y = "Points per game",
ggplot2title = "Most productive NHL seasons wearing #98",
caption = "data pulled from hockey-reference.com using hockeyR")
The most reliable way to grab player stats is through the
get_rosters()
function. The get_player_stats_hr()
function still has some kinks to be worked out, as it works based on the player name (and messes up if you want to differentiate the Sebastian Ahos).
With get_rosters()
, you can look up the current roster for any team in the league or the roster at season’s end for any prior season. By default, it will only pull basic player info (name, age, height & weight, etc), but you can grab all the basic counting stats by setting include_stats
to TRUE
.
<- get_rosters(c("COL","Detroit red wings"), season = 2001, include_stats = TRUE) %>%
player_stats ::mutate(
dplyrg_60 = 60 * g / toi,
a_60 = 60 * a /toi,
p_60 = 60 * pts / toi
%>%
) ::filter(toi >= 300) %>%
dplyr::left_join(team_logos_colors, by = "team_abbr")
dplyr
<- dplyr::filter(
top_performers
player_stats,>= dplyr::arrange(player_stats, -p_60) %>%
p_60 ::slice(10) %>%
dplyr::pull(p_60)
dplyr
)
%>%
player_stats ::ggplot(ggplot2::aes(a_60,g_60)) +
ggplot2::geom_hline(yintercept = 60 * sum(player_stats$g) / sum(player_stats$toi),
ggplot2linetype = "dashed", color = "black") +
::geom_vline(xintercept = 60 * sum(player_stats$a) / sum(player_stats$toi),
ggplot2linetype = "dashed", color = "black") +
#geom_point(aes(size = toi), show.legend = FALSE,
# color = player_stats$team_color_alt1, alpha = .8) +
::geom_image(ggplot2::aes(image = team_logo_espn),
ggimagesize = 0.07, asp = 1.5) +
::geom_text_repel(
ggrepeldata = top_performers,
::aes(label = player),
ggplot2color = top_performers$team_color_alt1
+
) ::scale_y_continuous(breaks = scales::pretty_breaks()) +
ggplot2::scale_x_continuous(breaks = scales::pretty_breaks()) +
ggplot2::theme(
ggplot2panel.background = ggplot2::element_rect(fill = "#708090"),
plot.background = ggplot2::element_rect(fill = "#708090"),
title = ggplot2::element_text(color = "white")
+
) ::labs(x = "Assists/60", y = "Goals/60",
ggplot2title = "2000-01 Wings v Avs, regular season stats",
subtitle = "min. 300 minutes",
caption = "data pulled from hockey-reference.com using hockeyR")