Spaces:
Build error
Build error
Update app.R
Browse files
app.R
CHANGED
|
@@ -15,55 +15,48 @@ summarise(
|
|
| 15 |
.groups = 'drop'
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
-
break_plot_Szn <- function(game,data1,sdate,edate) {
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
game <- game %>% filter(!is.na(pitch_abbr))
|
| 30 |
title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
|
| 31 |
-
# Get pitcher's handedness
|
| 32 |
pitcher_hand <- unique(game$phand)
|
| 33 |
|
| 34 |
angle_degrees <- round(mean(game$arm_angle,na.rm = TRUE),digits = 1)
|
| 35 |
-
# Calculate average movement by arm angle and pitch type from season data
|
| 36 |
-
# AAaveragePT <- data1 %>%
|
| 37 |
-
# mutate(arm_angle = round(arm_angle, digits = 0)) %>%
|
| 38 |
-
# group_by(arm_angle, phand, pitch_name) %>%
|
| 39 |
-
# summarise(
|
| 40 |
-
# AvgIVB = mean(IVB, na.rm = TRUE),
|
| 41 |
-
# AvgHB = mean(HB, na.rm = TRUE),
|
| 42 |
-
# .groups = 'drop'
|
| 43 |
-
# )
|
| 44 |
-
|
| 45 |
-
pitch_colors <- c(
|
| 46 |
-
"FF" = "#FF4136",
|
| 47 |
-
"SI" = "#FF851B",
|
| 48 |
-
"FC" = "#FFDC00",
|
| 49 |
-
"CH" = "#2ECC40",
|
| 50 |
-
"SL" = "#0074D9",
|
| 51 |
-
"ST" = "#ED68ED",
|
| 52 |
-
"CU" = "#B10DC9",
|
| 53 |
-
"FS" = "#01FF70",
|
| 54 |
-
"KC" = "#85144b",
|
| 55 |
-
"SV" = "#3D9970",
|
| 56 |
-
"KN" = "#39CCCC",
|
| 57 |
-
"FO" = "#F012BE",
|
| 58 |
-
"EP" = "#AAAAAA",
|
| 59 |
-
"FA" = "#7FDBFF",
|
| 60 |
-
"SC" = "#FF69B4"
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# Convert angle to radians
|
| 64 |
angle_radians <- angle_degrees * (pi / 180)
|
| 65 |
|
| 66 |
-
# Legend location based on handedness
|
| 67 |
if(pitcher_hand == "L") {
|
| 68 |
leg <- c(0.08, .22)
|
| 69 |
factor <- -1
|
|
@@ -72,70 +65,64 @@ break_plot_Szn <- function(game,data1,sdate,edate) {
|
|
| 72 |
factor <- 1
|
| 73 |
}
|
| 74 |
|
| 75 |
-
# Calculate the endpoint coordinates
|
| 76 |
x_end <- 50 * cos(angle_radians) * factor
|
| 77 |
y_end <- 50 * sin(angle_radians)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
avg_locations <- game %>%
|
| 80 |
group_by(pitch_abbr) %>%
|
| 81 |
summarize(
|
| 82 |
avg_HB = mean(HB, na.rm = TRUE),
|
| 83 |
-
avg_IVB = mean(IVB, na.rm = TRUE)
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
|
| 96 |
-
# Remove NAs that might have been introduced by the join
|
| 97 |
-
filter(!is.na(pitch_abbr)) %>%
|
| 98 |
-
filter(pitch_abbr %in% game_pitches)
|
| 99 |
|
| 100 |
ggplot(game, aes(x = HB, y = IVB)) +
|
| 101 |
-
# Base layers
|
| 102 |
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 103 |
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 104 |
-
|
| 105 |
-
# Add ellipses from season data for this arm angle
|
| 106 |
stat_ellipse(
|
| 107 |
data = arm_angle_data,
|
| 108 |
-
aes(
|
|
|
|
| 109 |
geom = "polygon",
|
| 110 |
alpha = 0.2,
|
| 111 |
level = 0.68,
|
| 112 |
show.legend = FALSE
|
| 113 |
) +
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
# filter(pitch_abbr %in% game_pitches),
|
| 125 |
-
# aes(x = AvgHB, y = AvgIVB, fill = pitch_abbr),
|
| 126 |
-
# color = "black",
|
| 127 |
-
# size = 6,
|
| 128 |
-
# stroke = .5,
|
| 129 |
-
# shape = 21
|
| 130 |
-
# ) +
|
| 131 |
-
geom_point(data = avg_locations, aes(x = avg_HB, y = avg_IVB, fill = pitch_abbr),
|
| 132 |
-
color = "black", size = 6, stroke = .5, shape = 21) +
|
| 133 |
# Arm angle line
|
| 134 |
-
geom_segment(x = 0, y = 0, xend = x_end, yend = y_end,
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
scale_fill_manual(values = pitch_colors) +
|
| 138 |
-
scale_color_manual(values = pitch_colors) +
|
| 139 |
labs(
|
| 140 |
x = "Horizontal Break (in)",
|
| 141 |
y = "Induced Vertical Break (in)",
|
|
@@ -170,46 +157,29 @@ arm_angle_data <- data1 %>%
|
|
| 170 |
panel.border = element_blank()
|
| 171 |
)
|
| 172 |
}
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
game <- game %>% filter(!is.na(pitch_abbr))
|
| 176 |
title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
|
| 177 |
-
# Get pitcher's handedness
|
| 178 |
pitcher_hand <- unique(game$phand)
|
| 179 |
|
| 180 |
angle_degrees <- round(mean(game$arm_angle,na.rm = TRUE),digits = 1)
|
| 181 |
-
# Calculate average movement by arm angle and pitch type from season data
|
| 182 |
-
# AAaveragePT <- data1 %>%
|
| 183 |
-
# mutate(arm_angle = round(arm_angle, digits = 0)) %>%
|
| 184 |
-
# group_by(arm_angle, phand, pitch_name) %>%
|
| 185 |
-
# summarise(
|
| 186 |
-
# AvgIVB = mean(IVB, na.rm = TRUE),
|
| 187 |
-
# AvgHB = mean(HB, na.rm = TRUE),
|
| 188 |
-
# .groups = 'drop'
|
| 189 |
-
# )
|
| 190 |
-
|
| 191 |
-
pitch_colors <- c(
|
| 192 |
-
"FF" = "#FF4136",
|
| 193 |
-
"SI" = "#FF851B",
|
| 194 |
-
"FC" = "#FFDC00",
|
| 195 |
-
"CH" = "#2ECC40",
|
| 196 |
-
"SL" = "#0074D9",
|
| 197 |
-
"ST" = "#ED68ED",
|
| 198 |
-
"CU" = "#B10DC9",
|
| 199 |
-
"FS" = "#01FF70",
|
| 200 |
-
"KC" = "#85144b",
|
| 201 |
-
"SV" = "#3D9970",
|
| 202 |
-
"KN" = "#39CCCC",
|
| 203 |
-
"FO" = "#F012BE",
|
| 204 |
-
"EP" = "#AAAAAA",
|
| 205 |
-
"FA" = "#7FDBFF",
|
| 206 |
-
"SC" = "#FF69B4"
|
| 207 |
-
)
|
| 208 |
-
|
| 209 |
-
# Convert angle to radians
|
| 210 |
angle_radians <- angle_degrees * (pi / 180)
|
| 211 |
|
| 212 |
-
# Legend location based on handedness
|
| 213 |
if(pitcher_hand == "L") {
|
| 214 |
leg <- c(0.08, .22)
|
| 215 |
factor <- -1
|
|
@@ -218,71 +188,72 @@ break_plot_tot <- function(game,data1,sdate,edate) {
|
|
| 218 |
factor <- 1
|
| 219 |
}
|
| 220 |
|
| 221 |
-
# Calculate the endpoint coordinates
|
| 222 |
x_end <- 50 * cos(angle_radians) * factor
|
| 223 |
y_end <- 50 * sin(angle_radians)
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
avg_locations <- game %>%
|
| 226 |
group_by(pitch_abbr) %>%
|
| 227 |
summarize(
|
| 228 |
avg_HB = mean(HB, na.rm = TRUE),
|
| 229 |
-
avg_IVB = mean(IVB, na.rm = TRUE)
|
| 230 |
-
|
|
|
|
| 231 |
|
| 232 |
-
#
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
-
# Filter season data for the current arm angle, handedness, and only pitches in the game
|
| 236 |
arm_angle_data <- data1 %>%
|
| 237 |
-
|
| 238 |
-
filter(!is.na(arm_angle), !is.na(angle_degrees), !is.na(phand), !is.na(pitcher_hand), !is.na(pitch_name)) %>%
|
| 239 |
-
# Then proceed with your original filtering
|
| 240 |
filter(abs(round(arm_angle) - angle_degrees) <= 2, phand == pitcher_hand) %>%
|
| 241 |
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
|
| 242 |
-
# Remove NAs that might have been introduced by the join
|
| 243 |
filter(!is.na(pitch_abbr)) %>%
|
| 244 |
filter(pitch_abbr %in% game_pitches)
|
| 245 |
|
| 246 |
-
game <- game %>% filter(!is.na(pitch_abbr))
|
| 247 |
ggplot(game, aes(x = HB, y = IVB)) +
|
| 248 |
-
# Base layers
|
| 249 |
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 250 |
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 251 |
-
|
| 252 |
-
# Add ellipses from season data for this arm angle
|
| 253 |
stat_ellipse(
|
| 254 |
data = arm_angle_data,
|
| 255 |
-
aes(
|
|
|
|
| 256 |
geom = "polygon",
|
| 257 |
alpha = 0.2,
|
| 258 |
level = 0.68,
|
| 259 |
show.legend = FALSE
|
| 260 |
) +
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
#
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
color = "black", size = 6, stroke = .5, shape = 21) +
|
| 280 |
# Arm angle line
|
| 281 |
-
geom_segment(x = 0, y = 0, xend = x_end, yend = y_end,
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
scale_fill_manual(values = pitch_colors) +
|
| 285 |
-
scale_color_manual(values = pitch_colors) +
|
| 286 |
labs(
|
| 287 |
x = "Horizontal Break (in)",
|
| 288 |
y = "Induced Vertical Break (in)",
|
|
@@ -317,11 +288,10 @@ break_plot_tot <- function(game,data1,sdate,edate) {
|
|
| 317 |
panel.border = element_blank()
|
| 318 |
)
|
| 319 |
}
|
| 320 |
-
|
| 321 |
ui <- fluidPage(
|
| 322 |
# Application title
|
| 323 |
titlePanel("2020-2024 MLB Pitch Plots"),
|
| 324 |
-
|
| 325 |
sidebarLayout(
|
| 326 |
sidebarPanel(
|
| 327 |
width = 3,
|
|
@@ -340,7 +310,7 @@ ui <- fluidPage(
|
|
| 340 |
class = "btn btn-success btn-block")
|
| 341 |
),
|
| 342 |
|
| 343 |
-
|
| 344 |
mainPanel(
|
| 345 |
width = 9,
|
| 346 |
plotOutput("Plot")
|
|
@@ -349,17 +319,29 @@ ui <- fluidPage(
|
|
| 349 |
)
|
| 350 |
|
| 351 |
# Define server
|
| 352 |
-
server <- function(input, output) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
| 358 |
})
|
| 359 |
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
if(nrow(game) == 0) {
|
| 365 |
return(ggplot() +
|
|
@@ -370,37 +352,42 @@ server <- function(input, output) {
|
|
| 370 |
theme(plot.background = element_rect(fill = "#333333", color = NA)))
|
| 371 |
}
|
| 372 |
|
| 373 |
-
if(
|
| 374 |
-
break_plot_Szn(game,
|
| 375 |
} else{
|
| 376 |
-
break_plot_tot(game,
|
| 377 |
}
|
| 378 |
-
})
|
| 379 |
-
|
| 380 |
-
output$Plot <- renderPlot({
|
| 381 |
-
current_plot()
|
| 382 |
}, width = 1000, height = 1000)
|
| 383 |
|
| 384 |
output$download <- downloadHandler(
|
| 385 |
filename = function() {
|
| 386 |
-
|
| 387 |
paste0(
|
| 388 |
-
gsub(" ", "_",
|
| 389 |
-
format(
|
| 390 |
-
format(
|
| 391 |
)
|
| 392 |
},
|
| 393 |
content = function(file) {
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
ggsave(file,
|
| 396 |
-
plot =
|
| 397 |
-
width =
|
| 398 |
-
height =
|
| 399 |
dpi = 300,
|
| 400 |
bg = "#333333")
|
| 401 |
}
|
| 402 |
)
|
| 403 |
}
|
| 404 |
|
| 405 |
-
# Run the application
|
| 406 |
shinyApp(ui = ui, server = server)
|
|
|
|
| 15 |
.groups = 'drop'
|
| 16 |
)
|
| 17 |
|
| 18 |
+
pitch_colors <- c(
|
| 19 |
+
"FF" = "#FF4136",
|
| 20 |
+
"SI" = "#FF851B",
|
| 21 |
+
"FC" = "#FFDC00",
|
| 22 |
+
"CH" = "#2ECC40",
|
| 23 |
+
"SL" = "#0074D9",
|
| 24 |
+
"ST" = "#ED68ED",
|
| 25 |
+
"CU" = "#B10DC9",
|
| 26 |
+
"FS" = "#01FF70",
|
| 27 |
+
"KC" = "#85144b",
|
| 28 |
+
"SV" = "#3D9970",
|
| 29 |
+
"KN" = "#39CCCC",
|
| 30 |
+
"FO" = "#F012BE",
|
| 31 |
+
"EP" = "#AAAAAA",
|
| 32 |
+
"FA" = "#7FDBFF",
|
| 33 |
+
"SC" = "#FF69B4"
|
| 34 |
)
|
| 35 |
|
| 36 |
+
break_plot_Szn <- function(game, data1, sdate, edate) {
|
| 37 |
+
# Calculate pitch usage percentages
|
| 38 |
+
|
| 39 |
+
game <- game %>% filter(between(as.Date(date), sdate, edate))
|
| 40 |
+
total_pitches <- nrow(game)
|
| 41 |
+
usage_stats <- game %>%
|
| 42 |
+
group_by(pitch_name) %>%
|
| 43 |
+
summarise(
|
| 44 |
+
count = n(),
|
| 45 |
+
usage = sprintf("%.1f%%", (count/total_pitches) * 100)
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
game <- game %>%
|
| 49 |
+
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
|
| 50 |
+
left_join(usage_stats, by = "pitch_name")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
game <- game %>% filter(!is.na(pitch_abbr))
|
| 54 |
title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
|
|
|
|
| 55 |
pitcher_hand <- unique(game$phand)
|
| 56 |
|
| 57 |
angle_degrees <- round(mean(game$arm_angle,na.rm = TRUE),digits = 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
angle_radians <- angle_degrees * (pi / 180)
|
| 59 |
|
|
|
|
| 60 |
if(pitcher_hand == "L") {
|
| 61 |
leg <- c(0.08, .22)
|
| 62 |
factor <- -1
|
|
|
|
| 65 |
factor <- 1
|
| 66 |
}
|
| 67 |
|
|
|
|
| 68 |
x_end <- 50 * cos(angle_radians) * factor
|
| 69 |
y_end <- 50 * sin(angle_radians)
|
| 70 |
|
| 71 |
+
# Create the base color mapping
|
| 72 |
+
game_pitches <- unique(game$pitch_abbr)
|
| 73 |
+
used_colors <- pitch_colors[game_pitches]
|
| 74 |
+
|
| 75 |
+
# Add usage to the game data
|
| 76 |
avg_locations <- game %>%
|
| 77 |
group_by(pitch_abbr) %>%
|
| 78 |
summarize(
|
| 79 |
avg_HB = mean(HB, na.rm = TRUE),
|
| 80 |
+
avg_IVB = mean(IVB, na.rm = TRUE),
|
| 81 |
+
usage = first(usage) # Keep the usage info
|
| 82 |
+
)
|
| 83 |
|
| 84 |
+
# Create labels with percentages
|
| 85 |
+
legend_labels <- paste0(names(used_colors), " (",
|
| 86 |
+
avg_locations$usage[match(names(used_colors), avg_locations$pitch_abbr)], ")")
|
| 87 |
+
|
| 88 |
+
# Create the color scale with usage labels
|
| 89 |
+
fill_values <- used_colors
|
| 90 |
+
names(fill_values) <- legend_labels
|
| 91 |
|
| 92 |
+
arm_angle_data <- data1 %>%
|
| 93 |
+
filter(!is.na(arm_angle), !is.na(phand), !is.na(pitch_name)) %>%
|
| 94 |
+
filter(abs(round(arm_angle) - angle_degrees) <= 2, phand == pitcher_hand) %>%
|
| 95 |
+
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
|
| 96 |
+
filter(!is.na(pitch_abbr)) %>%
|
| 97 |
+
filter(pitch_abbr %in% game_pitches)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
ggplot(game, aes(x = HB, y = IVB)) +
|
|
|
|
| 100 |
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 101 |
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 102 |
+
# Ellipses
|
|
|
|
| 103 |
stat_ellipse(
|
| 104 |
data = arm_angle_data,
|
| 105 |
+
aes(fill = paste0(pitch_abbr, " (",
|
| 106 |
+
avg_locations$usage[match(pitch_abbr, avg_locations$pitch_abbr)], ")")),
|
| 107 |
geom = "polygon",
|
| 108 |
alpha = 0.2,
|
| 109 |
level = 0.68,
|
| 110 |
show.legend = FALSE
|
| 111 |
) +
|
| 112 |
+
# Average points
|
| 113 |
+
geom_point(
|
| 114 |
+
data = avg_locations,
|
| 115 |
+
aes(x = avg_HB, y = avg_IVB,
|
| 116 |
+
fill = paste0(pitch_abbr, " (", usage, ")")),
|
| 117 |
+
color = "black",
|
| 118 |
+
size = 6,
|
| 119 |
+
stroke = .5,
|
| 120 |
+
shape = 21
|
| 121 |
+
) +
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Arm angle line
|
| 123 |
+
geom_segment(x = 0, y = 0, xend = x_end, yend = y_end,
|
| 124 |
+
color = "red", linewidth = 1, linetype = 5) +
|
| 125 |
+
scale_fill_manual(values = fill_values) +
|
|
|
|
|
|
|
| 126 |
labs(
|
| 127 |
x = "Horizontal Break (in)",
|
| 128 |
y = "Induced Vertical Break (in)",
|
|
|
|
| 157 |
panel.border = element_blank()
|
| 158 |
)
|
| 159 |
}
|
| 160 |
+
|
| 161 |
+
break_plot_tot <- function(game, data1, sdate, edate) {
|
| 162 |
+
# Calculate pitch usage percentages
|
| 163 |
+
game <- game %>% filter(between(as.Date(date), sdate, edate))
|
| 164 |
+
total_pitches <- nrow(game)
|
| 165 |
+
usage_stats <- game %>%
|
| 166 |
+
group_by(pitch_name) %>%
|
| 167 |
+
summarise(
|
| 168 |
+
count = n(),
|
| 169 |
+
usage = sprintf("%.1f%%", (count/total_pitches) * 100)
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
game <- game %>%
|
| 173 |
+
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
|
| 174 |
+
left_join(usage_stats, by = "pitch_name")
|
| 175 |
+
|
| 176 |
game <- game %>% filter(!is.na(pitch_abbr))
|
| 177 |
title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
|
|
|
|
| 178 |
pitcher_hand <- unique(game$phand)
|
| 179 |
|
| 180 |
angle_degrees <- round(mean(game$arm_angle,na.rm = TRUE),digits = 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
angle_radians <- angle_degrees * (pi / 180)
|
| 182 |
|
|
|
|
| 183 |
if(pitcher_hand == "L") {
|
| 184 |
leg <- c(0.08, .22)
|
| 185 |
factor <- -1
|
|
|
|
| 188 |
factor <- 1
|
| 189 |
}
|
| 190 |
|
|
|
|
| 191 |
x_end <- 50 * cos(angle_radians) * factor
|
| 192 |
y_end <- 50 * sin(angle_radians)
|
| 193 |
|
| 194 |
+
# Create the base color mapping
|
| 195 |
+
game_pitches <- unique(game$pitch_abbr)
|
| 196 |
+
used_colors <- pitch_colors[game_pitches]
|
| 197 |
+
|
| 198 |
+
# Add usage to the game data
|
| 199 |
avg_locations <- game %>%
|
| 200 |
group_by(pitch_abbr) %>%
|
| 201 |
summarize(
|
| 202 |
avg_HB = mean(HB, na.rm = TRUE),
|
| 203 |
+
avg_IVB = mean(IVB, na.rm = TRUE),
|
| 204 |
+
usage = first(usage) # Keep the usage info
|
| 205 |
+
)
|
| 206 |
|
| 207 |
+
# Create labels with percentages
|
| 208 |
+
legend_labels <- paste0(names(used_colors), " (",
|
| 209 |
+
avg_locations$usage[match(names(used_colors), avg_locations$pitch_abbr)], ")")
|
| 210 |
+
|
| 211 |
+
# Create the color scale with usage labels
|
| 212 |
+
fill_values <- used_colors
|
| 213 |
+
names(fill_values) <- legend_labels
|
| 214 |
|
|
|
|
| 215 |
arm_angle_data <- data1 %>%
|
| 216 |
+
filter(!is.na(arm_angle), !is.na(phand), !is.na(pitch_name)) %>%
|
|
|
|
|
|
|
| 217 |
filter(abs(round(arm_angle) - angle_degrees) <= 2, phand == pitcher_hand) %>%
|
| 218 |
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
|
|
|
|
| 219 |
filter(!is.na(pitch_abbr)) %>%
|
| 220 |
filter(pitch_abbr %in% game_pitches)
|
| 221 |
|
|
|
|
| 222 |
ggplot(game, aes(x = HB, y = IVB)) +
|
|
|
|
| 223 |
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 224 |
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 225 |
+
# Ellipses
|
|
|
|
| 226 |
stat_ellipse(
|
| 227 |
data = arm_angle_data,
|
| 228 |
+
aes(fill = paste0(pitch_abbr, " (",
|
| 229 |
+
avg_locations$usage[match(pitch_abbr, avg_locations$pitch_abbr)], ")")),
|
| 230 |
geom = "polygon",
|
| 231 |
alpha = 0.2,
|
| 232 |
level = 0.68,
|
| 233 |
show.legend = FALSE
|
| 234 |
) +
|
| 235 |
+
# Individual points
|
| 236 |
+
geom_point(
|
| 237 |
+
aes(fill = paste0(pitch_abbr, " (", usage, ")")),
|
| 238 |
+
color = "#c6c6c6",
|
| 239 |
+
size = 3,
|
| 240 |
+
stroke = .5,
|
| 241 |
+
shape = 21
|
| 242 |
+
) +
|
| 243 |
+
# Average points
|
| 244 |
+
geom_point(
|
| 245 |
+
data = avg_locations,
|
| 246 |
+
aes(x = avg_HB, y = avg_IVB,
|
| 247 |
+
fill = paste0(pitch_abbr, " (", usage, ")")),
|
| 248 |
+
color = "black",
|
| 249 |
+
size = 6,
|
| 250 |
+
stroke = .5,
|
| 251 |
+
shape = 21
|
| 252 |
+
) +
|
|
|
|
| 253 |
# Arm angle line
|
| 254 |
+
geom_segment(x = 0, y = 0, xend = x_end, yend = y_end,
|
| 255 |
+
color = "red", linewidth = 1, linetype = 5) +
|
| 256 |
+
scale_fill_manual(values = fill_values) +
|
|
|
|
|
|
|
| 257 |
labs(
|
| 258 |
x = "Horizontal Break (in)",
|
| 259 |
y = "Induced Vertical Break (in)",
|
|
|
|
| 288 |
panel.border = element_blank()
|
| 289 |
)
|
| 290 |
}
|
|
|
|
| 291 |
ui <- fluidPage(
|
| 292 |
# Application title
|
| 293 |
titlePanel("2020-2024 MLB Pitch Plots"),
|
| 294 |
+
|
| 295 |
sidebarLayout(
|
| 296 |
sidebarPanel(
|
| 297 |
width = 3,
|
|
|
|
| 310 |
class = "btn btn-success btn-block")
|
| 311 |
),
|
| 312 |
|
| 313 |
+
|
| 314 |
mainPanel(
|
| 315 |
width = 9,
|
| 316 |
plotOutput("Plot")
|
|
|
|
| 319 |
)
|
| 320 |
|
| 321 |
# Define server
|
| 322 |
+
server <- function(input, output, session) {
|
| 323 |
+
# Create a reactive value to store current plot settings
|
| 324 |
+
plotSettings <- reactiveVal(list(
|
| 325 |
+
player = NULL,
|
| 326 |
+
type = "Season Average",
|
| 327 |
+
dates = c(as.Date("2024-03-20"), as.Date("2024-10-01"))
|
| 328 |
+
))
|
| 329 |
|
| 330 |
+
# Update settings only when submit is clicked
|
| 331 |
+
observeEvent(input$submit, {
|
| 332 |
+
plotSettings(list(
|
| 333 |
+
player = input$player,
|
| 334 |
+
type = input$type,
|
| 335 |
+
dates = c(input$date1[1], input$date1[2])
|
| 336 |
+
))
|
| 337 |
})
|
| 338 |
|
| 339 |
+
output$Plot <- renderPlot({
|
| 340 |
+
settings <- plotSettings()
|
| 341 |
+
req(settings$player) # Wait until we have a player selected
|
| 342 |
+
|
| 343 |
+
game <- data1 %>%
|
| 344 |
+
filter(`Pitcher Name` == settings$player)
|
| 345 |
|
| 346 |
if(nrow(game) == 0) {
|
| 347 |
return(ggplot() +
|
|
|
|
| 352 |
theme(plot.background = element_rect(fill = "#333333", color = NA)))
|
| 353 |
}
|
| 354 |
|
| 355 |
+
if(settings$type == "Season Average"){
|
| 356 |
+
break_plot_Szn(game, data1, settings$dates[1], settings$dates[2])
|
| 357 |
} else{
|
| 358 |
+
break_plot_tot(game, data1, settings$dates[1], settings$dates[2])
|
| 359 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
}, width = 1000, height = 1000)
|
| 361 |
|
| 362 |
output$download <- downloadHandler(
|
| 363 |
filename = function() {
|
| 364 |
+
settings <- plotSettings()
|
| 365 |
paste0(
|
| 366 |
+
gsub(" ", "_", settings$player), "_",
|
| 367 |
+
format(settings$dates[1], "%Y%m%d"), "_to_",
|
| 368 |
+
format(settings$dates[2], "%Y%m%d"), ".png"
|
| 369 |
)
|
| 370 |
},
|
| 371 |
content = function(file) {
|
| 372 |
+
settings <- plotSettings()
|
| 373 |
+
|
| 374 |
+
game <- data1 %>%
|
| 375 |
+
filter(`Pitcher Name` == settings$player)
|
| 376 |
+
|
| 377 |
+
plot <- if(settings$type == "Season Average"){
|
| 378 |
+
break_plot_Szn(game, data1, settings$dates[1], settings$dates[2])
|
| 379 |
+
} else{
|
| 380 |
+
break_plot_tot(game, data1, settings$dates[1], settings$dates[2])
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
ggsave(file,
|
| 384 |
+
plot = plot,
|
| 385 |
+
width = 10,
|
| 386 |
+
height = 10,
|
| 387 |
dpi = 300,
|
| 388 |
bg = "#333333")
|
| 389 |
}
|
| 390 |
)
|
| 391 |
}
|
| 392 |
|
|
|
|
| 393 |
shinyApp(ui = ui, server = server)
|