# Filter the data for the pitcher Joe Voli
Brian_Foley_data <- data %>%
  filter(Pitcher == "Voli, Joseph")

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_Foley_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall, RelHeight, RelSide, Extension) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak,
    ReleaseHeight = RelHeight,
    ReleaseSide = RelSide
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 2),
    ReleaseHeight = round(ReleaseHeight, 2),
    ReleaseSide = round(ReleaseSide, 2),
    Extension = round(Extension, 2),
    ClockTilt = round((Tilt / 30) %% 12, 1) # Interpret Tilt as clock face
  )

# Display the detailed table
knitr::kable(detailed_pitch_table, caption = "Detailed Pitch Table for Joe Voli")
Detailed Pitch Table for Joe Voli
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 73.50 2578.47 107.86 -4.62 3.08 BallinDirt 5.21 1.77 5.83 3.6
Four-Seam 81.72 2455.19 198.72 7.89 24.76 BallCalled 5.61 1.37 6.37 6.6
Curveball 72.84 2469.05 128.67 -8.72 8.76 BallCalled 5.07 1.81 5.76 4.3
Changeup 79.84 2204.46 220.07 14.80 19.11 StrikeCalled 5.09 1.82 6.45 7.3
Changeup 81.14 2415.72 215.84 12.95 19.43 StrikeCalled 5.09 1.90 6.38 7.2
Curveball 72.99 2440.22 103.38 -7.45 3.52 BallCalled 5.09 1.74 5.69 3.4
Changeup 80.01 2318.63 207.35 11.08 22.96 BallCalled 5.62 1.57 6.26 6.9
Curveball 71.27 2403.95 74.13 -5.45 0.25 StrikeCalled 5.57 1.54 5.55 2.5
Curveball 72.22 2534.18 101.33 -6.37 3.01 BallinDirt 5.27 1.90 5.58 3.4
Curveball 69.99 2368.72 79.68 -9.20 0.37 InPlay 5.74 1.76 5.74 2.7
Curveball 69.93 2357.01 101.41 -8.51 3.78 BallCalled 5.48 1.94 5.53 3.4
Curveball 73.33 2512.45 69.69 -5.79 -0.28 BallCalled 5.29 1.74 5.53 2.3
Changeup 80.40 2372.29 193.56 4.51 20.26 BallCalled 5.60 1.39 6.30 6.5
Four-Seam 82.42 2425.98 200.44 7.85 22.52 BallCalled 5.58 1.49 6.37 6.7
Curveball 71.00 2410.56 44.79 -8.45 -6.49 BallCalled 5.33 1.74 5.65 1.5
Curveball 69.17 2302.85 57.05 -9.28 -3.87 StrikeCalled 5.44 1.67 5.71 1.9
Changeup 80.65 2484.87 202.56 8.26 21.45 BallCalled 5.32 1.84 5.92 6.8
Curveball 71.19 2391.65 70.34 -6.67 -0.52 BallCalled 5.43 1.62 5.66 2.3
Four-Seam 82.77 2482.97 204.65 9.43 22.03 StrikeSwinging 5.31 1.77 6.29 6.8
Four-Seam 83.33 2388.12 206.89 10.03 21.28 InPlay 5.42 1.81 6.12 6.9
Curveball 73.85 2584.91 98.97 -5.84 2.78 StrikeCalled 5.17 1.78 5.42 3.3
Curveball 72.49 2604.82 85.00 -6.63 1.46 BallCalled 5.20 1.77 5.46 2.8
Curveball 71.31 2455.95 61.96 -6.61 -1.53 BallCalled 5.46 1.52 5.58 2.1
Curveball 71.74 2479.65 79.48 -7.18 0.70 StrikeCalled 5.05 1.91 5.88 2.6
Curveball 73.34 2520.38 57.05 -10.58 -4.91 BallCalled 5.27 1.56 5.68 1.9
Four-Seam 83.61 2439.46 203.61 8.81 21.68 InPlay 5.58 1.43 6.22 6.8
Curveball 71.68 2403.19 59.90 -9.97 -3.85 BallCalled 5.30 1.73 5.53 2.0
Curveball 70.20 2428.66 97.62 -7.77 3.10 BallCalled 5.36 1.73 5.77 3.3
Changeup 82.10 2557.11 210.72 10.65 19.36 BallCalled 5.04 1.89 6.37 7.0
Curveball 72.53 2421.01 106.47 -5.13 3.37 StrikeCalled 5.25 1.81 5.63 3.5
Curveball 70.05 2318.46 96.63 -12.90 3.64 BallCalled 5.32 1.71 5.56 3.2
# Calculate the total number of pitches
total_pitches <- nrow(detailed_pitch_table)

# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    Usage = sprintf("%.2f%%", n() / total_pitches * 100),
    Balls = sum(PitchCall %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # Count everything not a ball as a strike
    BallPercentage = sprintf('%.2f%%', Balls / TotalPitches * 100),
    StrikePercentage = sprintf('%.2f%%', Strikes / TotalPitches * 100),
    AvgVelocity = round(mean(ReleaseSpeed, na.rm = TRUE), 2),
    AvgSpinRate = round(mean(SpinRate, na.rm = TRUE), 2),
    AvgInducedVertBreak = round(mean(InducedVerticalBreak, na.rm = TRUE), 2),
    AvgHorzBreak = round(mean(HorizontalBreak, na.rm = TRUE), 2),
    AvgTilt = round(mean(Tilt, na.rm = TRUE), 2),
    AvgClockTilt = round(mean(ClockTilt, na.rm = TRUE), 1), # Clock face interpretation
    AvgReleaseHeight = round(mean(ReleaseHeight, na.rm = TRUE), 2),
    AvgReleaseSide = round(mean(ReleaseSide, na.rm = TRUE), 2),
    AvgExtension = round(mean(Extension, na.rm = TRUE), 2)
  ) %>%
  select(AutoPitchType, Usage, everything())

# Display the total number of pitches
cat("Total number of pitches thrown: ", total_pitches, "\n")
## Total number of pitches thrown:  31
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Joe Voli")
Summary Pitch Table for Joe Voli
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 19.35% 6 4 2 66.67% 33.33% 80.69 2392.18 20.43 10.38 208.35 7.0 5.29 1.73 6.28
Curveball 64.52% 20 14 6 70.00% 30.00% 71.73 2449.31 0.82 -7.66 84.07 2.8 5.32 1.74 5.64
Four-Seam 16.13% 5 2 3 40.00% 60.00% 82.77 2438.34 22.45 8.80 202.86 6.8 5.50 1.57 6.27
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Joe Voli maximum FB velocity: ", max_fb_velocity, "mph\n")
## Joe Voli maximum FB velocity:  83.61 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_data %>%
  select(AutoPitchType, PlateLocHeight, PlateLocSide, PitchCall) %>%
  rename(
    PitchHeight = PlateLocHeight,
    PitchSide = PlateLocSide
  ) %>%
  mutate(
    SwingTake = ifelse(PitchCall %in% c("StrikeSwinging", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | PitchHeight < 1.5 | PitchHeight > 3.5), "Chase", "Non-Chase")
  )

# Create the scatter plot with specified strike zone boxes
ggplot(pitch_location_data, aes(x = PitchSide, y = PitchHeight, color = SwingTake, shape = Chase)) +
  geom_point(size = 3) +
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -0.85, xmax = 0.85, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) +
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) +
  labs(title = "Pitch Locations for Joe Voli",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) +
  theme_minimal() +
  theme(
    legend.position = "right",
    panel.grid.major = element_line(color = "grey80"),
    panel.grid.minor = element_line(color = "grey90"),
    axis.text = element_text(color = "black"),
    axis.title = element_text(color = "black"),
    plot.title = element_text(color = "black"),
    legend.background = element_rect(fill = "white", color = NA),
    legend.key = element_rect(fill = "white", color = NA),
    legend.text = element_text(color = "black"),
    legend.title = element_text(color = "black")
  )

# Create the scatter plot for horizontal and vertical breaks
ggplot(detailed_pitch_table, aes(x = HorizontalBreak, y = InducedVerticalBreak, color = AutoPitchType)) +
  geom_point(size = 3) + # Increase point size
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement"), 
       x = "Horizontal Break (inches)",
       y = "Induced Vertical Break (inches)",
       color = "Pitch Type") +
  theme_minimal() +
  theme(
    legend.position = "right",
    panel.grid.major = element_line(color = "grey80"),
    panel.grid.minor = element_line(color = "grey90"),
    axis.text = element_text(color = "black"),
    axis.title = element_text(color = "black"),
    plot.title = element_text(color = "black"),
    legend.background = element_rect(fill = "white", color = NA),
    legend.key = element_rect(fill = "white", color = NA),
    legend.text = element_text(color = "black"),
    legend.title = element_text(color = "black")
  )