Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Alex-Devoid/3c42122dd7e44daa36902306e97ee19e to your computer and use it in GitHub Desktop.
Save Alex-Devoid/3c42122dd7e44daa36902306e97ee19e to your computer and use it in GitHub Desktop.
az-wildcats-vs-UCLA-bruins-Turnovers-Field-goals
---
title: "Visualizing One Million NCAA Basketball Shots"
author: "Max Woolf (@minimaxir)"
date: "2018-03-19"
output:
html_notebook:
highlight: tango
mathjax: null
number_sections: yes
theme: spacelab
---
This R Notebook is the complement to my blog post [Visualizing One Million NCAA Basketball Shots](http://minimaxir.com/2018/03/basketball-shots/).
This notebook is licensed under the MIT License. If you use the code or data visualization designs contained within this notebook, it would be greatly appreciated if proper attribution is given back to this notebook and/or myself. Thanks! :)
# Setup
```{r}
library(tidyverse)
library(scales)
library(viridis)
library(extrafont)
library(chron)
library(gganimate)
library(png)
library(gifski)
# Special thanks to Ewen Gallic for his implementation of a ggplot2 basketball court
# http://egallic.fr/en/drawing-a-basketball-court-with-r/
source("bb_court_college.R")
sessionInfo()
```
```{r}
theme_set(theme_minimal(base_size=16, base_family="serif") +
theme(plot.title = element_text(size=18, family="serif", margin=margin(t = -0.1, b = 0.1, unit='cm')),
axis.title.x = element_text(size=16),
axis.title.y = element_text(size=16),
plot.subtitle = element_text(family="serif", color="#969696", size=12),
plot.caption = element_text(size=12, color="#969696"),
legend.text = element_text(size = 12),
legend.key.width = unit(0.25, unit='cm')))
bb_theme <- theme(
plot.title = element_text(size=15, family="serif", margin=margin(t = -0.1, b = 0.0, unit='cm')),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid = element_blank(),
legend.position = 'top',
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
legend.key.width = unit(1, unit='cm'),
legend.key.height = unit(0.25, unit='cm'),
legend.margin = margin(c(0, 0, -0.4, 0), unit='cm'))
```
```{r}
df <- jsonlite::fromJSON('all.json', flatten = TRUE)
df <- df %>% mutate(
x = rescale(location.coord_x, to = c(0,94)),
y = rescale(location.coord_y, to = c(-50,0)),
time_convert2 = as.POSIXct(strptime(paste0(clock_decimal, ":00"), "%H:%M:%S")),
index1 = sequence,
fieldgoal_count = 0,
turnover_count = 0,
type = event_type
)
df <- df[with(df, order(time_convert2)), ]
df <- df[with(df, order(index1)), ]
df
#count field goals and turnovers
count3 = 0
for (val1 in df$index1){
count3 = count3+1
count4 = 0
count1 = 0
count2 = 0
for (val in df$index1){
count4 = count4+1
if(val1 >= val && df$event_type[count4] == 'turnover'){
print('turnover')
print(count4)
count1 = count1+1
}
if(val1 >= val && df$event_type[count4] != 'turnover'){
print('field goal')
print(count4)
count2 = count2+1
}
df$turnover_count[count3] = count1
df$fieldgoal_count[count3] = count2
}
}
df <- rowid_to_column(df,"index11")
df
```
```{r}
plot2 <- P_180 + geom_point(data = df, aes(x = x, y = y, colour = event_type), size = 5, alpha = .8)+ scale_color_manual(values=c("#6868ff", "#ff6868", "#6868ff"), name ='', breaks=c("turnover", "twopointmade", "threepointmade"),labels=c("Turnover", "Two Points", "Three Points"))+bb_theme + ease_aes()+transition_states(index11,transition_length = 0,
state_length = 3,wrap = FALSE) + labs(caption='Arizona Wildcats vs. UCLA Bruins (2/29/2020)',
title='Wildcat Turnovers: {df$turnover_count[which(df$index11 == closest_state)]} Wildcat Field Goals: {df$fieldgoal_count[which(df$index11 == closest_state)]}',
subtitle='{closest_state} : {df$description[which(df$index11 == closest_state)]}')+shadow_mark()
plot2
animate(plot2, renderer = gifski_renderer("gganim2.gif") )
animate(plot2, renderer = ffmpeg_renderer(), width = 800, height = 450)
anim_save("turnovers.mp4")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment