Skip to content

Instantly share code, notes, and snippets.

@TysonStanley
Last active August 29, 2015 14:20
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 TysonStanley/38f438096617dc94a80b to your computer and use it in GitHub Desktop.
Save TysonStanley/38f438096617dc94a80b to your computer and use it in GitHub Desktop.
Density Shot Chart of Stephen Curry 2015 (ggplot2 & hexbin & png)
# Steph Curry Shot Selection in 2015
# Data from: NBAsavant.com (awesome data source)
packs <- c("png","grid","ggplot2")
lapply(packs, require, character.only=TRUE)
steph <- read.csv("data.csv") # from NBAsavant.com
court <- readPNG("court.png") # diagram of NBA court
g <- rasterGrob(court, interpolate=TRUE)
# Density Shot Chart
Plot <- ggplot(steph,aes(x=x,y=y,label=shot_made_flag)) + # x is placement in horizontal units and y for vertical
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + # adds the background court
stat_density2d(aes(colour=..level..,fill=..level..,),alpha=.2, geom="polygon") + # density plot
scale_x_continuous(expand=c(0.3, 0)) + # removes edge problems
theme_bw() +
xlab("") + ylab("") +
# Remove axes
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank()) +
# Fit to court picture - Needs work here to make it fit
coord_cartesian(xlim=c(-265,265),
ylim=c(10,450)) # I played around with this knowing that he shoots a lot of 3's
Plot
# Hexbin Shot Chart (still needs work)
Plot2 <- ggplot(steph,aes(x=x,y=y,label=shot_made_flag)) +
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
stat_binhex(alpha=.75,bin=100) +
theme_bw() +
coord_cartesian(xlim=c(-275,275),
ylim=c(0,450))
Plot2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment