shotchart: Plots different kinds of charts based on shot coordinates

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/shotchart.R

Description

Plots different kinds of charts based on shot coordinates

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
shotchart(
  data,
  x,
  y,
  z = NULL,
  z.fun = median,
  result = NULL,
  type = NULL,
  scatter = FALSE,
  num.sect = 7,
  n = 1000,
  col.limits = c(NA, NA),
  courtline.col = "black",
  bg.col = "white",
  sectline.col = "white",
  text.col = "white",
  legend = FALSE,
  drop.levels = TRUE,
  pt.col = "black",
  pt.alpha = 0.5,
  nbins = 25,
  palette = "mixed"
)

Arguments

data

A data frame whose rows are field shots and columns are half-court shot coordinates x and y, and optionally additional variables to be specified in z and/or result (see Details).

x

character, indicating the variable name of the x coordinate.

y

character, indicating the variable name of the y coordinate.

z

character, indicating the name of the variable used to color the points (if type=NULL) or the sectors (if type="sectors", in this case z must be a numeric variable).

z.fun

function (active when type="sectors"), used to summarize the values of z variable within each sector (recommended: mean, median).

result

character (active when type="sectors" and scatter=FALSE), indicating the name of the factor with the shot result (allowed categories made and missed).

type

character, indicating the plot type; available option are NULL, "sectors", "density-polygons", "density-raster", "density-hexbin".

scatter

logical, if TRUE a scatter plot of the shots is added to the plot.

num.sect

integer (active when type="sectors"), number of sectors.

n

integer (active when type="sectors"), number of points used to draw arcs (must be > 500).

col.limits

numeric vector, (active when z is a numeric variable), limits c(min, max) for the gradient color scale of z variable.

courtline.col

color of court lines.

bg.col

background color.

sectline.col

color of sector lines (active when type="sectors").

text.col

color of text annotation within sectors (active when type="sectors").

legend

logical, if TRUE a legend for z is plotted.

drop.levels

logical, if TRUE unused levels of the z variable are dropped.

pt.col

color of points in the scatter plot.

pt.alpha

numeric, transparency of points in the scatter plot.

nbins

integer (active when type="density-hexbin"), number of bins.

palette

color palette; available options "main", "cool", "hot", "mixed", "grey", "bwr" (blue, white, red).

Details

The data dataframe could also be a play-by-play dataset provided that rows corresponding to events different from field shots have missing x and y coordinates.

x and y coordinates must be expressed in feets; the origin of the axes is positioned at the center of the field.

Value

A ggplot2 object.

Author(s)

Marco Sandri, Paola Zuccolotto, Marica Manisera (basketball.analyzer.help@gmail.com)

References

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

See Also

drawNBAcourt, geom_density_2d, geom_hex

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
PbP <- PbPmanipulation(PbP.BDB)
subdata <- subset(PbP, player=="Kevin Durant")
subdata$xx <- subdata$original_x/10
subdata$yy <- subdata$original_y/10-41.75
shotchart(data=subdata, x="xx", y="yy", scatter=TRUE)
shotchart(data=subdata, x="xx", y="yy", scatter=TRUE, z="result")
shotchart(data=subdata, x="xx", y="yy", scatter=TRUE, z="result",
          bg.col="black", courtline.col="white", palette="hot")
shotchart(data=subdata, x="xx", y="yy", result="result",
          type="sectors", sectline.col="gray", text.col="red")
shotchart(data=subdata, x="xx", y="yy", z="playlength", result="result",
          type="sectors",  num.sect=5)
shotchart(data=subdata, x="xx", y="yy", type="density-polygons", palette="bwr")
shotchart(data=subdata, x="xx", y="yy", type="density-raster",
          scatter=TRUE, pt.col="tomato", pt.alpha=0.1)
shotchart(data=subdata, x="xx", y="yy", type="density-hexbin", nbins=30)

BasketballAnalyzeR documentation built on July 2, 2020, 2:14 a.m.