merge_shot_data: Merge Shot Data with Half Court Lattice Shapefile

Description Usage Arguments Value Examples

View source: R/graphics.R

Description

Merge the shot chart data with a SpatialPolygonDataFrame representing an NBA regulation half court. This makes it useful to construct a weight matrix that can be used in spatial modeling.

Usage

1
merge_shot_data(shpfile, shot_df, hex = FALSE)

Arguments

shpfile

SpatialPolygonsDataFrame returned from half_court_raster.

shot_df

Data frame object returned from get_shotchart.

Value

SpatialPolygonsDataFrame object which contains the spatial polygons along with additional data from(shot_df) in the data slot.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# create a half court raster SpatialPolygonsDataFrame object
sp_pol <- half_court_raster()
# merge shot chart data with the spatial polygon (grid)
sp_pol <- merge_shot_data(sp_pol, NBAapi::shots_cavs)
# plot the result
## first deal with color
colors <- c("white", "black")
num_cols <- length(unique(sp_pol@data$counts))
plotclr <- colorRampPalette(colors)(num_cols)
breaks <- seq(0, max(max(unique(sp_pol@data$counts), na.rm = TRUE)), length.out = num_cols + 1)
colcode <- plotclr[findInterval(sp_pol@data$counts, vec = sort(unique(sp_pol@data$counts)))]
## now plot grid
plot(sp_pol, col = colcode, border = "transparent")
## now plot shots
draw_halfcourt(add = TRUE)

imadmali/NBAapi documentation built on May 6, 2019, 2:31 p.m.