This package allows the user to build custom shot charts. A shot chart is a scatterplot of the locations from which a player takes shots overlayed onto an outline of the court. They are a very powerful visualization tool to analyze the shooting preferences and efficiency of players. Raw shots data can be retrieved with getGenericData through the "shotchartdetail" endpoint. The package also includes an interactive app for building shot charts (see launchApp), although the functionality described here provides the building blocks for more customized charts.

Plotting the Court

This package provides two functions for assisting in plotting the lines of an NBA court: courtOutline and courtOutlinePlot. The first returns a data frame of coordinates of line segments that, when connected, draw the lines of an NBA court. One optional parameter specifies whether to get the coordinates of the full court or only half the court (default is half court).

half.court <- rNBA::courtOutline()
full.court <- rNBA::courtOutline(full = TRUE)
head(half.court)

Using base graphics, the lines can be plotted as follows.

examplePlotLines <- function(court) {
  plot(x = NULL, xlim = range(court$x), ylim = range(court$y), xaxt = "n", yaxt = "n", ann = FALSE)
  for (nm in unique(court$type)) {
     s <- court$type == nm
     points(x = court$x[s], y = court$y[s], type = "l", lty = court$ltype[s])
  } 
}
examplePlotLines(half.court)
examplePlotLines(full.court)

The second function, courtOutlinePlot, essentially produces the same plot as above except that it uses ggplot. This function would likely be preferable for almost all applications, as ggplot makes it easy to add layers. Again, one optional parameter specifies whether to plot the full court or only half the court (default is half court).

p.half <- rNBA::courtOutlinePlot()
p.full <- rNBA::courtOutlinePlot(full = TRUE)
p.half
p.full

Basic Shot Charts

The above functions can be used as a starting point for building a custom shot chart. Calling getGenericData with the "shotchartdetail" endpoint gets raw (x, y) coordinates of shots in the same coordinate axis as the above plots that can then be plotted directly on those plots. However, the package also includes a function shotChart that builds a basic complete shot chart for you. This function returns a list with two elements: the plot and the raw data from which the plot was built. With no arguments, this function will produce a default chart for Steph Curry using the stephCurryShots data that is attached to the package.

sc1 <- rNBA::shotChart()
sc1$plot

Arguments can be supplied to customize. The params argument takes in a list of parameters in the same format as getGenericData for the "shotchartdetail" endpoint. Thus, the paremeters that can be supplied are shown below. However, the user need not specify all of these, because the shotChart function will use defaults for any of the non specified parameters.

rNBA::getEndpointParams("shotchartdetail")

shotChart also can take a color parameter that defines which attribute to color points by in the shot chart. Valid options for this are any attribute returned by the "shotchartdetail" endpoint, which are those shown below.

names(sc1$data)

A custom shot chart can thus be built as follows.

rNBA::searchIDMappings(player = "lebron")
sc2 <- rNBA::shotChart(params = list(PlayerID = 2544), color = "SHOT_ZONE_BASIC")
sc2$plot


llefebure/nba-stats documentation built on May 21, 2019, 7:34 a.m.