Plots different kinds of charts based on shot coordinates
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"
)
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).
character, indicating the variable name of the x coordinate.
character, indicating the variable name of the y coordinate.
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).
function (active when type="sectors"
), used to summarize the values of z
variable within each sector (recommended: mean
, median
).
character (active when type="sectors"
and scatter=FALSE
), indicating the name of the factor with the shot result (allowed categories made
and missed
).
character, indicating the plot type; available option are NULL
, "sectors"
, "density-polygons"
, "density-raster"
, "density-hexbin"
.
logical, if TRUE a scatter plot of the shots is added to the plot.
integer (active when type="sectors"
), number of sectors.
integer (active when type="sectors"
), number of points used to draw arcs (must be > 500).
numeric vector, (active when z
is a numeric variable), limits c(min, max)
for the gradient color scale of z
variable.
color of court lines.
background color.
color of sector lines (active when type="sectors"
).
color of text annotation within sectors (active when type="sectors"
).
logical, if TRUE a legend for z
is plotted.
logical, if TRUE unused levels of the z
variable are dropped.
color of points in the scatter plot.
numeric, transparency of points in the scatter plot.
integer (active when type="density-hexbin"
), number of bins.
color palette; available options "main"
, "cool"
, "hot"
, "mixed"
, "grey"
, "bwr"
(blue, white, red).
A ggplot2 object.
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.
P. Zuccolotto and M. Manisera (2020) Basketball Data Science: With Applications in R. CRC Press.
# NOT RUN {
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)
# }
Run the code above in your browser using DataLab