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
To get the basic counting stats for every skater in a given year, use
the get_skater_stats_hr
function. Like the other functions
in this package, the season
argument needs to be the
end-year of the season you wish to scrape.
<- get_skater_stats_hr(2022)
df2
%>%
df2 ::arrange(-goals) %>%
dplyrhead()
#> # A tibble: 6 x 30
#> player team_~1 season age posit~2 games~3 goals assists points plus_~4
#> <chr> <chr> <chr> <int> <chr> <int> <int> <int> <int> <int>
#> 1 Auston Matt~ TOR 2021-~ 24 C 73 60 46 106 20
#> 2 Leon Draisa~ EDM 2021-~ 26 C 80 55 55 110 17
#> 3 Chris Kreid~ NYR 2021-~ 30 LW 81 52 25 77 19
#> 4 Alex Ovechk~ WSH 2021-~ 36 LW 77 50 40 90 8
#> 5 Kyle Connor WPG 2021-~ 25 LW 79 47 46 93 -3
#> 6 Kirill Kapr~ MIN 2021-~ 24 LW 81 47 61 108 27
#> # ... with 20 more variables: penalty_minutes <int>, hr_point_shares <dbl>,
#> # goals_even_strength <int>, goals_powerplay <int>, goals_shorthanded <int>,
#> # goals_game_winning <int>, assists_even_strength <int>,
#> # assists_powerplay <int>, assists_shorthanded <int>, shots_on_goal <int>,
#> # shooting_percent <dbl>, time_on_ice <int>, mean_time_on_ice <chr>,
#> # blocks <int>, hits <int>, faceoff_wins <int>, faceoff_losses <int>,
#> # faceoff_win_percent <dbl>, link <chr>, player_id <chr>, and abbreviated ...
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 ::arrange(-points) %>%
dplyr::slice(1:10) %>%dplyr::select(player, team_abbr, goals, assists, points) %>%
dplyr::left_join(team_logos_colors, by = "team_abbr")
dplyr
# make a bar chart
%>%
df3 ::ggplot(ggplot2::aes(stats::reorder(player, -points), points)) +
ggplot2::geom_col(fill = df3$team_color1, color = df3$team_color2) +
ggplot2::geom_image(
ggimage::aes(y = points + 2, image = team_logo_espn),
ggplot2size = .05, asp = 1.5
+
) ::geom_text(ggplot2::aes(y = 2, label = player),
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",
ggplot2title = "Top-10 point scorers in the NHL in 2021-22",
caption = "data pulled from hockey-reference.com using hockeyR")
Use the get_rosters()
function to look up a team 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
. Note as shown below that the team
argument accepts both full team names or team abbreviations.
<- 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")