Description Usage Details Examples
View source: R/nhl_plot_utils.R
Initialize a plot in base graphics with a to-scale NHL rink as the background
1 |
The placement of rink features & their sizes are exact according to the NHL rule book; see citation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ## Not run:
# Retrieve some game feed data
gameFeeds <- lapply(
2019010001:2019010010,
nhlapi::nhl_games_feed
)
# Create a data.frame with plays
getPlaysDf <- function(gm) {
playsRes <- try(gm[[1L]][["liveData"]][["plays"]][["allPlays"]])
if (inherits(playsRes, "try-error")) data.frame() else playsRes
}
plays <- lapply(gameFeeds, getPlaysDf)
plays <- nhlapi:::util_rbindlist(plays)
plays <- plays[!is.na(plays$coordinates.x), ]
# Move the coordinates to non-negative values before plotting
plays$coordx <- plays$coordinates.x + abs(min(plays$coordinates.x))
plays$coordy <- plays$coordinates.y + abs(min(plays$coordinates.y))
# Select goals only
goals <- plays[plays$result.event == "Goal", ]
# Create the plot and add goals
nhlapi::plot_rink()
points(goals$coordinates.x, goals$coordinates.y)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.