R/ggrinkplot.R

Defines functions ggrinkplot circleFun

Documented in ggrinkplot

#' Function to draw an nhl rink with ggplot.
#'
#' @import jsonlite
#' @import ggplot2
#'
#' @export
ggrinkplot <- function() {
  ggplot() +
    scale_x_continuous(limits = c(-100, 100),
                       breaks = c(-89, -69, -36, 0, 36, 69, 89)) +
    scale_y_continuous(limits = c(-40, 40),
                       breaks = c(-22, 0, 22)) +
    geom_vline(xintercept = 0, col = 'red', lty = 2) +
    geom_vline(xintercept = c(-89, 89), col = 'red') +
    geom_vline(xintercept = c(-36, 36), col = 'blue') +
    geom_point(data = data.frame(
      x = c(-69, 69, -69, 69, -31, -31, 31, 31),
      y = c(-22, -22, 22, 22, -22, 22, -22, 22)),
      aes(x = x, y = y),
      col = 'red') +
    geom_point(
      aes(x = 0, y = 0),
      col = 'blue') +
    geom_path(data = circleFun(center = c(69, 22)),
              aes(x = x, y = y), col = 'red') +
    geom_path(data = circleFun(center = c(-69, 22)),
              aes(x = x, y = y), col = 'red') +
    geom_path(data = circleFun(center = c(69, -22)),
              aes(x = x, y = y), col = 'red') +
    geom_path(data = circleFun(center = c(-69, -22)),
              aes(x = x, y = y), col = 'red') +
    geom_path(data = circleFun(center = c(0, 0)),
              aes(x = x, y = y), col = 'blue') +
    geom_rect(aes(xmin = 89, xmax = 93,
                  ymin = -3, ymax = 3),
              col = 'red', fill = 'skyblue') +
    geom_rect(aes(xmin = -93, xmax = -89,
                  ymin = -3, ymax = 3),
              col = 'red', fill = 'skyblue') +
    theme_minimal() +
    theme(panel.grid.minor = element_blank())
}

# Helper function for drawing a rink
circleFun <- function(center = c(0,0), diameter = 15, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  return(data.frame(x = xx, y = yy))
}
alexpavlakis/nhl documentation built on May 18, 2019, 2:35 p.m.