I'm trying to create a basketball shot chart, and I've come to enjoy the look of a lattice shot chart plot, but it seems that I am unable to combine (or, place on top of) it with my ggplot court design. Is there anyway to do this? Or do I have to end up using the court as a raster image in the background (which is already giving me fits due to guessing and checking the image's coordinates)?
Here is a sample for the dat
data frame
location_x location_y points
1 5.5 22.1 2
2 6.0 24.1 2
3 6.1 39.4 2
4 25.3 38.7 0
5 8.0 24.4 2
6 8.8 12.7 0
7 9.0 6.0 0
8 10.6 31.6 0
9 4.2 47.5 0
10 4.3 8.9 2
EDIT: Below is my code, thanks to the help of the post: Using both color and size attributes in Hexagon Binning (ggplot2). The geom_path line is what's being used to draw the court. The error that I am currently getting is:
Error: Don't know how to add o to a plot
dat = mod_shots[mod_shots$player_id == 398068, ]
lattice_plot = xyplot(location_x~location_y, data = dat, panel = function(x,y,...)
{
hbin = hexbin(dat$location_y, dat$location_x, xbins=40, IDs=TRUE)
mtrans = hexTapply(hbin, dat$points, sum, na.rm=TRUE)
cols = rev(heat.colors(7))
grid.hexagons(hbin, style='lattice',
minarea=0.1,maxarea=3,
border="NA",
pen=cols[mtrans+1])
})
ggplot() + geom_path(data = court_points,
aes(x = x, y = y, z = NULL, group = desc, linetype = dash),
color = "#000004") + lattice_plot