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

# Create a detailed table for each pitch
detailed_pitch_table <- Reed_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 Reed Interdonato")
Detailed Pitch Table for Reed Interdonato
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 90.88 2401.00 239.53 20.32 13.11 StrikeSwinging 5.39 2.42 5.56 8.0
Sinker 92.24 2438.68 239.21 20.65 13.45 StrikeCalled 5.37 2.27 6.01 8.0
Sinker 93.22 2418.87 239.65 19.90 12.72 BallCalled 5.51 2.27 5.88 8.0
Sinker 92.50 2416.28 249.76 21.20 8.93 InPlay 5.35 2.30 6.04 8.3
Slider 84.88 2343.71 206.07 3.39 8.19 StrikeCalled 5.06 2.34 5.76 6.9
Slider 85.28 2295.14 207.27 2.93 7.08 BallCalled 5.22 2.53 5.63 6.9
Sinker 93.02 2451.96 236.60 20.59 14.74 BallCalled 5.49 1.87 6.11 7.9
Slider 85.72 2296.80 198.69 1.56 5.95 StrikeSwinging 4.95 2.34 5.70 6.6
Sinker 94.51 2479.58 232.13 19.15 16.04 InPlay 5.61 1.90 5.91 7.7
Changeup 84.36 1792.20 253.20 14.53 5.65 BallinDirt 4.89 2.50 6.55 8.4
Sinker 92.45 2477.22 238.19 21.07 14.08 StrikeSwinging 5.32 2.25 6.20 7.9
Sinker 94.08 2460.85 237.45 21.07 14.52 BallCalled 5.45 2.30 6.01 7.9
Sinker 92.23 2449.98 238.62 20.75 13.69 FoulBallNotFieldable 5.36 2.26 6.17 8.0
Slider 85.24 2383.08 215.59 2.00 3.85 BallCalled 5.11 2.56 5.71 7.2
Four-Seam 93.87 2520.32 233.43 20.34 16.06 StrikeSwinging 5.47 2.09 6.28 7.8
NA NA NA NA NA NA StrikeSwinging NA NA NA NA
Slider 86.12 2408.81 239.58 1.76 2.23 BallCalled 5.16 2.41 5.72 8.0
Sinker 92.65 2519.90 244.19 20.83 11.21 BallCalled 5.45 2.21 6.15 8.1
Sinker 91.15 2456.30 243.27 21.09 11.78 BallCalled 5.49 2.15 6.14 8.1
Sinker 92.64 2409.45 244.92 20.40 10.62 FoulBallNotFieldable 5.33 2.29 6.53 8.2
Four-Seam 93.39 2484.82 229.34 17.87 16.54 StrikeSwinging 5.46 2.09 5.91 7.6
Changeup 85.90 2047.98 251.47 18.32 7.40 StrikeCalled 5.02 2.55 6.28 8.4
Sinker 93.59 2513.40 240.61 21.16 13.10 BallCalled 5.41 2.18 5.95 8.0
Changeup 85.74 1840.52 241.70 16.26 10.01 BallinDirt 4.98 2.33 6.30 8.1
Sinker 91.87 2482.22 235.87 20.36 14.95 StrikeCalled 5.35 2.40 6.24 7.9
Sinker 93.58 2370.81 226.49 19.13 19.31 BallCalled 5.61 2.00 6.17 7.5
Sinker 92.82 2501.38 239.14 21.78 14.15 InPlay 5.37 2.24 6.18 8.0
Slider 86.65 2348.92 259.12 5.25 2.13 BallCalled 5.23 2.20 6.06 8.6
Slider 85.76 2302.76 239.22 4.76 3.91 BallinDirt 5.12 2.40 5.95 8.0
Sinker 92.31 2430.36 237.88 21.57 14.63 StrikeCalled 5.55 2.03 6.19 7.9
Slider 85.68 2358.16 143.11 -0.87 2.19 BallCalled 5.27 2.33 5.59 4.8
Sinker 91.83 2495.40 231.71 20.52 17.31 StrikeCalled 5.55 2.10 6.52 7.7
Four-Seam 92.98 2415.53 233.99 21.17 16.45 FoulBallNotFieldable 5.55 1.88 6.34 7.8
Slider 86.74 2370.22 206.25 1.56 4.50 InPlay 5.22 2.26 5.61 6.9
Slider 84.92 2364.07 241.04 5.28 4.40 BallCalled 5.10 2.33 5.70 8.0
Slider 86.07 2299.30 210.86 4.86 9.35 FoulBallNotFieldable 5.12 2.22 5.78 7.0
Sinker 92.32 2504.19 234.50 19.73 15.17 FoulBallNotFieldable 5.71 1.80 6.29 7.8
Slider 86.20 2450.01 228.36 7.04 7.41 BallinDirt 5.20 2.18 5.86 7.6
Four-Seam 93.09 2454.53 221.29 15.97 19.14 BallCalled 5.50 1.83 6.11 7.4
Slider 85.45 2271.00 244.91 4.17 3.16 StrikeSwinging 5.21 2.16 5.65 8.2
Changeup 85.83 1985.61 230.67 16.93 15.05 FoulBallNotFieldable 5.03 2.37 6.19 7.7
Four-Seam 93.26 2555.00 232.36 20.44 16.81 BallCalled 5.40 2.12 6.13 7.7
Slider 84.53 2297.91 220.30 2.97 4.64 StrikeCalled 5.11 2.41 5.56 7.3
Four-Seam 93.51 2529.76 226.57 17.82 17.90 StrikeSwinging 5.50 1.92 5.91 7.6
Slider 86.64 2320.04 210.63 2.18 4.83 StrikeSwinging 5.11 2.40 5.96 7.0
Four-Seam 92.71 2499.26 226.24 15.36 15.62 FoulBallNotFieldable 5.52 1.78 6.23 7.5
Slider 83.64 2166.15 243.47 2.60 2.27 BallinDirt 5.01 2.17 5.84 8.1
Slider 85.56 2231.30 277.12 3.60 0.60 StrikeSwinging 5.39 2.16 5.72 9.2
Changeup 83.20 1795.19 234.06 17.76 14.16 BallCalled 5.08 2.56 6.24 7.8
Sinker 91.14 2467.47 230.19 17.58 15.85 StrikeSwinging 5.58 2.13 5.78 7.7
Changeup 85.20 1983.82 243.77 18.04 10.16 BallCalled 5.13 2.35 5.99 8.1
Sinker 91.21 2554.69 233.84 18.85 15.02 BallCalled 5.52 2.13 6.27 7.8
Four-Seam 92.08 2582.81 232.86 19.09 15.62 BallCalled 5.55 2.21 6.25 7.8
Slider 83.44 2306.95 219.02 4.67 7.10 FoulBallNotFieldable 5.33 2.39 5.85 7.3
Slider 83.80 2336.98 176.96 -0.13 3.78 BallCalled 5.19 2.13 5.65 5.9
Slider 83.36 2231.20 234.69 4.04 4.27 BallinDirt 5.22 2.07 5.84 7.8
Sinker 91.20 2495.93 224.49 16.35 17.69 InPlay 5.44 2.04 6.00 7.5
Changeup 84.76 1985.77 246.50 21.09 10.39 StrikeCalled 5.12 2.44 6.00 8.2
Sinker 91.05 2428.71 237.07 23.03 15.99 BallCalled 5.31 2.29 5.98 7.9
Changeup 85.41 1975.02 228.94 14.91 14.11 StrikeCalled 5.05 2.42 6.10 7.6
Changeup 84.55 1817.13 230.59 12.33 11.22 BallCalled 5.02 2.16 6.35 7.7
Sinker 92.16 2479.06 235.06 20.31 15.18 FoulBallNotFieldable 5.41 2.31 6.27 7.8
Slider 85.71 2338.82 185.43 0.45 5.88 InPlay 5.12 2.30 5.73 6.2
Sinker 92.09 2334.55 229.39 18.78 17.20 StrikeCalled 5.52 2.24 6.13 7.6
Changeup 85.68 2015.87 241.35 16.37 10.18 BallCalled 5.08 2.34 6.33 8.0
Changeup 85.35 2068.46 244.15 17.46 9.70 StrikeSwinging 5.01 2.29 6.43 8.1
Four-Seam 92.36 2547.15 231.76 19.27 16.32 FoulBallNotFieldable 5.73 2.21 6.13 7.7
Changeup 85.03 1890.19 244.14 14.68 8.31 BallCalled 5.09 2.31 6.28 8.1
Four-Seam 93.02 2415.01 216.59 13.15 18.73 BallCalled 5.48 2.17 6.10 7.2
Four-Seam 92.17 2394.59 223.18 14.88 16.89 BallCalled 5.51 2.25 6.22 7.4
# 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:  70
# 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 17.14% 12 5 7 41.67% 58.33% 85.08 1933.15 10.53 16.56 240.88 8.0 5.04 2.38 6.25
Four-Seam 15.71% 11 5 6 45.45% 54.55% 92.95 2490.80 16.92 17.76 227.96 7.6 5.52 2.05 6.15
Sinker 35.71% 25 9 16 36.00% 64.00% 92.35 2457.53 14.42 20.25 236.79 7.9 5.46 2.18 6.11
Slider 30.00% 21 7 14 33.33% 66.67% 85.30 2320.06 4.65 3.05 219.41 7.3 5.16 2.30 5.76
NA 1.43% 1 0 1 0.00% 100.00% NaN NaN NaN NaN NaN NaN NaN NaN NaN
# 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:  94.51 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Reed_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) +
  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 for", unique(detailed_pitch_table$AutoPitchType)),
       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")
  )