-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShareable-GIFs.R
146 lines (131 loc) · 5.03 KB
/
Shareable-GIFs.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Nice Animations
# Distance Between Player and Ball
library(tidyverse)
library(ggplot2)
library(gganimate)
library(geomtextpath)
library(sportyR)
################################################################
### This gives the distance at all times
################################################################
# Didn't go play
# 1902_02_TeamMG_TeamA3 Play ID:313
# Did go play
# 1903_27_TeamNK_TeamB Play ID: 39
# Distance Between Player and Ball
library(tidyverse)
library(ggplot2)
library(gganimate)
library(geomtextpath)
library(sportyR)
################################################################
### This gives the distance at all times
################################################################
ball_pos <- read_csv("Data/ball_pos.csv")
player_pos <- read_csv("Data/player_pos.csv")
# Join all tracking data together
tracking_data <- full_join(ball_pos, player_pos,
by = c("game_str", "play_id", "timestamp"))
distance <- function(x1, x2, y1, y2) {
output <- sqrt((y2-y1)^2 + (x2-x1)^2)
return(output)
}
# For the 2 examples, all I need is 1st to 3rd so it cleans up a little nicer
plotting_data <- tracking_data %>%
mutate(ball_to_3 = distance(x1 = ball_position_x,
y1 = ball_position_y,
x2 = -sqrt(4050),
y2 = sqrt(4050)),
player_to_3 = distance(x1 = field_x,
y1 = field_y,
x2 = -sqrt(4050),
y2 = sqrt(4050)),
player_color = case_when(player_position <= 9 ~"navy",
TRUE ~ "lightblue")) %>%
filter(player_position <= 13) %>%
mutate(key = paste(game_str, play_id, sep = "_"),
linealpha = case_when(player_position == 11 ~ 1,
TRUE ~ 0))
# Get our two plays
no_go_play <- plotting_data %>%
filter(game_str == "1902_02_TeamMG_TeamA3", play_id == 313)
go_play <- plotting_data %>%
filter(game_str == "1903_27_TeamNK_TeamB", play_id == 39)
# no_go_play animation
geom_baseball(league = "MLB") +
# Fielders
geom_point(data = no_go_play,
aes(x = field_x,
y = field_y,
color = player_color),
size = 3) +
# Scale color by value
scale_color_identity() +
# Line Telling Ball to 3rd
geom_textsegment(data = no_go_play,
aes(x = ball_position_x, xend = -sqrt(4050),
y = ball_position_y, yend = sqrt(4050),
label = paste(round(ball_to_3), "ft.")),
color = "yellow") +
# Line Telling Player to 3rd
geom_textsegment(data = no_go_play,
aes(x = field_x, xend = -sqrt(4050),
y = field_y, yend = sqrt(4050),
label = paste(round(player_to_3), "ft."),
alpha = linealpha),
color = "blue") +
geom_point(data = no_go_play,
aes(x = ball_position_x,
y = ball_position_y),
color = "white",
size = 2) +
# gganimate code!
transition_time(
# Variable that you want each state to represent
time = no_go_play$timestamp) +
# Labs and theme
labs(title = "Probability Safe at Third: 3%",
caption = "Game: 1902_02_TeamMG_TeamA3 Play ID: 313 ~ Data from SMT",
x = "",
y = "") +
theme(plot.title = element_text(hjust = 0.5))
anim_save(filename = "Graphics/Stay-Example.gif")
# go_play animation
geom_baseball(league = "MLB") +
# Fielders
geom_point(data = go_play,
aes(x = field_x,
y = field_y,
color = player_color),
size = 3) +
# Scale color by value
scale_color_identity() +
# Line Telling Ball to 3rd
geom_textsegment(data = go_play,
aes(x = ball_position_x, xend = -sqrt(4050),
y = ball_position_y, yend = sqrt(4050),
label = paste(round(ball_to_3), "ft.")),
color = "yellow") +
# Line Telling Player to 3rd
geom_textsegment(data = go_play,
aes(x = field_x, xend = -sqrt(4050),
y = field_y, yend = sqrt(4050),
label = paste(round(player_to_3), "ft."),
alpha = linealpha),
color = "blue") +
geom_point(data = go_play,
aes(x = ball_position_x,
y = ball_position_y),
color = "white",
size = 2) +
# gganimate code!
transition_time(
# Variable that you want each state to represent
time = go_play$timestamp) +
# Labs and theme
labs(title = "Probability Safe at Third: 88%",
caption = "Game: 1903_27_TeamNK_TeamB Play ID: 39 ~ Data from SMT",
x = "",
y = "") +
theme(plot.title = element_text(hjust = 0.5))
anim_save(filename = "Graphics/Go-Example.gif")