# Filter the data for the pitcher Derek Benzinger
Brian_Foley_data <- data %>%
  filter(Pitcher == "Benzinger, Derek")

# 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 Derek Benzinger")
Detailed Pitch Table for Derek Benzinger
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 90.36 2163.06 219.84 14.13 18.22 FoulBallNotFieldable 5.72 1.39 5.71 7.3
Changeup 87.67 1933.14 253.46 19.08 6.98 StrikeSwinging 5.45 1.59 5.87 8.4
Sinker 89.58 2113.32 230.68 16.35 14.69 BallCalled 5.64 1.42 5.66 7.7
Sinker 89.02 2003.69 234.70 14.27 11.27 BallCalled 5.49 1.55 5.88 7.8
Slider 79.68 2449.58 94.98 -8.25 2.06 FoulBallNotFieldable 5.47 1.63 5.74 3.2
Sinker 88.08 1921.88 249.52 15.66 7.06 InPlay 5.48 1.46 5.86 8.3
Changeup 87.11 1920.23 220.89 12.69 16.01 BallCalled 5.62 1.45 5.84 7.4
Slider 85.10 2252.01 211.19 4.84 9.27 StrikeCalled 5.34 1.57 6.01 7.0
Changeup 86.65 1938.04 231.62 14.07 12.55 StrikeCalled 5.52 1.61 5.88 7.7
Four-Seam 87.58 2028.38 220.17 11.48 14.93 BallCalled 5.61 1.59 5.86 7.3
Slider 78.06 2519.28 106.12 -4.84 2.79 FoulBallNotFieldable 5.48 1.48 5.70 3.5
Four-Seam 89.47 2113.75 220.90 13.42 16.71 FoulBallNotFieldable 5.69 1.39 5.91 7.4
Four-Seam 90.17 2217.43 214.89 13.18 20.13 InPlay 5.71 1.37 5.68 7.2
Cutter 84.03 2176.70 179.59 -0.10 15.65 BallCalled 5.49 1.61 6.16 6.0
Sinker 88.46 1894.99 241.18 14.20 9.01 BallCalled 5.52 1.44 5.92 8.0
Slider 79.90 2415.50 81.28 -8.42 0.23 StrikeCalled 5.45 1.57 5.54 2.7
Sinker 88.32 2072.23 236.46 16.03 11.88 FoulBallNotFieldable 5.66 1.42 5.84 7.9
Changeup 82.57 2015.97 252.27 18.56 7.62 InPlay 5.40 1.58 6.34 8.4
Slider 77.92 2416.36 130.62 -6.32 6.93 StrikeCalled 5.56 1.54 5.68 4.4
Four-Seam 88.35 2272.70 212.56 12.65 21.19 StrikeCalled 5.81 1.10 5.89 7.1
Slider 78.87 2614.89 94.77 -10.84 2.48 FoulBallNotFieldable 5.52 1.23 5.57 3.2
Four-Seam 89.71 2194.89 205.95 9.85 21.45 BallCalled 5.80 1.10 5.81 6.9
Slider 79.18 2507.56 95.08 -5.28 1.90 InPlay 5.49 1.37 5.83 3.2
Slider 81.99 2256.83 188.73 1.75 12.85 BallCalled 5.55 1.48 5.75 6.3
Slider 77.86 2417.72 92.51 -9.24 1.80 StrikeCalled 5.50 1.36 5.68 3.1
Four-Seam 87.60 2067.20 202.19 6.65 17.65 BallCalled 5.54 1.46 6.00 6.7
Cutter 83.93 2251.11 194.23 3.80 16.35 StrikeSwinging 5.62 1.30 5.75 6.5
Slider 78.73 2496.85 105.46 -5.11 2.89 InPlay 5.47 1.30 5.82 3.5
Four-Seam 89.98 2219.42 210.76 11.61 20.73 BallCalled 5.74 0.97 5.90 7.0
Curveball 77.94 2483.98 82.07 -6.27 0.56 InPlay 5.53 1.35 5.77 2.7
# 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 == "BallCalled"),
    Strikes = sum(PitchCall != "BallCalled"), # 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:  30
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Derek Benzinger")
Summary Pitch Table for Derek Benzinger
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 13.33% 4 1 3 25.00% 75.00% 86.00 1951.85 10.79 16.10 239.56 8.0 5.50 1.56 5.98
Curveball 3.33% 1 0 1 0.00% 100.00% 77.94 2483.98 0.56 -6.27 82.07 2.7 5.53 1.35 5.77
Cutter 6.67% 2 1 1 50.00% 50.00% 83.98 2213.90 16.00 1.85 186.91 6.2 5.56 1.46 5.96
Four-Seam 26.67% 8 4 4 50.00% 50.00% 89.15 2159.60 18.88 11.62 213.41 7.1 5.70 1.30 5.84
Sinker 16.67% 5 3 2 60.00% 40.00% 88.69 2001.22 10.78 15.30 238.51 7.9 5.56 1.46 5.83
Slider 33.33% 10 1 9 10.00% 90.00% 79.73 2434.66 4.32 -5.17 120.07 4.0 5.48 1.45 5.73
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Two-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Derek Benzinger maximum FB velocity: ", max_fb_velocity, "mph\n")
## Derek Benzinger maximum FB velocity:  90.36 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", "FoulBallNonSwinging", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.75 | PitchSide > 0.75 | 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 Derek Benzinger",
       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")
  )