# 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.33 2200.66 218.69 12.59 16.96 StrikeCalled 6.15 1.95 4.75 7.3
Changeup 82.76 2081.81 259.77 17.79 4.59 StrikeCalled 5.63 2.28 5.34 8.7
Four-Seam 90.77 2361.21 214.90 12.49 19.10 StrikeSwinging 6.08 1.82 4.86 7.2
Sinker 88.30 2136.61 222.93 13.29 15.53 BallCalled 6.14 2.02 4.91 7.4
Sinker 88.76 2175.79 223.02 14.21 16.47 BallCalled 6.14 1.89 5.03 7.4
Slider 84.30 2304.11 155.58 -2.73 7.22 BallCalled 5.83 2.04 5.14 5.2
Four-Seam 88.44 2191.10 219.83 12.66 16.39 StrikeCalled 6.12 1.87 5.15 7.3
Slider 83.15 2362.09 150.26 -3.57 7.46 BallCalled 5.80 2.08 4.95 5.0
Slider 83.34 2343.90 155.98 -2.54 6.95 StrikeSwinging 5.77 2.07 5.03 5.2
Slider 83.37 2337.50 162.64 -2.37 8.83 InPlay 5.75 1.99 5.05 5.4
Four-Seam 89.80 2190.73 213.27 12.43 20.17 StrikeSwinging 6.17 1.71 4.83 7.1
Four-Seam 90.50 2362.33 214.26 11.96 18.76 StrikeSwinging 6.09 1.67 5.04 7.1
Four-Seam 90.46 2277.04 215.60 12.70 18.94 FoulBallNotFieldable 6.10 1.63 4.73 7.2
Slider 80.76 2582.28 92.03 -8.55 1.52 StrikeSwinging 5.74 1.87 5.02 3.1
Four-Seam 87.69 2237.50 220.35 12.47 15.86 StrikeCalled 6.17 1.91 5.03 7.3
Curveball 75.93 2415.90 35.45 -8.31 -10.28 BallCalled 5.77 2.11 5.16 1.2
Four-Seam 89.53 2272.41 221.30 13.27 16.31 BallCalled 6.06 1.85 5.06 7.4
Slider 81.57 2411.35 129.40 -5.90 6.13 StrikeCalled 5.85 2.15 5.05 4.3
Changeup 81.18 1929.43 244.60 15.70 8.89 InPlay 5.55 2.19 5.67 8.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 == "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:  19
# 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 10.53% 2 0 2 0.00% 100.00% 81.97 2005.62 6.74 16.74 252.18 8.4 5.59 2.23 5.50
Curveball 5.26% 1 1 0 100.00% 0.00% 75.93 2415.90 -10.28 -8.31 35.45 1.2 5.77 2.11 5.16
Four-Seam 42.11% 8 1 7 12.50% 87.50% 89.69 2261.62 17.81 12.57 217.28 7.2 6.12 1.80 4.93
Sinker 10.53% 2 2 0 100.00% 0.00% 88.53 2156.20 16.00 13.75 222.98 7.4 6.14 1.96 4.97
Slider 31.58% 6 2 4 33.33% 66.67% 82.75 2390.20 6.35 -4.28 140.98 4.7 5.79 2.03 5.04
# 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.77 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.75, xmax = 0.75, 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")
  )