Learn R Programming

BasketballAnalyzeR (version 0.5.0)

densityplot: Computes and plots kernel density estimation of shots with respect to a concurrent variable

Description

Computes and plots kernel density estimation of shots with respect to a concurrent variable

Usage

densityplot(
  data,
  var,
  shot.type = "field",
  thresholds = NULL,
  best.scorer = FALSE,
  period.length = 12,
  bw = NULL,
  title = NULL
)

Arguments

data

a data frame whose rows are shots and with the following columns: ShotType, player, points and at least one of playlength, periodTime, totalTime, shot_distance (the column specified in var, see Details).

var

character, a string giving the name of the numerical variable according to which the shot density is estimated. Available options: "playlength", "periodTime", "totalTime", "shot_distance".

shot.type

character, a string giving the type of shots to be analyzed. Available options: "2P", "3P", "FT", "field".

thresholds

numerical vector with two thresholds defining the range boundaries that divide the area under the density curve into three regions. If NULL default values are used.

best.scorer

logical; if TRUE, displays the player who scored the highest number of points in the corresponding interval.

period.length

numeric, the length of a quarter in minutes (default: 12 minutes as in NBA).

bw

numeric, the value for the smoothing bandwidth of the kernel density estimator or a character string giving a rule to choose the bandwidth (see density).

title

character, plot title.

Value

A ggplot2 plot

Details

The data data frame could also be a play-by-play dataset provided that rows corresponding to events different from shots have NA in the ShotType variable.

Required columns:

  • ShotType, a factor with the following levels: "2P", "3P", "FT" (and NA for events different from shots)

  • player, a factor with the name of the player who made the shot

  • points, a numeric variable (integer) with the points scored by made shots and 0 for missed shots

  • playlength, a numeric variable with time between the shot and the immediately preceding event

  • periodTime, a numeric variable with seconds played in the quarter when the shot is attempted

  • totalTime, a numeric variable with seconds played in the whole match when the shot is attempted

  • shot_distance, a numeric variable with the distance of the shooting player from the basket (in feet)

References

P. Zuccolotto and M. Manisera (2020) Basketball Data Science: With Applications in R. CRC Press.

Examples

Run this code
# NOT RUN {
PbP <- PbPmanipulation(PbP.BDB)
data.team  <- subset(PbP, team=="GSW" & result!="")
densityplot(data=data.team, shot.type="2P", var="playlength", best.scorer=TRUE)
data.opp <- subset(PbP, team!="GSW" & result!="")
densityplot(data=data.opp, shot.type="2P", var="shot_distance", best.scorer=TRUE)
# }

Run the code above in your browser using DataLab