Skip to content

Instantly share code, notes, and snippets.

@edkupfer
edkupfer / lattice.nba.fullcourt
Last active December 21, 2015 05:49
Draw a full NBA basketball court in R with lattice
# NBA courts are 50ft sideline-to-sideline
# and 94ft end to end. This will plot a court
# with the sidelines on the y-axis, centered
# on the halfcourt circle. Measurement units
# are in feet. The basket at the bottom is
# centered on {x = 0, y = -41.75} and at the
# top on {x = 0, y = 41.75}
library(lattice)
@edkupfer
edkupfer / lattice.ncaa.fullcourt
Last active December 21, 2015 19:29
Draw a NCAA full basketball court using Lattice
# NCAA courts are 50ft sideline-to-sideline
# and 94ft end to end. This will plot a court
# with the sidelines on the y-axis, centered
# on the halfcourt circle. Measurement units
# are in feet. The basket at the bottom is
# centered on {x = 0, y = -41.75} and at the
# top on {x = 0, y = 41.75}
library(lattice)
@edkupfer
edkupfer / BRefShotChart
Created March 4, 2015 14:57
Take shot data from Basketball-Reference player-season page, create ggplot with shot points and isobars
library(ggplot2)
library(stringr)
library(plyr)
library(car)
### the url of the player-season shooting page from basketball reference
url="http://www.basketball-reference.com/players/b/bryanko01/shooting/2015/"
dat=readLines(url)
dat=dat[grep("<div style=\"position:absolute;top:",dat)]
@edkupfer
edkupfer / ggplot.nba.fullcourt
Created August 27, 2013 15:18
Draw a full NBA court in R using ggplot2
library(ggplot2)
ggplot(data=data.frame(x=1,y=1),aes(x,y))+
###outside box:
geom_path(data=data.frame(x=c(-25,-25,25,25,-25),y=c(-47,47,47,-47,-47)))+
###halfcourt line:
geom_path(data=data.frame(x=c(-25,25),y=c(0,0)))+
###halfcourt semicircle:
geom_path(data=data.frame(x=c(-6000:(-1)/1000,1:6000/1000),y=c(sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(x=x,y=y))+
geom_path(data=data.frame(x=c(-6000:(-1)/1000,1:6000/1000),y=-c(sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(x=x,y=y))+
@edkupfer
edkupfer / ggplot.nba.halfcourt
Last active March 19, 2024 19:13
Draw a NBA halfcourt in R using ggplot2
library(ggplot2)
ggplot(data=data.frame(x=1,y=1),aes(x,y))+
###outside box:
geom_path(data=data.frame(x=c(-25,-25,25,25,-25),y=c(0,47,47,0,0)))+
###solid FT semicircle above FT line:
geom_path(data=data.frame(x=c(-6000:(-1)/1000,1:6000/1000),y=c(19+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(x=x,y=y))+
###dashed FT semicircle below FT line:
geom_path(data=data.frame(x=c(-6000:(-1)/1000,1:6000/1000),y=c(19-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(x=x,y=y),linetype='dashed')+
###key: