-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDC App.R
288 lines (256 loc) · 14.2 KB
/
BDC App.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
library(shiny)
library(tidyverse)
library(DT)
library(plotly)
lb <- read.csv("leaders.csv") %>%
mutate(All = "All",
Team = ifelse(Team == "Olympic (Women) - Canada", "Canada",
ifelse(Team == "Olympic (Women) - United States", "USA",
ifelse(Team == "Olympic (Women) - Finland", "Finland",
ifelse(Team == "Olympic (Women) - Olympic Athletes from Russia", "Russia", as.character(Team))))))
teams <- unique(as.character(lb$Team))
lb[is.na(lb)] <- 0
results <- read.csv("results.csv")
goal_x <- c(188, 190, 190, 188)
goal_y <- c(40, 40, 45, 45)
goal <- data.frame(goal_x, goal_y)
med <- median(results$xAV)
shots <- read.csv("shots.csv")
non_passes <- read.csv("non_passes_shots.csv")
passes <- read.csv("pass_cluster.csv")
data <- rbind(shots, non_passes, passes) %>%
arrange(row_num)
zone <- data %>%
filter(Event %in% c("Goal", "Shot", "Play", "Incomplete Play")) %>%
#not considering dump in/outs b/c intentionally conceding possession
group_by(id) %>%
count(Event) %>%
spread(Event, n, fill = 0) %>%
mutate(Events = Goal + `Incomplete Play` + Shot + Play,
Shots = Goal + Shot,
#creating separate shots variable makes shooting% calculation easier
Shot_pct = round((Shots / Events)*100, digits = 0),
Move_pct = round(((`Incomplete Play` + Play) / Events)*100, digits = 0),
#Shot_pct + Move_pct should equal 1
Make_pct = round((Goal / Shots)*100, digits = 0),
Make_pct = ifelse(Make_pct == "NaN", 0, Make_pct),
#cleaning up the Make_pct variable so that we have 0s instead of NaNs
AVxy = round(Shot_pct * Make_pct, digits = 5)) %>%
dplyr::select(id, Shot_pct, Make_pct, Move_pct)
results <- right_join(results, zone, by = "id")
#movement between zones
movement <- data %>%
filter(Event %in% c("Goal", "Shot", "Play", "Incomplete Play")) %>%
#not considering dump in/outs b/c intentionally conceding possession
group_by(id) %>%
count(zone_next) %>%
#using zone_next b/c it accounts for turnovers
#since we want to know how often someone tries to pass from Zone X to Zone Y
#knowing where they were aimed helps, even if the pass was taken away
filter(! is.na(zone_next))
obs <- data %>%
filter(! is.na(zone_next)) %>%
ungroup() %>%
count(id) %>%
mutate(Obs = n) %>%
dplyr::select(-n)
movement <- right_join(movement, obs, by = c("id"))
#enables us to find out how often someone went from Zone id to the next zone
#essentially: movement / total movements from that zone
movement <- movement %>%
mutate(pct_of_moves = round(n / Obs, digits = 5)) %>%
dplyr::select(id, zone_next, pct_of_moves)
df <- right_join(results, movement, by = "id")
next_res <- results %>%
dplyr::select(id, x:y_max) %>%
rename_all(paste0, "_next")
df <- right_join(df, next_res, by = c("zone_next" = "id_next"))
ui <- fluidPage(
navbarPage("Big Data Cup Leaderboard",
tabPanel("Movement Frequency and xAV Leaderboard",
sidebarLayout(
sidebarPanel(sliderInput("zone", "Select a Zone:",
min = 1, max = 70, value = 34, step = 1),
wellPanel(
p("Use slider above to toggle between zones. The zone outlined in black
is the zone you have currently selected. The white-red gradient represents
how frequently a player moves from your current zone to another zone."),
p("The Move, Shot, Make numbers below represent how often a player moved, shot,
or, given a shot, made the shot from your selected zone.")
),
verbatimTextOutput("info"),
width = 5),
mainPanel(plotOutput("new", width = 600, height = 400),
width = 7)),
hr(style = "border-color:black;"),
column(selectInput("league", "Select a League",
choices = c("NWHL", "BDC"), selected = "NWHL"), width = 4),
column(selectInput("team", "Select a Team",
choices = c("All", teams),
selected = "All"), width = 4),
column(sliderInput("min_events", "Minimum Number of Events",
min = 0, max = max(lb$Events), value = 150, step = 10), width = 4),
DTOutput("leaderboard"),
hr(style = "border-color:black;")
)#,
#tabPanel("xAV Plot",
#plotOutput("xav_plot"),
#plotlyOutput("reactive_plot", width = 600, height = 400),
#)
)
)
server <- function(input, output, session) {
updateSelectInput(session,
"league", choices = c("NWHL", "BDC"))
observeEvent(input$league,
{
use <- lb %>%
filter(League %in% c(input$league)) %>%
dplyr::select(All, Team)
updateSelectInput(session,
"team",
choices = c("All", unique(use$Team)))
})
output$leaderboard <- renderDT(
lb %>%
filter(League %in% c(input$league) & (Team %in% c(input$team) | All == input$team) &
Events >= input$min_events) %>%
dplyr::select(-League, -All, -Shot.) %>%
mutate_at(vars(xAV, xAV_100, xG, xg_100), funs(round(., 2))) %>%
dplyr::select(Team, Player, Events, Shot, Goal, xAV, xAV_100, xG, xg_100) %>%
arrange(desc(xAV_100)) %>%
datatable(extensions = "Responsive",
rownames = FALSE,
colnames = c("Team", "Player", "Events", "Shots", "Goals", "xAV",
"xAV/100", "xG", "xG/100")) %>%
formatStyle(c(1, 2, 5, 7), `border-right` = "solid 1px")
)
output$xav_plot <- renderPlot(
results %>%
ggplot() +
geom_rect(aes(xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max, color = xAV, fill = xAV)) +
scale_color_gradient2(low = "lightblue", mid = "white", midpoint = med, high = "red") +
scale_fill_gradient2(low = "lightblue", mid = "white", midpoint = med, high = "red") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank()) +
geom_segment(aes(x = 25 - 100, xend = 175 - 100, y = 0 - 42.5, yend = 0 - 42.5)) +
geom_segment(aes(x = 25 - 100, xend = 175 - 100, y = 85 - 42.5, yend = 85 - 42.5)) +
geom_segment(aes(x = 0 - 100, xend = 0 - 100, y = 20 - 42.5, yend = 60 - 42.5)) +
geom_segment(aes(x = 200 - 100, xend = 200 - 100, y = 20 - 42.5, yend = 60 - 42.5)) +
geom_curve(aes(x = 0 - 100, xend = 25 - 100, y = 60 - 42.5, yend = 85 - 42.5),
curvature = -0.32) +
geom_curve(aes(x = 0 - 100, xend = 25 - 100, y = 20 - 42.5, yend = 0 - 42.5),
curvature = 0.32) +
geom_curve(aes(x = 175 - 100, xend = 200 - 100, y = 0 - 42.5, yend = 20 - 42.5),
curvature = 0.32) +
geom_curve(aes(x = 175 - 100, xend = 200 -100, y = 85 - 42.5, yend = 60 - 42.5),
curvature = -0.32) +
geom_path(data = goal, aes(x = goal_x - 100, y = goal_y - 42.5)) +
geom_segment(aes(x = 100 - 100, xend = 100 - 100, y = 85 - 42.5, yend = 0 - 42.5), size = 2, color = "red") +
labs(title = "Average xAV by Location",
caption = "Ben Howell | @benhowell71 | benhowell71.com",
fill = "xAV") +
geom_path(data = goal, aes(x = goal_x - 100, y = goal_y - 42.5), size = 2) +
theme(panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
plot.caption = element_text(hjust = 0.5, face = "italic", size = 10),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "none"),
width = 600, height = 400)
# output$val <- renderPrint(
# hover <- input$plot_hover
#
# x <- nearPoints(results, input$plot_hover)
# )
# output$reactive_plot <- renderPlotly(
# results %>%
# ggplot() +
# geom_rect(aes(xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max, fill = xAV), color = "lightgrey") +
# scale_color_gradient2(low = "lightblue", mid = "white", midpoint = med, high = "red") +
# scale_fill_gradient2(low = "lightblue", mid = "white", midpoint = med, high = "red") +
# theme_minimal() +
# theme(panel.grid = element_blank(),
# axis.text = element_blank(),
# axis.title = element_blank()) +
# geom_segment(aes(x = 25 - 100, xend = 175 - 100, y = 0 - 42.5, yend = 0 - 42.5)) +
# geom_segment(aes(x = 25 - 100, xend = 175 - 100, y = 85 - 42.5, yend = 85 - 42.5)) +
# geom_segment(aes(x = 0 - 100, xend = 0 - 100, y = 20 - 42.5, yend = 60 - 42.5)) +
# geom_segment(aes(x = 200 - 100, xend = 200 - 100, y = 20 - 42.5, yend = 60 - 42.5)) +
# geom_curve(aes(x = 0 - 100, xend = 25 - 100, y = 60 - 42.5, yend = 85 - 42.5),
# curvature = -0.32) +
# geom_curve(aes(x = 0 - 100, xend = 25 - 100, y = 20 - 42.5, yend = 0 - 42.5),
# curvature = 0.32) +
# geom_curve(aes(x = 175 - 100, xend = 200 - 100, y = 0 - 42.5, yend = 20 - 42.5),
# curvature = 0.32) +
# geom_curve(aes(x = 175 - 100, xend = 200 -100, y = 85 - 42.5, yend = 60 - 42.5),
# curvature = -0.32) +
# geom_path(data = goal, aes(x = goal_x - 100, y = goal_y - 42.5)) +
# geom_segment(aes(x = 100 - 100, xend = 100 - 100, y = 85 - 42.5, yend = 0 - 42.5), size = 2, color = "red") +
# labs(title = "Hover xAV by Location",
# caption = "Ben Howell | @benhowell71 | benhowell71.com",
# fill = "xAV") +
# geom_path(data = goal, aes(x = goal_x - 100, y = goal_y - 42.5), size = 2) +
# theme(panel.grid = element_blank(),
# axis.text = element_blank(),
# axis.title = element_blank(),
# plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
# plot.caption = element_text(hjust = 0.5, face = "italic", size = 10),
# plot.subtitle = element_text(hjust = 0.5),
# legend.position = "none")#,
#
# # fig <- ggplotly(p),
# #
# # fig %>%
# # layout(hoverinfo = 'text',
# # text = ~paste('</br> Move: ', Move_pct, "%",
# # '</br> Shot: ', Shot_pct, "%",
# # '</br> Make: ', Make_pct, "%")))
# )
output$new <- renderPlot(
df %>%
filter(id == input$zone) %>%
mutate(pct_of_moves = round(pct_of_moves * 100, digits = 1)) %>%
ggplot() +
geom_rect(aes(xmin = x_min_next, xmax = x_max_next, ymin = y_min_next, ymax = y_max_next, fill = pct_of_moves, color = pct_of_moves)) +
geom_rect(aes(xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max), size = 1, color = "black", fill= NA) +
scale_color_gradient2(low = "lightblue", mid = "white", midpoint = med, high = "red") +
scale_fill_gradient2(low = "lightblue", mid = "white", midpoint = med, high = "red") +
theme_minimal() +
geom_segment(aes(x = 25 - 100, xend = 175 - 100, y = 0 - 42.5, yend = 0 - 42.5)) +
geom_segment(aes(x = 25 - 100, xend = 175 - 100, y = 85 - 42.5, yend = 85 - 42.5)) +
geom_segment(aes(x = 0 - 100, xend = 0 - 100, y = 20 - 42.5, yend = 60 - 42.5)) +
geom_segment(aes(x = 200 - 100, xend = 200 - 100, y = 20 - 42.5, yend = 60 - 42.5)) +
geom_curve(aes(x = 0 - 100, xend = 25 - 100, y = 60 - 42.5, yend = 85 - 42.5),
curvature = -0.32) +
geom_curve(aes(x = 0 - 100, xend = 25 - 100, y = 20 - 42.5, yend = 0 - 42.5),
curvature = 0.32) +
geom_curve(aes(x = 175 - 100, xend = 200 - 100, y = 0 - 42.5, yend = 20 - 42.5),
curvature = 0.32) +
geom_curve(aes(x = 175 - 100, xend = 200 -100, y = 85 - 42.5, yend = 60 - 42.5),
curvature = -0.32) +
geom_path(data = goal, aes(x = goal_x - 100, y = goal_y - 42.5)) +
geom_segment(aes(x = 100 - 100, xend = 100 - 100, y = 85 - 42.5, yend = 0 - 42.5), size = 2, color = "red") +
labs(title = "Movement Frequency from Zone to Zone", fill = "Movement%", color = "Movement%",
caption = "Ben Howell | @benhowell71 | benhowell71.com") +
geom_path(data = goal, aes(x = goal_x - 100, y = goal_y - 42.5), size = 2) +
theme(panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
plot.caption = element_text(hjust = 0.5, face = "italic", size = 10),
plot.subtitle = element_text(hjust = 0.5))
)
output$info <- renderText(
paste0(
'Move: ', unique(subset(df, id == input$zone)$Move_pct), "%\n",
'Shot: ', unique(subset(df, id == input$zone)$Shot_pct), "%\n",
'Make: ', unique(subset(df, id == input$zone)$Make_pct), "%\n"
)
)
}
# Run the application
shinyApp(ui = ui, server = server)