Created
September 5, 2019 11:18
-
-
Save cavedave/5f6d61ed5b6eef0ea74a713ce3b33c94 to your computer and use it in GitHub Desktop.
R package animation comparing nfl combine stats over the years. BMI forty yard dash idea Idea from https://medium.com/@slackjack314/bmi-speed-and-nfl-positions-11f90b407551 as I know nothing about NFL. data fromhttp://www.nflsavant.com/dump/combine.csv this type of graph i learned at https://www.r-bloggers.com/ggplot2-easy-way-to-mix-multiple-gr…
This file contains 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(ggplot2) | |
library(dplyr) | |
library(animation) | |
library(ggpubr) | |
library(stringr) | |
#read website | |
#Run this only once to not take too much out of nflsavant's server | |
#I found this website in this article https://medium.com/@slackjack314/bmi-speed-and-nfl-positions-11f90b407551 | |
URL <- "http://www.nflsavant.com/dump/combine.csv" | |
download.file(URL, destfile = "combine2.csv", method="curl") | |
#This can be run each time you overwrite or whatever the dataframe | |
football <- read.csv("combine2.csv") | |
#just WR and DE is not enough of a sample so I have divided positions into what I guess are those that run about and those that don't | |
football <- football %>% | |
mutate(type = str_replace_all(position, c("DE"= "line", "DT"= "line", | |
"OG"= "line", "OT"= "line", | |
"CB"= "run", "WR"= "run", | |
"SS"= "run","RB"= "run"))) | |
#Clean up data a bit by removing people elss than a foot tall and such | |
football<-filter(football, heightinchestotal > 40 )#%>% na.omit() | |
#football<- filter(football,position == "DE" |position == "WR") | |
football<- filter(football,type == "run" |type == "line") | |
#football2<- filter(football2,year == 2015) | |
#change name of a column | |
names(football)[names(football) == "heightinchestotal"] <- "height" | |
#fortyyd in less then a second seems unlikely | |
football<- filter(football,fortyyd >1) | |
#get bmi | |
football<-football %>% | |
mutate(bmi = (703*weight)/(height*height)) | |
#Now make the animation | |
saveGIF({ | |
for(i in 1999:2015){ | |
football2<- filter(football,year == i) | |
#BMI graph on top | |
xplot<-ggdensity(football2, "bmi",fill = "type",palette = "jco")+coord_cartesian(xlim = c(25,45), ylim = c(0,.25)) | |
#forty yard dash on side | |
yplot<-ggdensity(football2, "fortyyd",fill = "type",palette = "jco")+coord_cartesian(xlim = c(4.2,6), ylim = c(0,5)) | |
yplot <- yplot + clean_theme() | |
xplot <- xplot + clean_theme() | |
#scatter plot at bottom left | |
sp <- ggscatter(football2, x = "bmi", y = "fortyyd", | |
color = "type", palette = "jco", | |
size = 3, alpha = 0.6)+annotate(x=24, y=4, geom="text", label=i, size= 11)+coord_cartesian(xlim = c(22,45), ylim = c(4,6)) | |
#combine these three graphs | |
figure<-ggarrange(xplot, NULL, sp, yplot, | |
ncol = 2, nrow = 2, align = "hv", | |
widths = c(2, 1), heights = c(1, 2), | |
common.legend = TRUE) | |
figure<-annotate_figure(figure, | |
top = text_grob("BMI vs. 40 Yard Dash in NFL Scouting Combine", color = "Black", face = "bold", size = 20), | |
bottom = text_grob("data: nflsavant.com", color = "blue", | |
hjust = 1, x = 1, face = "italic", size = 10)) | |
print(figure | |
) | |
#last frame should go on longer so i print it three extra times. | |
if(i == 2015){ | |
print(figure) | |
print(figure) | |
print(figure)} | |
} | |
}, interval=0.4,ani.width = 900, ani.height = 600) | |
Author
cavedave
commented
Sep 5, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment