Skip to content

Instantly share code, notes, and snippets.

@boooeee
Created August 18, 2020 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boooeee/8e1dff3904c12cdbad2cbbaff326f93d to your computer and use it in GitHub Desktop.
Save boooeee/8e1dff3904c12cdbad2cbbaff326f93d to your computer and use it in GitHub Desktop.
NBA Player Bank Shot Scatter Plot using ggplot
# this code generates bank shot scatterplots for different players via facet_wrap
# dbsel is a data frame with the following columns (sourced from 2013-2016 tracking data):
# nm (player name)
# bx (x-coordinate of backboard strike)
# by (y-coordinate of backboard strike)
ggplot(dbsel,aes(x=by,y=bz)) +
theme_bw() +
coord_fixed() + # so the backboard is shown to scale #
geom_point(shape=1,size=3,color="red") +
ggtitle("Location of NBA player 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(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") +
facet_wrap(~nm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment