nhl_plot_rink: Plot an NHL rink

Description Usage Details Examples

View source: R/nhl_plot_utils.R

Description

Initialize a plot in base graphics with a to-scale NHL rink as the background

Usage

1

Details

The placement of rink features & their sizes are exact according to the NHL rule book; see citation.

Examples

 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)

nhlapi documentation built on Feb. 20, 2021, 9:06 a.m.