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

# Create a detailed table for each pitch
detailed_pitch_table <- Keevan_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 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 ClockTilt
Changeup 83.33 2073.29 95.90 -23.71 3.79 StrikeSwinging 3.2
Four-Seam 84.83 2210.88 165.53 -4.61 19.27 BallCalled 5.5
Changeup 77.25 1336.60 112.14 -25.54 12.04 BallCalled 3.7
Changeup 79.01 2002.62 134.17 -14.91 15.99 FoulBallNotFieldable 4.5
Four-Seam 86.17 2203.37 176.34 -1.53 25.38 StrikeCalled 5.9
Changeup 85.81 2241.55 126.20 -18.44 14.73 BallCalled 4.2
Changeup 83.18 2207.15 125.65 -19.23 15.10 FoulBallNotFieldable 4.2
Changeup 76.29 1332.11 143.63 -6.70 10.61 InPlay 4.8
Splitter 83.14 2138.11 141.88 -14.55 19.91 BallCalled 4.7
Four-Seam 85.62 2205.65 206.74 22.80 46.75 BallCalled 6.9
Changeup 78.62 2180.78 160.44 -8.37 25.05 FoulBallNotFieldable 5.3
Changeup 75.11 1483.34 110.93 -12.20 6.48 InPlay 3.7
Changeup 82.22 2051.59 121.10 -15.54 10.68 BallCalled 4.0
Splitter 83.05 2158.03 135.91 -15.57 17.34 FoulBallNotFieldable 4.5
Splitter 84.20 2203.62 135.46 -16.64 18.27 FoulBallNotFieldable 4.5
Splitter 83.11 2243.27 134.22 -17.96 18.81 BallCalled 4.5
Changeup 75.38 1644.66 112.38 -14.11 7.31 InPlay 3.7
Changeup 85.67 2367.59 131.17 -18.41 17.42 FoulBallNotFieldable 4.4
Sinker 85.66 2221.99 135.43 -18.00 19.65 StrikeSwinging 4.5
Changeup 85.56 2264.83 131.82 -18.83 18.18 StrikeSwinging 4.4
Changeup 84.38 2341.03 129.82 -18.20 16.56 BallCalled 4.3
Curveball 71.87 2035.70 279.15 5.64 1.12 StrikeCalled 9.3
Curveball 72.63 2215.37 268.64 5.20 2.05 BallCalled 9.0
Splitter 84.06 2346.98 133.71 -18.43 18.92 StrikeSwinging 4.5
Changeup 84.46 2314.35 133.04 -17.15 17.24 BallCalled 4.4
Splitter 84.44 2202.59 135.65 -16.57 18.19 BallCalled 4.5
# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    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
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Keevan")
Summary Pitch Table for Keevan
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 14 5 9 35.71% 64.29% 81.16 1988.68 13.66 -16.52 126.31 4.2
Curveball 2 1 1 50.00% 50.00% 72.25 2125.53 1.58 5.42 273.90 9.2
Four-Seam 3 2 1 66.67% 33.33% 85.54 2206.63 30.47 5.55 182.87 6.1
Sinker 1 0 1 0.00% 100.00% 85.66 2221.99 19.65 -18.00 135.43 4.5
Splitter 6 3 3 50.00% 50.00% 83.67 2215.43 18.57 -16.62 136.14 4.5
# 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")
  )