# 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
Sinker 87.80 2162.71 223.94 14.90 16.74 BallCalled 6.19 1.85 5.13 7.5
Slider 83.21 2375.87 193.99 1.04 5.59 StrikeSwinging 5.78 2.11 4.90 6.5
Sinker 88.03 2196.68 227.06 14.94 15.18 BallCalled 5.91 1.96 5.05 7.6
Slider 83.34 2423.45 226.03 7.53 8.56 InPlay 5.87 1.99 4.83 7.5
Curveball 76.87 2495.21 64.97 -7.07 -1.71 FoulBallNotFieldable 5.84 1.93 4.93 2.2
Slider 78.52 2564.28 103.78 -4.52 2.64 StrikeSwinging 5.86 1.73 5.02 3.5
Curveball 79.29 2581.79 55.81 -5.01 -1.96 BallCalled 5.84 1.69 4.96 1.9
Sinker 87.69 2136.58 232.27 15.40 13.17 InPlay 5.98 1.72 5.26 7.7
Sinker 88.02 2131.41 232.79 16.11 13.45 StrikeCalled 5.93 1.97 5.24 7.8
Changeup 82.83 1927.43 243.56 14.22 8.42 BallCalled 5.66 2.05 5.56 8.1
Changeup 87.04 1909.38 249.62 18.60 8.19 InPlay 5.84 1.89 5.04 8.3
Curveball 76.81 1243.84 39.88 -5.82 -5.43 HitByPitch 5.92 1.93 4.84 1.3
Slider 82.69 2299.61 183.28 0.21 5.05 BallCalled 5.88 1.86 4.98 6.1
Four-Seam 86.19 2153.17 215.94 12.09 18.09 InPlay 6.25 1.68 5.11 7.2
Changeup 85.86 1927.85 256.87 16.00 4.97 BallCalled 5.97 2.11 5.08 8.6
Changeup 86.47 2006.20 218.69 12.44 16.91 BallCalled 6.15 1.85 5.01 7.3
Slider 81.69 2410.37 180.50 0.04 5.91 StrikeCalled 5.98 2.13 4.62 6.0
Slider 84.36 2390.77 191.13 0.90 5.87 BallCalled 6.05 1.82 4.71 6.4
Sinker 87.28 2154.23 228.90 16.21 15.44 BallCalled 6.09 1.88 5.08 7.6
Slider 84.04 2394.03 200.67 1.89 6.37 BallCalled 5.95 1.96 4.86 6.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 %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:  20
# 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 20.00% 4 3 1 75.00% 25.00% 85.55 1942.72 9.62 15.32 242.18 8.1 5.90 1.97 5.17
Curveball 15.00% 3 1 2 33.33% 66.67% 77.66 2106.95 -3.03 -5.97 53.55 1.8 5.87 1.85 4.91
Four-Seam 5.00% 1 0 1 0.00% 100.00% 86.19 2153.17 18.09 12.09 215.94 7.2 6.25 1.68 5.11
Sinker 25.00% 5 3 2 60.00% 40.00% 87.76 2156.32 14.80 15.51 228.99 7.6 6.02 1.88 5.15
Slider 35.00% 7 3 4 42.86% 57.14% 82.55 2408.34 5.71 1.01 182.77 6.1 5.91 1.94 4.85
# 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("Derek Benzinger maximum FB velocity: ", max_fb_velocity, "mph\n")
## Derek Benzinger maximum FB velocity:  88.03 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 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")
  )