I am hoping to construct some charts to display the shooting tendencies/effectiveness of some NBA players and teams. I would like to format the hexagons as follows: size will represent the number of shots and color will represent the relative efficiency (pts/attempt) from that location. Here is a great example of what I'm looking for, created by Kirk Goldsberry:
I have been able to use hexbins
and hexTapply
to achieve something close to the desired result, but the shapes are circles. Here is my code (which includes sample data):
library(hexbin); library(ggplot2)
df <- read.table(text="xCoord yCoord pts
11.4 14.9 2
2.6 1.1 0
4.8 4.1 2
-14.4 8.2 2
4.2 0.3 0
0.4 0.0 2
-23.2 -1.1 3", header=TRUE)
h <- hexbin (x=df$xCoord, y = df$yCoord, IDs = TRUE, xbins=50)
pts.binned <- hexTapply (h, df$pts, FUN=mean)
df.binned <- data.frame (xCoord = h@xcm,
yCoord = h@ycm, FGA = h@count, pts = pts.binned)
chart.player <- ggplot (df.binned, aes (x =xCoord ,
y =yCoord , col = pts, size = FGA)) + coord_fixed() +
geom_point() + scale_colour_gradient("Points/Attempt", low = "green", high="red")
Another way to think about it would be to coloring the hexagons in plot(h, style="lattice")
by pts/attempt -- but I'm not sure how to do that, either.
Is there a way to get this graph with hexagons rather than circles?