Spaces:
Build error
Build error
File size: 12,239 Bytes
971fe13 7e1e22e 971fe13 7e1e22e 971fe13 7e1e22e 971fe13 7e1e22e 971fe13 7e1e22e 971fe13 7e1e22e | 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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | library(shiny)
library(tidyverse)
data1 <- read.csv("data20-24.csv", header = TRUE, check.names = FALSE, fileEncoding = "UTF-8")
AAaveragePT <- data1 %>%
mutate(arm_angle = round(arm_angle, digits = 0)) %>%
group_by(arm_angle, phand, pitch_name) %>%
summarise(
AvgIVB = mean(IVB, na.rm = TRUE),
AvgHB = mean(HB, na.rm = TRUE),
.groups = 'drop'
)
break_plot_Szn <- function(game,data1,sdate,edate) {
game <- game %>% left_join(pitch_type_lookup, by = c("pitch_name"))
title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
# Get pitcher's handedness
pitcher_hand <- unique(game$phand)
angle_degrees <- round(mean(game$arm_angle,na.rm = TRUE),digits = 1)
# Calculate average movement by arm angle and pitch type from season data
# AAaveragePT <- data1 %>%
# mutate(arm_angle = round(arm_angle, digits = 0)) %>%
# group_by(arm_angle, phand, pitch_name) %>%
# summarise(
# AvgIVB = mean(IVB, na.rm = TRUE),
# AvgHB = mean(HB, na.rm = TRUE),
# .groups = 'drop'
# )
pitch_colors <- c(
"FF" = "#FF4136",
"SI" = "#FF851B",
"FC" = "#FFDC00",
"CH" = "#2ECC40",
"SL" = "#0074D9",
"ST" = "#ED68ED",
"CU" = "#B10DC9",
"FS" = "#01FF70",
"KC" = "#85144b",
"SV" = "#3D9970",
"KN" = "#39CCCC",
"FO" = "#F012BE",
"EP" = "#AAAAAA",
"FA" = "#7FDBFF",
"SC" = "#FF69B4"
)
# Convert angle to radians
angle_radians <- angle_degrees * (pi / 180)
# Legend location based on handedness
if(pitcher_hand == "L") {
leg <- c(0.08, .22)
factor <- -1
} else {
leg <- c(0.92, .22)
factor <- 1
}
# Calculate the endpoint coordinates
x_end <- 50 * cos(angle_radians) * factor
y_end <- 50 * sin(angle_radians)
avg_locations <- game %>%
group_by(pitch_abbr) %>%
summarize(
avg_HB = mean(HB, na.rm = TRUE),
avg_IVB = mean(IVB, na.rm = TRUE)
)
# Get unique pitch types from the game data
game_pitches <- unique(game$pitch_abbr)
# Filter season data for the current arm angle, handedness, and only pitches in the game
arm_angle_data <- data1 %>%
filter(abs(round(arm_angle) - angle_degrees) <= 2,
phand == pitcher_hand) %>%
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
filter(pitch_abbr %in% game_pitches)
ggplot(game, aes(x = HB, y = IVB)) +
# Base layers
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
# Add ellipses from season data for this arm angle
stat_ellipse(
data = arm_angle_data,
aes(x = HB, y = IVB, fill = pitch_abbr),
geom = "polygon",
alpha = 0.2,
level = 0.68,
show.legend = FALSE
) +
# Individual pitch points from the game
# geom_point(aes(fill = pitch_abbr), color = "#c6c6c6", size = 3, stroke = .5, shape = 21) +
# # Average location points from season data
# geom_point(
# data = AAaveragePT %>%
# filter(arm_angle == angle_degrees,
# phand == pitcher_hand) %>%
# left_join(pitch_type_lookup, by = c("pitch_name")) %>%
# filter(pitch_abbr %in% game_pitches),
# aes(x = AvgHB, y = AvgIVB, fill = pitch_abbr),
# color = "black",
# size = 6,
# stroke = .5,
# shape = 21
# ) +
geom_point(data = avg_locations, aes(x = avg_HB, y = avg_IVB, fill = pitch_abbr),
color = "black", size = 6, stroke = .5, shape = 21) +
# Arm angle line
geom_segment(x = 0, y = 0, xend = x_end, yend = y_end, color = "red", linewidth = 1, linetype = 5) +
# Aesthetics
scale_fill_manual(values = pitch_colors) +
scale_color_manual(values = pitch_colors) +
labs(
x = "Horizontal Break (in)",
y = "Induced Vertical Break (in)",
title = title,
subtitle = paste0("Arm Angle: ", angle_degrees, "\u00b0"),
caption = "Data: MLB | Viz: @TimStats\nEllipses show 1σ of movement for same-handed pitchers at similar arm angles"
) +
xlim(-25, 25) +
ylim(-25, 25) +
theme_minimal() +
theme(
legend.position = leg,
plot.title = element_text(hjust = 0.5, face = "bold", color = "white"),
plot.subtitle = element_text(hjust = 0.5, face = "italic", color = "white"),
aspect.ratio = 1,
plot.background = element_rect(fill = "#333333", color = NA),
panel.background = element_rect(fill = "#333333", color = NA),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
legend.background = element_rect(fill = "#333333"),
legend.text = element_text(color = "white"),
legend.title = element_blank(),
plot.margin = margin(10, 5, 10, 5),
axis.line = element_blank(),
axis.ticks = element_line(color = "white"),
panel.grid = element_blank(),
legend.key = element_blank(),
plot.caption = element_text(
color = "white",
hjust = 0.5
),
panel.border = element_blank()
)
}
break_plot_tot <- function(game,data1,sdate,edate) {
game <- game %>% left_join(pitch_type_lookup, by = c("pitch_name"))
title <- paste0(unique(game$`Pitcher Name`)," ",sdate," to ",edate)
# Get pitcher's handedness
pitcher_hand <- unique(game$phand)
angle_degrees <- round(mean(game$arm_angle,na.rm = TRUE),digits = 1)
# Calculate average movement by arm angle and pitch type from season data
# AAaveragePT <- data1 %>%
# mutate(arm_angle = round(arm_angle, digits = 0)) %>%
# group_by(arm_angle, phand, pitch_name) %>%
# summarise(
# AvgIVB = mean(IVB, na.rm = TRUE),
# AvgHB = mean(HB, na.rm = TRUE),
# .groups = 'drop'
# )
pitch_colors <- c(
"FF" = "#FF4136",
"SI" = "#FF851B",
"FC" = "#FFDC00",
"CH" = "#2ECC40",
"SL" = "#0074D9",
"ST" = "#ED68ED",
"CU" = "#B10DC9",
"FS" = "#01FF70",
"KC" = "#85144b",
"SV" = "#3D9970",
"KN" = "#39CCCC",
"FO" = "#F012BE",
"EP" = "#AAAAAA",
"FA" = "#7FDBFF",
"SC" = "#FF69B4"
)
# Convert angle to radians
angle_radians <- angle_degrees * (pi / 180)
# Legend location based on handedness
if(pitcher_hand == "L") {
leg <- c(0.08, .22)
factor <- -1
} else {
leg <- c(0.92, .22)
factor <- 1
}
# Calculate the endpoint coordinates
x_end <- 50 * cos(angle_radians) * factor
y_end <- 50 * sin(angle_radians)
avg_locations <- game %>%
group_by(pitch_abbr) %>%
summarize(
avg_HB = mean(HB, na.rm = TRUE),
avg_IVB = mean(IVB, na.rm = TRUE)
)
# Get unique pitch types from the game data
game_pitches <- unique(game$pitch_abbr)
# Filter season data for the current arm angle, handedness, and only pitches in the game
arm_angle_data <- data1 %>%
filter(abs(round(arm_angle) - angle_degrees) <= 2,
phand == pitcher_hand) %>%
left_join(pitch_type_lookup, by = c("pitch_name")) %>%
filter(pitch_abbr %in% game_pitches)
ggplot(game, aes(x = HB, y = IVB)) +
# Base layers
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
# Add ellipses from season data for this arm angle
stat_ellipse(
data = arm_angle_data,
aes(x = HB, y = IVB, fill = pitch_abbr),
geom = "polygon",
alpha = 0.2,
level = 0.68,
show.legend = FALSE
) +
# Individual pitch points from the game
geom_point(aes(fill = pitch_abbr), color = "#c6c6c6", size = 3, stroke = .5, shape = 21) +
# # Average location points from season data
# geom_point(
# data = AAaveragePT %>%
# filter(arm_angle == angle_degrees,
# phand == pitcher_hand) %>%
# left_join(pitch_type_lookup, by = c("pitch_name")) %>%
# filter(pitch_abbr %in% game_pitches),
# aes(x = AvgHB, y = AvgIVB, fill = pitch_abbr),
# color = "black",
# size = 6,
# stroke = .5,
# shape = 21
# ) +
geom_point(data = avg_locations, aes(x = avg_HB, y = avg_IVB, fill = pitch_abbr),
color = "black", size = 6, stroke = .5, shape = 21) +
# Arm angle line
geom_segment(x = 0, y = 0, xend = x_end, yend = y_end, color = "red", linewidth = 1, linetype = 5) +
# Aesthetics
scale_fill_manual(values = pitch_colors) +
scale_color_manual(values = pitch_colors) +
labs(
x = "Horizontal Break (in)",
y = "Induced Vertical Break (in)",
title = title,
subtitle = paste0("Arm Angle: ", angle_degrees, "\u00b0"),
caption = "Data: MLB | Viz: @TimStats\nEllipses show 1σ of movement for same-handed pitchers at similar arm angles"
) +
xlim(-25, 25) +
ylim(-25, 25) +
theme_minimal() +
theme(
legend.position = leg,
plot.title = element_text(hjust = 0.5, face = "bold", color = "white"),
plot.subtitle = element_text(hjust = 0.5, face = "italic", color = "white"),
aspect.ratio = 1,
plot.background = element_rect(fill = "#333333", color = NA),
panel.background = element_rect(fill = "#333333", color = NA),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
legend.background = element_rect(fill = "#333333"),
legend.text = element_text(color = "white"),
legend.title = element_blank(),
plot.margin = margin(10, 5, 10, 5),
axis.line = element_blank(),
axis.ticks = element_line(color = "white"),
panel.grid = element_blank(),
legend.key = element_blank(),
plot.caption = element_text(
color = "white",
hjust = 0.5
),
panel.border = element_blank()
)
}
ui <- fluidPage(
# Application title
titlePanel("2020-2024 MLB Pitch Plots"),
sidebarLayout(
sidebarPanel(
width = 3,
selectInput("player", "Select Player:", choices = unique(data1$`Pitcher Name`),
width = "100%"),
dateRangeInput("date1", "Date Range:",
start = "2024-02-09",
end = "2024-09-05",
width = "100%"),
radioButtons("type", "Plot Type",
choices = c("Season Average","All Pitches")),
actionButton("submit", "Update Plot",
class = "btn btn-primary btn-block",
style = "margin-bottom: 10px"),
downloadButton("download", "Download Plot",
class = "btn btn-success btn-block")
),
mainPanel(
width = 9,
plotOutput("Plot")
)
)
)
# Define server
server <- function(input, output) {
filtered_data <- eventReactive(input$submit, {
data1 %>%
filter(`Pitcher Name` == input$player,
between(as.Date(date), input$date1[1], input$date1[2]))
})
current_plot <- reactive({
game <- filtered_data()
season_data <- data1
if(nrow(game) == 0) {
return(ggplot() +
annotate("text", x = 0.5, y = 0.5,
label = "No data available for selected date range",
color = "white") +
theme_void() +
theme(plot.background = element_rect(fill = "#333333", color = NA)))
}
if(input$type == "Season Average"){
break_plot_Szn(game, season_data, input$date1[1], input$date1[2])
} else{
break_plot_tot(game, season_data, input$date1[1], input$date1[2])
}
})
output$Plot <- renderPlot({
current_plot()
}, width = 1000, height = 1000)
output$download <- downloadHandler(
filename = function() {
paste0(
gsub(" ", "_", input$player), "_",
format(input$date1[1], "%Y%m%d"), "_to_",
format(input$date1[2], "%Y%m%d"), ".png"
)
},
content = function(file) {
ggsave(file,
plot = current_plot(),
width = 7,
height = 7,
dpi = 300,
bg = "#333333")
}
)
}
# Run the application
shinyApp(ui = ui, server = server) |