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

# 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 Reed Interdonato")
Detailed Pitch Table for Reed Interdonato
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 90.94 2421.62 237.68 20.40 14.08 FoulBallNotFieldable 5.61 2.39 5.44 7.9
Slider 84.12 2244.11 206.62 1.01 3.12 StrikeCalled 5.18 2.65 5.10 6.9
Sinker 93.69 2421.97 237.48 20.44 14.19 FoulBallNotFieldable 5.67 2.30 5.45 7.9
Slider 84.80 2271.82 305.04 3.49 -1.29 StrikeSwinging 5.28 2.57 5.33 10.2
Slider 84.98 2260.75 255.41 2.13 1.64 StrikeCalled 5.19 2.74 5.19 8.5
Sinker 92.55 2442.19 241.60 22.16 13.08 InPlay 5.60 2.32 5.44 8.1
Slider 84.28 2393.24 70.91 -2.04 0.26 StrikeSwinging 5.21 2.52 4.92 2.4
Slider 86.55 2334.42 284.83 2.97 0.33 BallCalled 5.23 2.46 5.48 9.5
Slider 85.71 2357.12 263.92 0.84 1.47 StrikeCalled 5.19 2.68 5.39 8.8
Slider 87.50 2269.59 176.37 -0.18 4.20 FoulBallNotFieldable 5.38 2.49 5.31 5.9
Slider 86.77 2258.19 229.43 1.57 2.50 FoulBallNotFieldable 5.28 2.64 5.09 7.6
Sinker 91.28 2305.88 237.93 17.38 11.91 BallCalled 5.69 2.29 5.35 7.9
Slider 85.17 2230.19 185.73 0.23 3.55 FoulBallNotFieldable 5.38 2.58 4.98 6.2
Sinker 92.22 2438.37 236.02 19.51 14.34 StrikeSwinging 5.51 2.38 5.38 7.9
Sinker 91.40 2360.04 249.76 18.85 8.19 BallCalled 5.38 2.69 5.34 8.3
Sinker 90.02 2419.66 242.08 21.05 12.49 FoulBallNotFieldable 5.30 2.74 5.07 8.1
Changeup 85.21 1995.16 256.16 17.42 5.61 InPlay 5.05 2.71 5.57 8.5
Sinker 90.91 2400.02 238.22 20.83 14.16 StrikeCalled 5.61 2.40 5.11 7.9
Slider 84.41 2301.26 23.02 -1.30 -1.57 StrikeSwinging 5.28 2.60 4.90 0.8
Slider 85.64 2299.92 236.31 1.54 2.35 InPlay 5.30 2.48 5.52 7.9
Changeup 84.66 2048.19 248.96 18.43 8.41 StrikeSwinging 5.09 2.62 5.71 8.3
Sinker 92.26 2312.85 243.17 21.09 11.86 BallCalled 5.38 2.49 5.62 8.1
Sinker 90.82 2308.31 238.55 19.83 13.34 BallCalled 5.43 2.48 5.34 8.0
Sinker 90.78 2298.66 236.81 19.52 14.02 StrikeCalled 5.50 2.48 4.98 7.9
Slider 84.67 2275.71 146.41 -1.82 4.12 BallCalled 5.27 2.43 4.84 4.9
Sinker 91.82 2328.96 240.42 20.74 12.92 FoulBallNotFieldable 5.38 2.25 5.49 8.0
Changeup 85.85 1906.49 271.83 17.03 0.64 BallCalled 5.14 2.42 5.45 9.1
Slider 86.92 2311.83 242.77 5.87 3.94 StrikeCalled 5.26 2.55 5.58 8.1
Sinker 92.75 2421.52 234.12 19.28 14.98 BallCalled 5.69 2.25 5.44 7.8
Slider 85.24 2331.08 34.38 -0.07 1.08 StrikeCalled 5.38 2.41 5.05 1.1
Four-Seam 93.98 2478.81 225.05 17.73 18.75 StrikeSwinging 5.76 2.11 5.37 7.5
Sinker 91.87 2437.35 233.31 19.05 15.25 BallCalled 5.51 2.49 5.44 7.8
Changeup 84.74 1853.95 226.60 11.15 11.73 BallCalled 5.16 2.59 5.87 7.6
Sinker 91.28 2361.88 237.04 19.33 13.59 BallCalled 5.48 2.51 5.51 7.9
Sinker 90.85 2414.44 239.46 19.89 12.81 BallCalled 5.40 2.58 5.46 8.0
Slider 84.91 2240.69 219.05 2.88 4.55 BallCalled 5.33 2.37 5.42 7.3
Sinker 93.14 2484.58 236.64 20.04 14.24 StrikeSwinging 5.66 2.35 5.50 7.9
Sinker 93.01 2194.60 235.80 17.70 13.00 StrikeSwinging 5.70 2.19 5.46 7.9
Sinker 93.14 1949.62 243.23 16.67 9.35 BallCalled 5.72 2.19 5.51 8.1
Slider 86.23 2214.69 195.39 1.48 6.48 FoulBallNotFieldable 5.42 2.44 5.17 6.5
Sinker 93.33 2472.19 232.82 17.35 14.29 BallCalled 5.71 2.09 5.42 7.8
Sinker 93.59 2236.78 235.06 16.33 12.51 BallCalled 5.56 2.29 5.31 7.8
Sinker 92.96 2407.12 230.65 15.59 13.99 StrikeCalled 5.69 2.16 5.45 7.7
Slider 85.87 2394.10 185.32 0.06 2.04 StrikeSwinging 5.29 2.60 5.03 6.2
Sinker 93.91 2476.04 234.48 17.99 13.96 FoulBallNotFieldable 5.78 2.02 5.35 7.8
Slider 85.30 2335.54 176.08 -0.04 1.84 InPlay 5.34 2.37 5.09 5.9
Sinker 90.70 2348.74 239.57 21.54 13.87 BallCalled 5.48 2.28 5.46 8.0
Slider 85.47 2303.60 174.22 -0.23 3.62 StrikeCalled 5.25 2.28 5.27 5.8
Slider 85.38 2321.74 221.41 2.65 4.29 FoulBallNotFieldable 5.30 2.56 5.22 7.4
Sinker 92.55 2289.72 240.57 19.59 12.21 BallCalled 5.71 2.06 5.57 8.0
Sinker 91.74 2201.20 238.12 19.56 13.33 StrikeSwinging 5.60 2.15 5.49 7.9
# 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:  51
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Reed Interdonato")
Summary Pitch Table for Reed Interdonato
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 7.84% 4 2 2 50.00% 50.00% 85.11 1950.95 6.60 16.01 250.89 8.4 5.11 2.58 5.65
Four-Seam 1.96% 1 0 1 0.00% 100.00% 93.98 2478.81 18.75 17.73 225.05 7.5 5.76 2.11 5.37
Sinker 50.98% 26 13 13 50.00% 50.00% 92.06 2352.09 13.15 19.30 238.10 7.9 5.57 2.34 5.40
Slider 39.22% 20 3 17 15.00% 85.00% 85.50 2297.48 2.43 1.05 191.63 6.4 5.29 2.52 5.19
# 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("Reed Interdonato maximum FB velocity: ", max_fb_velocity, "mph\n")
## Reed Interdonato maximum FB velocity:  93.98 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 Reed Interdonato",
       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")
  )