# Filter the data for the pitcher Keevan
Keevan_data <- Mystic612 %>%
  filter(Pitcher == "Keevan, Josh")

# Create a detailed table for each pitch
detailed_pitch_table <- Keevan_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 Keevan")
Detailed Pitch Table for Keevan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 81.73 2230.08 130.43 -20.07 18.70 StrikeCalled 5.41 -1.07 6.83 4.3
Changeup 77.55 1569.85 106.87 -18.88 7.16 BallinDirt 5.05 -1.27 7.07 3.6
Splitter 83.69 2207.38 137.96 -14.38 17.40 FoulBallFieldable 5.48 -0.94 6.78 4.6
Curveball 74.06 2215.66 267.35 7.91 1.99 BallCalled 5.14 -1.19 6.10 8.9
Splitter 83.97 2276.08 138.31 -17.66 21.30 InPlay 5.60 -0.81 6.68 4.6
Splitter 83.85 2264.10 130.75 -20.62 19.16 StrikeCalled 5.55 -0.84 6.79 4.4
Changeup 78.08 1821.83 122.22 -19.54 13.78 BallinDirt 4.99 -1.42 6.97 4.1
Splitter 84.47 2338.03 136.51 -17.86 20.17 BallCalled 5.46 -0.80 6.80 4.6
Changeup 83.46 2200.96 129.52 -17.65 16.25 HitByPitch 5.50 -0.90 6.92 4.3
Splitter 83.50 2270.84 134.30 -19.48 20.47 StrikeCalled 5.40 -0.99 6.55 4.5
Curveball 73.39 2295.87 279.21 6.05 0.80 BallinDirt 4.94 -1.30 6.14 9.3
Curveball 71.23 2218.72 290.64 9.78 -1.81 BallinDirt 4.92 -1.35 5.67 9.7
Splitter 83.57 2250.09 133.31 -20.23 20.44 BallCalled 5.31 -1.13 6.80 4.4
Changeup 81.06 2186.01 128.57 -20.88 18.06 InPlay 5.31 -0.95 6.98 4.3
Splitter 84.53 2286.26 138.39 -17.81 21.42 StrikeCalled 5.48 -0.93 6.86 4.6
Changeup 76.20 1522.00 124.88 -18.80 14.64 StrikeSwinging 5.11 -1.12 6.85 4.2
Changeup 76.33 1821.33 110.12 -21.27 9.34 FoulBallNotFieldable 5.02 -1.32 6.73 3.7
Changeup 80.51 2204.69 131.02 -17.63 16.74 InPlay 5.40 -0.74 7.06 4.4
Splitter 82.61 2153.08 133.00 -17.51 17.67 BallCalled 5.48 -0.71 6.89 4.4
Splitter 83.56 2206.81 132.27 -18.06 17.68 FoulBallNotFieldable 5.41 -0.83 6.87 4.4
Curveball 73.44 2098.87 181.68 0.05 3.41 StrikeCalled 5.04 -1.24 6.22 6.1
Curveball 72.19 2081.83 278.81 3.62 1.27 FoulBallNotFieldable 5.12 -0.99 6.00 9.3
Changeup 83.35 2282.38 129.99 -20.94 19.04 FoulBallNotFieldable 5.41 -0.78 6.92 4.3
Changeup 75.53 1629.24 101.29 -17.73 5.06 InPlay 5.02 -1.26 7.11 3.4
Splitter 82.31 2183.07 141.80 -14.60 19.86 StrikeCalled 5.38 -0.75 7.01 4.7
Changeup 76.88 1659.93 106.13 -19.34 7.04 BallinDirt 4.87 -1.14 7.08 3.5
Changeup 82.21 2096.48 132.42 -18.75 18.47 InPlay 5.40 -0.98 6.92 4.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\n")
## Total number of pitches thrown:  27
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Keevan")
Summary Pitch Table for Keevan
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 44.44% 12 0 12 0.00% 100.00% 79.41 1935.40 13.69 -19.29 121.12 4.0 5.21 -1.08 6.95
Curveball 18.52% 5 1 4 20.00% 80.00% 72.86 2182.19 1.13 5.48 259.54 8.7 5.03 -1.21 6.03
Splitter 37.04% 10 3 7 30.00% 70.00% 83.61 2243.57 19.56 -17.82 135.66 4.5 5.46 -0.87 6.80
# 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("Keevan maximum FB velocity: ", max_fb_velocity, "mph\n")
## Keevan maximum FB velocity:  -Inf mph
# Prepare data for plotting pitch locations
pitch_location_data <- Keevan_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 Keevan",
       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 Keevan",
       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")
  )