{"cells":[{"metadata":{"_uuid":"163726998806c5915e8486bc3fb766c7522fe066"},"cell_type":"markdown","source":"![](https://s3.amazonaws.com/nonwebstorage/headstrong/fieldbanner.jpg)\n\nThis script's purpose is to help visualize NFL punt plays with animation. The animations use the NFL's Next Gen Stats (NGS) data provided by the NFL Punt Analytics Competition.\n\nThe play shown here contains  a great 76-yard punt return. Unfortunately it also contains a concussion. You can see the block between the triangle-shaped players that causes the injury. My report on reducing concussions associated with punt plays is at https://www.kaggle.com/jpmiller/nfl-punt-analytics "},{"metadata":{"trusted":true,"_uuid":"cbde6c7ee70b950d71621ecb82dfc5ee38bb7b96","_kg_hide-output":true},"cell_type":"code","source":"library(data.table)\nlibrary(gganimate)\nlibrary(cowplot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59ad6c4e2510710f073d8d7b4f95c0aa8333c402","_kg_hide-output":true},"cell_type":"code","source":"# create animation; based on example at https://github.com/nfl-football-ops/Big-Data-Bowl \n# see the NFL Data Preparation kernel for source code producing the csv file\nexample.play = fread('../input/nfl-data-preparation/animate_585_733.csv')\nexample.play[Team == 'ball', Number := \":\"]\nexample.play[, Number := substr(Number, 1, 3)]\nexample.play[Number == 55, involved := 1]\nexample.play[Team == 'ball', involved := 2]\nexample.play[, involved := as.character(involved)]\nsetorder(example.play, -Team)\n\n\n# set field params\nymin <- 0\nymax <- 160/3\nxmin <- 0\nxmax <- 120\n\nhash.top <- 38.35\nhash.bottom <- 12\nhash.width <- 3.3\ndf.hash <- expand.grid(y = c(0, 18, ymax-18, ymax), x = (10:110))\n\n\n# create base plot\nanimate.play <- ggplot() +\n  \n  geom_rect(data=df.hash, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=\"#88c999\", alpha=0.2) +\n  \n    annotate(\"segment\", x = c(xmin, xmin, xmax, xmax), \n           y = c(ymin, ymax, ymax, ymin), \n           xend = c(xmin, xmax, xmax, xmin), \n           yend = c(ymax, ymax, ymin, ymin), colour = \"darkgreen\") + \n  \n    annotate(\"segment\", y = ymin, \n           x = seq(max(10, xmin), min(xmax, 110), by = 5), \n           yend =  ymax, \n           xend = seq(max(10, xmin), min(xmax, 110), by = 5), color=\"white\") + \n  \n    annotate(\"text\", x = df.hash$x[df.hash$y < 55/2],\n           y = df.hash$y[df.hash$y < 55/2], label = \"_\", hjust = 0, vjust = -0.2, \n          angle=90, color=\"white\") +\n \n    annotate(\"text\", x = df.hash$x[df.hash$y > 55/2],\n           y = df.hash$y[df.hash$y > 55/2], label = \"_\", hjust = 1, vjust = -0.2, \n           angle=90, color=\"white\") +\n  \n    annotate(\"text\", y = rep(hash.bottom - 2, 11), x = seq(10, 110, by = 10),\n           label = c(\" G\", seq(10, 50, by = 10), rev(seq(10, 40, by = 10)), \"G \"),\n           angle = 0, size = 4, color=\"white\") +\n    \n    annotate(\"text\", y = rep((ymax - hash.bottom + 3), 11), x = seq(10, 110, by = 10), \n           label = c(\"G  \", seq(10, 50, by = 10), rev(seq(10, 40, by = 10)), \" G\"), \n           angle = 180, size = 4, color=\"white\") + \n  \n  geom_point(data = example.play, aes(x = x, y = y, \n                    colour = Team, group = GSISID, pch = involved, size = involved)) + \n  \n  scale_size_manual(values = c(7, 7, 4), guide = FALSE) + \n  scale_shape_manual(values = c(19, 17, 19), guide = FALSE) +\n  scale_colour_manual(values = c(\"#654321\", \"#002244\", \"#e31837\"), guide = FALSE) + \n\n  geom_text(data = example.play, aes(x = x, y = y, label = Number), colour = \"white\", \n            hjust = 0.4, size = 3.5) + \n\n  ylim(ymin, ymax) + \n  coord_fixed() +  \n  theme_nothing() + \n  transition_time(frame_id)  +\n  ease_aes('linear') + \n  NULL\n","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"101d9a3b87c1f4a7c590960864054c3627f513f5"},"cell_type":"code","source":"# animate\nplay.length.ex <- length(unique(example.play$frame_id))\nanim = animate(animate.play, fps = 10, nframe = play.length.ex)\nanim_save(\"animation_585_733.gif\", anim)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4448cb6f020beb8dd5161c45876a1eb2555313e6"},"cell_type":"markdown","source":"You can access the animated gif file from the \"Output\" tab. "}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}