Last active
February 24, 2021 04:08
-
-
Save boooeee/b350de531c0620d5980c472b0c716674 to your computer and use it in GitHub Desktop.
plot animated shooting windups for multiple players on a single chart
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 multiple players on a single chart # | |
rm(list = ls(all = TRUE)) | |
library(dplyr) | |
library(ggplot2) | |
library(gganimate) | |
library(readr) | |
# enter location to output animated gifs # | |
locout<-"" | |
# url for path detail file # | |
pthu<-"https://www.dropbox.com/s/amwup78r3mqwkvo/path_detail.csv?dl=1" | |
pth<-read_csv(file=pthu) | |
# select players # | |
pnms<-c("Chris Paul","Damian Lillard","James Harden","Kawhi Leonard","Kevin Durant","Klay Thompson","LeBron James","Russell Westbrook","Stephen Curry") | |
pths<-pth %>% | |
mutate(name=paste(fnm,lnm)) %>% | |
filter(name %in% pnms) %>% | |
filter(round(t,2)==round(t,3)) %>% # narrow down dataset to every 1/100 second # | |
mutate(t=round(t,2),fac="") | |
ol<-vector('list') | |
for (t in unique(pths$t)) { | |
a<-pths[pths$t==t,] | |
a$fac<-"sel" | |
o<-rbind(a,pths[pths$t!=t,]) | |
o<-o[order(o$pid,o$t),] | |
o$trn<-t | |
ol[[as.character(t)]]<-o | |
} | |
old<-do.call('rbind',ol) | |
# profile view # | |
g<-ggplot(old , aes(x = cx, y = cz)) + | |
geom_point(aes(color=fac,size=fac)) + | |
scale_colour_manual(values = c("gray", "red")) + | |
scale_size_manual(values=c(1,3)) + | |
coord_fixed() + | |
theme_bw() + | |
scale_x_continuous(limits=c(-2,2)) + | |
facet_wrap(~name) + | |
labs(x = "horizontal distance (feet)", y="height (feet)") + | |
ggtitle("Animated Three Point Windups - at 1/5 Speed", subtitle="profile view - hoop is to the left") + | |
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=20,nframes=47,height=1400,width=1000,res=175) | |
anim_save(paste(locout,"topninexz3.gif",sep="")) | |
# overhead view # | |
g<-ggplot(old , aes(x = cx, y = cy)) + | |
geom_point(aes(color=fac,size=fac)) + | |
scale_colour_manual(values = c("gray", "red")) + | |
scale_size_manual(values=c(1,3)) + | |
coord_fixed() + | |
theme_bw() + | |
scale_x_continuous(limits=c(-1,1)) + | |
scale_y_continuous(limits=c(-0.25,0.75)) + | |
facet_wrap(~name) + | |
labs(x = "horizontal distance (feet)", y="horizontal distance (feet)") + | |
ggtitle("Animated Three Point Windups - at 1/5 Speed", subtitle="overhead view - hoop is to the left") + | |
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=20,nframes=47,height=1000,width=1400,res=175) | |
anim_save(paste(locout,"topninexy3.gif",sep="")) | |
# facing the hoop view # | |
g<-ggplot(old , aes(x = cy, y = cz)) + | |
geom_point(aes(color=fac,size=fac)) + | |
scale_colour_manual(values = c("gray", "red")) + | |
scale_size_manual(values=c(1,3)) + | |
coord_fixed() + | |
theme_bw() + | |
scale_x_continuous(limits=c(-2,2)) + | |
facet_wrap(~name) + | |
labs(x = "horizontal distance (feet)", y="height (feet)") + | |
ggtitle("Animated Three Point Windups - at 1/5 Speed", subtitle="face forward view - hoop is straight ahead") + | |
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=20,nframes=47,height=1400,width=1000,res=175) | |
anim_save(paste(locout,"topnineyz3.gif",sep="")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment