# Filter the data for the pitcher Isler
Isler_data <- Mystic612 %>%
  filter(Pitcher == "Isler, Nate")

# Create a detailed table for each pitch
detailed_pitch_table <- Isler_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 Isler")
Detailed Pitch Table for Isler
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 88.85 2011.21 187.42 2.68 21.85 BallCalled 6.41 0.78 6.51 6.2
Four-Seam 87.99 2025.61 197.47 7.12 23.96 FoulBallNotFieldable 6.50 0.89 6.32 6.6
Four-Seam 88.63 2171.96 191.73 4.63 23.66 BallCalled 6.51 0.93 6.52 6.4
Slider 77.00 2233.44 129.51 -5.99 6.94 BallCalled 5.94 1.57 5.78 4.3
Four-Seam 87.53 2180.49 188.28 3.77 27.50 StrikeCalled 6.42 0.79 6.27 6.3
Four-Seam 88.48 2067.81 191.17 4.85 25.95 InPlay 6.45 0.77 6.35 6.4
Four-Seam 88.97 2158.68 187.27 2.98 24.69 StrikeSwinging 6.55 0.45 6.45 6.2
Four-Seam 88.75 2205.63 186.54 3.14 28.76 BallCalled 6.51 0.75 6.40 6.2
Slider 76.90 2284.14 98.83 -14.99 4.18 BallCalled 5.97 1.50 5.88 3.3
Slider 77.96 2208.35 143.97 -5.86 9.92 InPlay 5.90 1.40 5.97 4.8
Four-Seam 87.53 2015.00 200.45 7.07 20.19 BallinDirt 6.32 1.03 6.52 6.7
Four-Seam 88.67 2351.29 189.44 4.18 26.45 FoulBallNotFieldable 6.38 0.92 6.52 6.3
Four-Seam 88.35 2135.03 188.58 3.46 24.33 InPlay 6.45 0.77 6.27 6.3
Four-Seam 89.44 2153.52 199.47 7.06 21.34 InPlay 6.46 1.00 6.20 6.6
Changeup 79.42 2136.94 198.67 4.33 14.41 StrikeSwinging 5.92 1.65 6.13 6.6
Curveball 75.81 2287.32 22.38 -7.53 -16.47 BallCalled 6.31 1.12 5.78 0.7
Changeup 77.99 1067.78 205.34 5.95 14.13 InPlay 5.92 1.62 6.14 6.8
# 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:  17
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Isler")
Summary Pitch Table for Isler
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 11.76% 2 0 2 0.00% 100.00% 78.70 1602.36 14.27 5.14 202.00 6.7 5.92 1.64 6.14
Curveball 5.88% 1 1 0 100.00% 0.00% 75.81 2287.32 -16.47 -7.53 22.38 0.7 6.31 1.12 5.78
Four-Seam 64.71% 11 3 8 27.27% 72.73% 88.47 2134.20 24.43 4.63 191.62 6.4 6.45 0.83 6.39
Slider 17.65% 3 2 1 66.67% 33.33% 77.29 2241.98 7.01 -8.95 124.10 4.1 5.94 1.49 5.88
# 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("Isler maximum FB velocity: ", max_fb_velocity, "mph\n")
## Isler maximum FB velocity:  89.44 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Isler_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 Isler",
       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 Isler",
       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")
  )