Skip to content

Instantly share code, notes, and snippets.

@boooeee
Last active August 21, 2020 03:17
Show Gist options
  • Save boooeee/f94f0d6c500e92dab0709360fea84b90 to your computer and use it in GitHub Desktop.
Save boooeee/f94f0d6c500e92dab0709360fea84b90 to your computer and use it in GitHub Desktop.
hexbin bank shots heat maps using ggplot
# this code generates hexbin heat maps for bank shots, overlaid over a backboard #
# dbdr is a data frame with the following columns (sourced from 2013-2016 tracking data):
# bx (x-coordinate of backboard strike)
# by (y-coordinate of backboard strike)
ggplot(dbdr,aes(x=by,y=bz)) +
theme_bw() +
coord_fixed() +
geom_hex(binwidth=c(0.25,0.25),aes(fill = stat((count)))) +
scale_fill_gradient(low="lightblue1",high="darkblue") +
ggtitle("Heatmap of NBA bank shots",subtitle = "where the ball hits the backboard (derived from 2013-2016 tracking data)") +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.position = "none") +
theme(plot.title = element_text(size=15)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
theme(text = element_text(family="Open Sans")) +
# the following lines of geom_segment code draw the backboard #
geom_segment(aes(x = 22, y = 10, xend = 28, yend = 10), color = "black") +
geom_segment(aes(x = 22, y = 13.5, xend = 28, yend = 13.5), color = "black") +
geom_segment(aes(x = 22, y = 10, xend = 22, yend = 13.5), color = "black") +
geom_segment(aes(x = 28, y = 10, xend = 28, yend = 13.5), color = "black") +
geom_segment(aes(x = 24, y = 10, xend = 26, yend = 10), color = "black") +
geom_segment(aes(x = 24, y = 11.5, xend = 26, yend = 11.5), color = "black") +
geom_segment(aes(x = 24, y = 10, xend = 24, yend = 11.5), color = "black") +
geom_segment(aes(x = 26, y = 10, xend = 26, yend = 11.5), color = "black")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment