Created
January 5, 2019 21:38
-
-
Save luisDVA/411134394b4b59bd8d96ac73b1ea78c0 to your computer and use it in GitHub Desktop.
animate b-ball shot distances
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
library(nbastatR) #install_github("abresler/nbastatR") if needed | |
# team shot chart for current season | |
hou <- teams_shots(teams = "Houston Rockets", | |
seasons = 2019) | |
library(dplyr) # because some functions conflict | |
last3games <- hou %>% select(idGame) %>% | |
unique() %>% top_n(3) %>% pull(idGame) | |
JHshots <- | |
hou %>% filter(namePlayer=="James Harden") %>% | |
filter(idGame %in% last3games) | |
library(ggplot2) | |
library(gganimate) | |
library(lubridate) | |
library(ggalt) | |
library(artyfarty) | |
# chronological game time | |
JHshots <- JHshots %>% mutate(gtime=ms(as.character(paste(minutesRemaining,secondsRemaining,sep = ":")))) %>% | |
mutate(time_chron=case_when( | |
numberPeriod==1~ms("12:00")-gtime, | |
numberPeriod==2~ms("24:00")-gtime, | |
numberPeriod==3~ms("36:00")-gtime, | |
numberPeriod==4~ms("48:00")-gtime, | |
numberPeriod==5~ms("52:00")-gtime)) | |
# for better plotting | |
JHshots <- JHshots %>% mutate(opponent=if_else(slugTeamAway!="HOU",slugTeamAway,slugTeamHome)) %>% | |
mutate(opponent=paste("vs",opponent,dateGame)) | |
JHshots <- JHshots %>% | |
mutate(distTrans=if_else(distanceShot==0,0.8,distanceShot)) | |
# plot | |
scoringD <- | |
ggplot(JHshots)+ | |
geom_hline(yintercept = 23.9,linetype=2,color="gray")+ | |
annotate("text",label="from downtown!",x=800,26.5,size=5,alpha=0.5)+ | |
geom_vline(xintercept = as.numeric(ms("48:00")),linetype=3,color="red")+ | |
geom_lollipop(aes(x=time_chron,y=distTrans, | |
color=isShotMade))+ | |
labs(y="shot distance (feet) \n *excludes dunks and free throws", | |
x="time (minutes)", title = "James Harden")+ | |
scale_x_time(breaks = ms(c("12:00","24:00","36:00","48:00")))+ | |
scale_color_manual(values = c("#00529b","#cc4b4b"),labels=c("made","missed"))+ | |
facet_wrap(~opponent,ncol = 1)+ | |
theme_farty()+ | |
theme(text = element_text(size = 19), | |
strip.background = element_blank(), | |
strip.text = element_text(family = "sans"), | |
legend.position = "bottom", | |
legend.title = element_blank(), | |
legend.text = element_text(size=19))+ | |
transition_states(idEvent)+shadow_mark() | |
# animate and export | |
scoringAnim <- animate(scoringD,height=900, width=800) | |
scoringAnim | |
#anim_save(path = here::here(),animation = scoringAnim,filename = "bballjh.gif") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment