Last active
January 31, 2021 05:41
-
-
Save boooeee/8d833a62b9dc87b311d1dbccde927110 to your computer and use it in GitHub Desktop.
code for creating animated ggplot charts of a player's shooting windup - split by open vs. defended
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this code creates animated shooting windup charts for a selected player # | |
rm(list = ls(all = TRUE)) | |
library(dplyr) | |
library(ggplot2) | |
library(gganimate) | |
library(readr) | |
# enter location to output animated gifs # | |
locout<-"" | |
# urls for path detail files # | |
opu<-"https://www.dropbox.com/s/v1a4vh3kg2xcpf0/path_detail_open.csv?dl=1" | |
dfu<-"https://www.dropbox.com/s/ttb14acd6rcqxta/path_detail_defended.csv?dl=1" | |
# open shots - no defenders within 4 feet at time of shot | |
# defended shots - defender within 4 feet at time of shot | |
op<-read_csv(file=opu) | |
df<-read_csv(file=dfu) | |
# select player # | |
pnm<-"Kyle Lowry" | |
ops<-op %>% | |
filter(paste(fnm,lnm)==pnm) %>% | |
filter(round(t,2)==round(t,3)) %>% # narrow down dataset to every 1/100 second # | |
mutate(cat="open") | |
dfs<-df %>% | |
filter(paste(fnm,lnm)==pnm) %>% | |
filter(round(t,2)==round(t,3)) %>% # narrow down dataset to every 1/100 second # | |
mutate(cat="defended") | |
od<-ops %>% | |
bind_rows(dfs) %>% | |
mutate(t=round(t,2),fac="") | |
ol<-vector('list') | |
for (t in unique(od$t)) { | |
a<-od[od$t==t,] | |
a$fac<-"sel" | |
o<-rbind(a,od[od$t!=t,]) | |
o<-o[order(o$pid,o$t),] | |
o$trn<-t | |
ol[[as.character(t)]]<-o | |
} | |
old<-do.call('rbind',ol) | |
old$fac1<-factor(paste(old$cat,old$fac)) | |
# profile view # | |
g<-ggplot(old , aes(x = cx, y = cz)) + | |
geom_point(aes(color=fac1,size=fac1)) +scale_colour_manual(values = c("red","red","blue","blue")) + | |
scale_size_manual(values=c(1,3,1,3)) + | |
coord_fixed() + | |
theme_minimal() + | |
scale_x_continuous(limits=c(-2,2)) + | |
scale_y_continuous(limits=c(2.5,9)) + | |
labs(x = "horizontal distance (feet)", y="height (feet)", caption="@inpredict") + | |
ggtitle(paste(pnm," Three Point Windup - at 1/5 Speed",sep=""), subtitle="profile view (defended=red, open=blue)") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
theme(plot.subtitle = element_text(hjust = 0.5)) + | |
theme(legend.position="none",text = element_text(size=12,family="Open Sans")) | |
anim<-g + transition_manual(trn) | |
animate(anim,fps=50,height=1000,width=900,res=175) | |
anim_save(paste(locout,"opendfndxz.gif",sep="")) | |
# overhead view # | |
g<-ggplot(old , aes(x = cx, y = cy)) + | |
geom_point(aes(color=fac1,size=fac1)) +scale_colour_manual(values = c("red","red","blue","blue")) + | |
scale_size_manual(values=c(1,3,1,3)) + | |
coord_fixed() + | |
theme_minimal() + | |
scale_x_continuous(limits=c(-1,1)) + | |
scale_y_continuous(limits=c(-1,1)) + | |
labs(x = "horizontal distance (feet)", y="height (feet)", caption="@inpredict") + | |
ggtitle(paste(pnm," Three Point Windup - at 1/5 Speed",sep=""), subtitle="overhead view (defended=red, open=blue)") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
theme(plot.subtitle = element_text(hjust = 0.5)) + | |
theme(legend.position="none",text = element_text(size=12,family="Open Sans")) | |
anim<-g + transition_manual(trn) | |
animate(anim,fps=50,height=1000,width=1000,res=175) | |
anim_save(paste(locout,"opendfndxy.gif",sep="")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment