# Filter the data for the pitcher Adams
Adams_data <- Sanford69 %>%
  filter(Pitcher == "Adams, Brandon")

# Create a detailed table for each pitch
detailed_pitch_table <- Adams_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall, RelHeight, RelSide) %>%
  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),
    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 Adams")
Detailed Pitch Table for Adams
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide ClockTilt
Four-Seam 88.64 2068.93 209.96 10.71 19.71 InPlay 5.78 2.60 7.0
Four-Seam 89.47 2217.47 215.02 12.48 18.86 BallCalled 5.82 2.29 7.2
Four-Seam 88.76 2115.06 211.56 11.38 19.59 BallCalled 5.72 2.29 7.1
Four-Seam 88.48 2194.37 221.05 15.37 18.74 StrikeSwinging 5.75 2.68 7.4
Four-Seam 90.07 2229.07 214.30 12.78 19.81 StrikeCalled 5.77 2.44 7.1
Four-Seam 91.17 2255.79 218.12 14.11 19.07 StrikeSwinging 5.76 2.39 7.3
Four-Seam 89.68 2195.41 211.79 12.44 21.19 BallCalled 5.72 2.38 7.1
Four-Seam 89.70 2204.05 215.82 13.08 19.24 FoulBallNotFieldable 5.85 2.46 7.2
Changeup 82.21 1987.19 244.37 14.46 8.24 BallCalled 5.49 2.55 8.1
Sinker 89.62 2107.17 224.78 13.78 15.00 InPlay 5.65 2.49 7.5
Four-Seam 88.44 2117.62 210.84 11.31 20.12 FoulBallNotFieldable 5.76 2.40 7.0
Sinker 89.11 2180.48 228.38 14.73 14.35 BallCalled 5.65 2.61 7.6
Sinker 87.78 2191.00 224.18 14.49 16.19 StrikeCalled 5.78 2.57 7.5
Changeup 81.89 1907.15 243.26 17.52 10.20 FoulBallNotFieldable 5.40 2.71 8.1
Changeup 81.52 1904.53 252.02 18.74 7.37 InPlay 5.43 2.82 8.4
Curveball 77.53 2239.34 51.02 -9.30 -6.22 BallCalled 5.64 2.67 1.7
Four-Seam 89.46 2145.17 218.40 14.97 20.01 FoulBallNotFieldable 5.72 2.44 7.3
Curveball 78.20 2342.86 45.81 -6.08 -4.54 StrikeSwinging 5.52 2.65 1.5
Curveball 78.11 2312.60 69.19 -9.93 -2.37 InPlay 5.65 2.54 2.3
Curveball 77.97 2246.63 51.44 -9.91 -6.54 BallCalled 5.52 2.59 1.7
Curveball 78.52 2287.68 36.57 -4.84 -5.08 StrikeCalled 5.49 2.66 1.2
Four-Seam 89.06 2173.77 210.28 10.56 19.26 InPlay 5.77 2.16 7.0
Sinker 88.94 2197.13 222.58 13.81 16.09 StrikeCalled 5.63 2.37 7.4
Four-Seam 89.70 2143.80 207.34 9.50 19.47 BallCalled 5.82 2.38 6.9
Curveball 76.99 2240.65 61.14 -8.68 -3.47 FoulBallNotFieldable 5.58 2.63 2.0
Curveball 76.72 2257.42 72.95 -12.82 -2.55 FoulBallNotFieldable 5.63 2.49 2.4
Four-Seam 89.83 2222.79 212.79 11.83 19.46 BallCalled 5.75 2.33 7.1
Sinker 89.07 2218.11 224.07 14.92 16.49 FoulBallNotFieldable 5.71 2.16 7.5
Curveball 79.82 2420.46 56.60 -7.10 -3.39 BallCalled 5.57 2.42 1.9
Four-Seam 88.39 2229.36 216.13 13.30 19.34 BallCalled 5.64 2.38 7.2
Four-Seam 88.90 2152.97 211.36 11.82 20.55 StrikeCalled 5.81 2.12 7.0
Slider 77.87 2310.83 79.60 -8.80 -0.27 FoulBallNotFieldable 5.58 2.50 2.7
Four-Seam 89.78 2257.45 219.18 14.90 19.43 InPlay 5.75 2.30 7.3
Four-Seam 87.67 2182.30 215.50 12.41 18.66 InPlay 5.68 2.32 7.2
# 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
    AvgReleaseHeight = round(mean(ReleaseHeight, na.rm = TRUE), 2),
    AvgReleaseSide = round(mean(ReleaseSide, na.rm = TRUE), 2)
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Adams")
Summary Pitch Table for Adams
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide
Changeup 3 1 2 33.33% 66.67% 81.87 1932.96 8.60 16.91 246.55 8.2 5.44 2.69
Curveball 8 3 5 37.50% 62.50% 77.98 2293.46 -4.27 -8.58 55.59 1.8 5.58 2.58
Four-Seam 17 6 11 35.29% 64.71% 89.25 2182.67 19.56 12.53 214.08 7.1 5.76 2.37
Sinker 5 1 4 20.00% 80.00% 88.90 2178.78 15.62 14.35 224.80 7.5 5.68 2.44
Slider 1 0 1 0.00% 100.00% 77.87 2310.83 -0.27 -8.80 79.60 2.7 5.58 2.50
# Prepare data for plotting pitch locations
pitch_location_data <- Adams_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 Adams",
       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 Adams",
       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")
  )