-
Notifications
You must be signed in to change notification settings - Fork 0
/
mahomes-hill.R
98 lines (82 loc) · 4.78 KB
/
mahomes-hill.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
library(tidyverse)
library(gganimate)
library(magick)
library(gifski)
one_week_data = read_csv("~/nfl-big-data-bowl-2021/week14.csv")
plays_data = read_csv("~/nfl-big-data-bowl-2021/plays.csv")
players = read_csv("~/nfl-big-data-bowl-2021/players.csv")
generate_play = function(game, play){
one_play = one_week_data %>% filter(gameId == game & playId == play)
one_play_summary = plays_data %>% filter(gameId == game, playId == play)
description = one_play_summary$playDescription[1]
one_play_for_plot = one_play
one_play_for_plot$time = format(one_play_for_plot$time, format = "%Y-%m-%d %H:%M:%OS3")
one_play_for_plot_home = one_play_for_plot %>% filter(team == 'home')
one_play_for_plot_away = one_play_for_plot %>% filter(team == 'away')
one_play_for_plot_ball = one_play_for_plot %>% filter(team == 'football')
# specify where the extra markings should go
x_markings = seq(from = 11, to = 109, by = 1)
x_markings = x_markings[x_markings %% 5 != 0]
y_bottom = rep(1, length(x_markings))
y_lower_mid = rep(18, length(x_markings))
y_upper_mid = rep(53.3-18, length(x_markings))
y_top = rep(53.3 - 1, length(x_markings))
# specify where the numbers should go
numbers_x = seq(from = 20, to = 100, by = 10)
numbers_bottom_y = rep(3, length(numbers_x))
numbers_top_y = rep(53.3-3, length(numbers_x))
# generate the base plot to animate
base_plot = ggplot(one_play_for_plot_ball, aes(x = x, y = y)) +
xlim(0,120) + ylim(0, 53.3) +
theme(panel.background = element_rect(fill='darkgreen', colour='red'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(size = 12)) +
geom_hline(yintercept=0, colour = 'white') +
geom_hline(yintercept=53.3, colour = 'white') +
geom_vline(xintercept = 0, colour = 'white') +
geom_vline(xintercept = 10, colour = 'white') +
geom_vline(xintercept = 15, colour = 'white') +
geom_vline(xintercept = 20, colour = 'white') +
geom_vline(xintercept = 25, colour = 'white') +
geom_vline(xintercept = 30, colour = 'white') +
geom_vline(xintercept = 35, colour = 'white') +
geom_vline(xintercept = 40, colour = 'white') +
geom_vline(xintercept = 45, colour = 'white') +
geom_vline(xintercept = 50, colour = 'white') +
geom_vline(xintercept = 55, colour = 'white') +
geom_vline(xintercept = 60, colour = 'white') +
geom_vline(xintercept = 65, colour = 'white') +
geom_vline(xintercept = 70, colour = 'white') +
geom_vline(xintercept = 75, colour = 'white') +
geom_vline(xintercept = 80, colour = 'white') +
geom_vline(xintercept = 85, colour = 'white') +
geom_vline(xintercept = 90, colour = 'white') +
geom_vline(xintercept = 95, colour = 'white') +
geom_vline(xintercept = 100, colour = 'white') +
geom_vline(xintercept = 105, colour = 'white') +
geom_vline(xintercept = 110, colour = 'white') +
geom_vline(xintercept = 120, colour = 'white') +
geom_rect(aes(xmin = 0.2 , xmax = 9.8, ymin = 0.2, ymax = 53.1) ) +
geom_rect(aes(xmin = 110.2 , xmax = 119.8, ymin = 0.2, ymax = 53.1) ) +
geom_text(data = data.frame(x = x_markings, y =y_bottom), aes(label = 'l'), color = 'white')+
geom_text(data = data.frame(x = x_markings, y =y_lower_mid), aes(label = 'l'), colour = 'white') +
geom_text(data = data.frame(x = x_markings, y =y_upper_mid), aes(label = 'l'), color = 'white') +
geom_text(data = data.frame(x = x_markings, y =y_top), aes(label = 'l'), color = 'white') +
geom_text(data = data.frame(x = 12.5, y = 53.3 / 2.0), aes(label = 'l'), color = 'white', size = 10) +
geom_text(data = data.frame(x = 107.5, y = 53.3 / 2.0), aes(label = 'l'), color = 'white', size = 10) +
geom_text(data= data.frame(x = numbers_x, y = numbers_bottom_y), aes(label = 50 - abs( 50 - (numbers_x-10) )), colour = 'white', size = 5) +
geom_text(data= data.frame(x = numbers_x, y = numbers_top_y), aes(label = 50 - abs( 50 - (numbers_x-10) )), colour = 'white', size = 5) +
geom_point(data = one_play_for_plot_home, aes(x=x,y=y), colour = 'red', size = 5) +
geom_point(data = one_play_for_plot_away, aes(x=x,y=y), colour = 'purple', size = 5) +
geom_text(data = one_play_for_plot_home, aes(x = x, y = y, label = jerseyNumber), colour = 'white') +
geom_text(data = one_play_for_plot_away, aes(x = x, y = y, label = jerseyNumber), colour = 'white') +
geom_point(colour = 'white', size = 2)
anim <- base_plot + transition_states(time,
transition_length = 10, state_length = 10)
animate(anim, width = 800, height = 400, fps = 50, duration = 10)
}
generate_play(game = 2018120905, play = 4037)