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

# Create a detailed table for each pitch
detailed_pitch_table <- Interdonato_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 Interdonato")
Detailed Pitch Table for Interdonato
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 91.08 2478.83 234.84 20.23 15.37 StrikeCalled 5.51 2.07 6.48 7.8
Sinker 92.19 2395.44 233.11 19.46 15.67 StrikeCalled 5.53 2.11 6.38 7.8
Sinker 93.07 2442.22 232.98 19.01 15.37 BallCalled 5.61 1.95 6.31 7.8
Slider 84.69 2420.34 153.88 -0.55 2.20 BallCalled 5.28 2.22 5.74 5.1
Sinker 92.63 2479.53 226.25 17.15 17.50 StrikeSwinging 5.62 2.10 6.44 7.5
Sinker 93.09 2431.45 233.50 18.60 14.81 StrikeSwinging 5.40 2.31 6.43 7.8
Sinker 91.78 2498.32 238.11 21.40 14.48 BallCalled 5.41 2.55 6.37 7.9
Sinker 93.50 2571.77 238.12 21.52 14.48 FoulBallNotFieldable 5.29 2.57 6.26 7.9
Slider 86.08 2529.97 211.90 0.42 1.76 BallCalled 5.27 2.46 5.81 7.1
Sinker 93.40 2548.20 249.78 20.26 8.53 BallinDirt 5.42 2.28 6.26 8.3
Sinker 93.44 2530.23 238.49 20.20 13.48 BallCalled 5.43 2.17 6.37 7.9
Slider 86.05 2448.85 199.92 1.38 4.89 BallCalled 5.22 2.31 5.76 6.7
Four-Seam 93.10 2571.72 232.26 18.58 15.48 BallCalled 5.60 2.18 6.65 7.7
Four-Seam 93.74 2532.82 229.22 17.02 15.76 StrikeCalled 5.41 2.24 6.64 7.6
Slider 85.31 2478.44 64.54 -1.64 0.30 StrikeCalled 5.29 2.43 5.76 2.2
Slider 86.31 2468.10 210.85 0.52 1.99 FoulBallNotFieldable 5.31 2.42 5.80 7.0
Sinker 92.83 2458.23 250.90 18.03 7.37 BallinDirt 5.65 1.94 6.51 8.4
Slider 85.85 2333.40 165.59 -0.54 3.20 InPlay 5.36 2.35 5.83 5.5
Sinker 90.93 2485.43 227.72 17.24 16.88 BallCalled 5.55 2.12 6.38 7.6
Slider 85.27 2431.90 182.52 0.02 1.98 InPlay 5.13 2.55 5.67 6.1
Sinker 91.12 2548.25 229.62 18.52 16.94 StrikeSwinging 5.38 2.56 6.60 7.7
Four-Seam 92.60 2516.85 233.70 20.00 15.91 BallCalled 5.32 2.46 6.23 7.8
Sinker 92.94 2535.97 238.35 21.25 14.25 BallCalled 5.37 2.36 6.33 7.9
Four-Seam 92.08 2548.52 223.26 16.62 18.87 StrikeSwinging 5.48 2.22 6.45 7.4
Four-Seam 93.66 2574.09 232.48 19.85 16.36 StrikeSwinging 5.51 2.38 6.39 7.7
Slider 84.89 2520.93 230.33 1.07 2.11 BallCalled 5.24 2.63 5.75 7.7
Slider 85.01 2572.27 34.31 -1.54 -1.05 BallCalled 5.22 2.41 5.89 1.1
Four-Seam 93.06 2473.05 224.85 16.80 18.02 BallCalled 5.45 2.03 6.54 7.5
Four-Seam 92.78 2530.72 232.63 19.06 15.65 BallCalled 5.54 2.17 6.51 7.8
Four-Seam 92.82 2569.16 223.45 16.79 18.88 BallCalled 5.64 1.62 6.40 7.4
Four-Seam 93.44 2550.45 226.02 16.35 16.83 StrikeSwinging 5.70 1.91 6.50 7.5
Slider 84.80 2472.27 283.30 1.01 1.18 StrikeCalled 5.39 2.27 5.71 9.4
Four-Seam 94.38 2484.97 226.54 17.68 17.82 StrikeSwinging 5.72 1.62 6.62 7.6
Sinker 91.11 2501.67 224.52 17.49 18.86 FoulBallNotFieldable 5.58 2.15 6.71 7.5
Slider 83.87 2463.69 112.25 -1.23 2.12 StrikeSwinging 5.31 2.26 5.68 3.7
Slider 84.68 2387.57 180.14 0.00 2.53 BallCalled 5.31 2.31 5.68 6.0
Four-Seam 92.65 2522.78 227.13 16.05 15.93 InPlay 5.64 1.98 6.54 7.6
Sinker 90.59 2490.08 238.77 20.67 13.67 BallCalled 5.53 2.27 6.37 8.0
Slider 84.71 2579.80 131.23 -0.30 1.39 StrikeCalled 5.05 2.61 5.39 4.4
Four-Seam 93.52 2558.50 235.99 19.87 14.48 BallCalled 5.59 2.20 6.34 7.9
Slider 84.93 2407.69 212.10 2.08 4.50 BallinDirt 5.13 2.47 5.77 7.1
Sinker 92.75 2484.66 233.16 18.96 15.28 FoulBallNotFieldable 5.52 2.16 6.43 7.8
Sinker 93.10 2460.79 233.23 18.12 14.65 BallCalled 5.47 2.16 6.54 7.8
Sinker 91.86 2443.05 230.35 18.44 16.42 StrikeCalled 5.57 1.80 6.61 7.7
Four-Seam 92.91 2516.84 227.30 18.21 17.93 BallCalled 5.64 1.70 6.28 7.6
Slider 84.82 2367.19 156.26 -1.25 4.01 BallCalled 5.27 2.13 5.79 5.2
Sinker 92.78 2493.35 242.35 19.05 11.07 InPlay 5.56 1.77 6.41 8.1
Sinker 92.47 2444.52 231.98 19.31 16.27 BallinDirt 5.33 2.20 6.35 7.7
Sinker 92.07 2514.16 235.58 19.20 14.29 BallCalled 5.50 2.36 6.65 7.9
Sinker 92.81 2457.17 241.86 19.73 11.66 StrikeSwinging 5.42 2.22 6.47 8.1
Four-Seam 93.50 2519.90 234.94 18.62 14.17 StrikeSwinging 5.41 2.32 6.28 7.8
Four-Seam 93.76 2563.12 227.90 17.64 17.01 StrikeSwinging 5.53 2.06 6.47 7.6
Four-Seam 93.91 2533.32 226.27 16.75 17.09 BallCalled 5.55 2.05 6.62 7.5
Slider 85.11 2385.57 146.41 -1.11 2.80 StrikeCalled 5.21 2.46 5.83 4.9
Slider 86.64 2307.83 224.41 1.49 2.72 InPlay 5.15 2.30 6.08 7.5
# 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\n")
## Total number of pitches thrown:  55
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Interdonato")
Summary Pitch Table for Interdonato
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Four-Seam 29.09% 16 8 8 50.00% 50.00% 93.24 2535.43 16.64 17.87 229.00 7.6 5.55 2.07 6.47
Sinker 40.00% 22 8 14 36.36% 63.64% 92.34 2486.06 14.42 19.27 235.62 7.9 5.48 2.19 6.44
Slider 30.91% 17 7 10 41.18% 58.82% 85.24 2445.64 2.27 -0.01 170.58 5.7 5.24 2.39 5.76
# 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("Interdonato maximum FB velocity: ", max_fb_velocity, "mph\n")
## Interdonato maximum FB velocity:  94.38 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Interdonato_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) + # Increase point size
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + # Red box
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + # Black box
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) + # Gray box
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) + # Adjust ratio to shrink vertical distance
  labs(title = "Pitch Locations for Interdonato",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each pitch type
  geom_text(aes(x = -1.75, y = 5, label = "RHH"), color = "black", size = 3, hjust = 0) + # Label for RHH
  geom_text(aes(x = 1.75, y = 5, label = "LHH"), color = "black", size = 3, hjust = 1) + # Label for LHH
  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
  labs(title = "Pitch Movement for Interdonato",
       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")
  )