TimStats commited on
Commit
bd9570c
·
verified ·
1 Parent(s): d78bb22

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +39 -9
app.R CHANGED
@@ -236,11 +236,20 @@ getBoxScore <- function(game_pk, pid) {
236
  }
237
 
238
  # Create a new function for split pitch location plots
 
 
 
239
  pitch_plot_split <- function(game, title) {
240
  game_lhb <- game %>% filter(bhand == "L")
241
  game_rhb <- game %>% filter(bhand == "R")
242
 
243
- pitch_colors <- c(
 
 
 
 
 
 
244
  "Four-Seam Fastball"= "#FF4136",
245
  "Sinker"= "#FF851B",
246
  "Cutter"= "#FFDC00",
@@ -257,39 +266,59 @@ pitch_colors <- c(
257
  "Fastball"= "#7FDBFF",
258
  "Slow Curve"= "#DDDDDD",
259
  "Screwball"= "#FF69B4"
260
- )
 
 
261
  plot_lhb <- ggplot(game_lhb, aes(x = px, y = pz, color = pitch_name)) +
 
 
 
 
 
 
 
 
 
262
  geom_point(size = 3) +
263
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
264
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
265
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
266
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
267
  scale_color_manual(values = pitch_colors) +
268
- labs(x = NULL, y = NULL, title = "LHB (Batter's Perspective)") +
269
  xlim(-3, 3) +
270
- ylim(0.2, 4) +
271
  coord_fixed(ratio = 1) +
272
- theme_minimal() +
273
  theme(
274
  legend.position = "none",
275
  plot.title = element_text(hjust = 0.5, face = "bold"),
276
  axis.text = element_blank(),
277
  axis.ticks = element_blank()
278
  )
279
- # scale_color_manual(values = pitch_colors)
280
 
 
281
  plot_rhb <- ggplot(game_rhb, aes(x = px, y = pz, color = pitch_name)) +
 
 
 
 
 
 
 
 
 
282
  geom_point(size = 3) +
283
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
284
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
285
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
286
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
287
  scale_color_manual(values = pitch_colors) +
288
- labs(x = NULL, y = NULL, title = "RHB (Batter's Perspective)") +
289
  xlim(-3, 3) +
290
- ylim(0.2, 4) +
291
  coord_fixed(ratio = 1) +
292
- theme_minimal() +
293
  theme(
294
  legend.position = "none",
295
  plot.title = element_text(hjust = 0.5, face = "bold"),
@@ -300,6 +329,7 @@ pitch_colors <- c(
300
  return(list(lhb = plot_lhb, rhb = plot_rhb))
301
  }
302
 
 
303
  calculate_timstuff <- function(game) {
304
  game <- game %>%
305
  mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),
 
236
  }
237
 
238
  # Create a new function for split pitch location plots
239
+ left_batter <- png::readPNG("left_batter.png")
240
+ right_batter <- png::readPNG("right_batter.png")
241
+
242
  pitch_plot_split <- function(game, title) {
243
  game_lhb <- game %>% filter(bhand == "L")
244
  game_rhb <- game %>% filter(bhand == "R")
245
 
246
+ # Define home plate coordinates (adjusted position)
247
+ home_plate <- data.frame(
248
+ x = c(0.6, -0.6, -0.7083, 0, 0.7083), # Keep x-coordinates
249
+ y = c(0.5, 0.5, 0.25, 0, 0.25) # Adjust y-coordinates to put point at 0
250
+ )
251
+
252
+ pitch_colors <- c(
253
  "Four-Seam Fastball"= "#FF4136",
254
  "Sinker"= "#FF851B",
255
  "Cutter"= "#FFDC00",
 
266
  "Fastball"= "#7FDBFF",
267
  "Slow Curve"= "#DDDDDD",
268
  "Screwball"= "#FF69B4"
269
+ )
270
+
271
+ # Create left-handed batter plot
272
  plot_lhb <- ggplot(game_lhb, aes(x = px, y = pz, color = pitch_name)) +
273
+ # Add home plate first (so it appears under the points)
274
+ geom_polygon(data = home_plate, aes(x = x, y = y),
275
+ fill = "white", color = "black", inherit.aes = FALSE) +
276
+ # Add batter image (now using left_batter on right side)
277
+ annotation_custom(rasterGrob(left_batter,
278
+ width = unit(.75, "npc"),
279
+ height = unit(1.5, "npc")),
280
+ xmin = .7, xmax = 3.2,
281
+ ymin = 1, ymax = 5) +
282
  geom_point(size = 3) +
283
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
284
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
285
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
286
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
287
  scale_color_manual(values = pitch_colors) +
288
+ labs(x = NULL, y = NULL, title = "LHB") +
289
  xlim(-3, 3) +
290
+ ylim(0, 5) +
291
  coord_fixed(ratio = 1) +
292
+ theme_void() +
293
  theme(
294
  legend.position = "none",
295
  plot.title = element_text(hjust = 0.5, face = "bold"),
296
  axis.text = element_blank(),
297
  axis.ticks = element_blank()
298
  )
 
299
 
300
+ # Create right-handed batter plot
301
  plot_rhb <- ggplot(game_rhb, aes(x = px, y = pz, color = pitch_name)) +
302
+ # Add home plate first
303
+ geom_polygon(data = home_plate, aes(x = x, y = y),
304
+ fill = "white", color = "black", inherit.aes = FALSE) +
305
+ # Add batter image (now using right_batter on left side)
306
+ annotation_custom(rasterGrob(right_batter,
307
+ width = unit(.75, "npc"),
308
+ height = unit(1.5, "npc")),
309
+ xmin = -3, xmax = -0.5,
310
+ ymin = 1, ymax = 5) +
311
  geom_point(size = 3) +
312
  geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
313
  geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
314
  geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
315
  geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
316
  scale_color_manual(values = pitch_colors) +
317
+ labs(x = NULL, y = NULL, title = "RHB") +
318
  xlim(-3, 3) +
319
+ ylim(0, 5) +
320
  coord_fixed(ratio = 1) +
321
+ theme_void() +
322
  theme(
323
  legend.position = "none",
324
  plot.title = element_text(hjust = 0.5, face = "bold"),
 
329
  return(list(lhb = plot_lhb, rhb = plot_rhb))
330
  }
331
 
332
+
333
  calculate_timstuff <- function(game) {
334
  game <- game %>%
335
  mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),