# Filter the data for the pitcher Eshleman
Eshleman_data <- Sanford64 %>%
  filter(Pitcher == "Eshleman, Jack")

# Create a detailed table for each pitch
detailed_pitch_table <- Eshleman_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 Eshleman")
Detailed Pitch Table for Eshleman
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ClockTilt
Four-Seam 89.83 2312.60 195.76 6.69 24.88 FoulBallFieldable 6.5
Slider 85.67 2519.44 129.48 -6.22 6.40 BallCalled 4.3
Slider 83.76 2483.65 117.89 -5.56 4.31 StrikeSwinging 3.9
Slider 85.27 2592.55 22.37 -2.10 -3.48 StrikeCalled 0.7
Four-Seam 91.70 2402.07 194.93 6.43 25.36 BallCalled 6.5
Four-Seam 90.88 2229.45 205.74 9.36 20.50 BallCalled 6.9
Four-Seam 90.44 2295.61 195.01 6.05 23.69 BallCalled 6.5
Four-Seam 90.07 2154.86 205.08 9.59 21.60 StrikeCalled 6.8
Four-Seam 91.30 2265.90 203.38 9.02 21.94 FoulBallNotFieldable 6.8
Four-Seam 90.76 2325.30 193.54 5.25 23.05 StrikeSwinging 6.5
Slider 85.58 2553.98 183.19 0.67 13.16 StrikeCalled 6.1
Cutter 86.29 2526.26 187.89 1.96 15.31 BallCalled 6.3
Slider 83.79 2584.66 153.78 -4.70 10.76 FoulBallNotFieldable 5.1
Four-Seam 90.96 2349.17 193.60 5.03 21.83 BallCalled 6.5
Curveball 82.46 2577.02 358.06 0.23 -5.60 FoulBallNotFieldable 11.9
Slider 85.92 2578.23 166.26 -1.40 6.74 InPlay 5.5
Four-Seam 90.93 2363.57 215.07 12.12 18.38 InPlay 7.2
Changeup 85.34 1978.93 252.82 15.38 6.02 StrikeCalled 8.4
Changeup 84.39 1973.92 251.31 17.96 7.31 InPlay 8.4
Four-Seam 89.79 2167.49 194.96 5.77 22.79 BallCalled 6.5
Slider 83.65 2467.20 192.08 2.90 14.82 StrikeSwinging 6.4
Slider 85.11 2488.42 188.32 1.32 10.24 StrikeSwinging 6.3
Slider 82.87 2534.12 50.04 -7.44 -4.88 BallCalled 1.7
Four-Seam 90.64 2276.80 197.39 6.87 23.09 BallCalled 6.6
Four-Seam 88.90 2189.29 188.95 3.55 23.70 FoulBallNotFieldable 6.3
Slider 83.76 2441.74 191.37 2.11 11.62 StrikeSwinging 6.4
Four-Seam 89.79 2345.23 193.12 5.23 23.66 StrikeCalled 6.4
Slider 84.71 2514.75 143.33 -3.85 6.48 BallCalled 4.8
Slider 83.37 2484.54 143.92 -4.02 6.87 StrikeSwinging 4.8
Slider 82.97 2602.83 37.58 -3.26 -3.02 StrikeSwinging 1.3
Four-Seam 89.75 2329.49 197.54 7.35 24.53 BallCalled 6.6
Four-Seam 89.32 2291.35 198.22 6.89 22.22 StrikeCalled 6.6
Slider 83.75 2493.93 136.06 -4.88 6.41 StrikeCalled 4.5
Slider 85.22 2527.27 181.17 0.09 5.51 BallCalled 6.0
Four-Seam 90.62 2346.22 190.38 4.29 24.70 InPlay 6.3
Four-Seam 89.22 2330.55 191.23 4.83 25.57 BallCalled 6.4
Four-Seam 88.25 2205.38 195.06 5.91 23.30 StrikeCalled 6.5
Slider 83.14 2547.36 180.75 0.14 11.98 FoulBallNotFieldable 6.0
Four-Seam 91.02 2287.35 195.69 5.92 22.30 StrikeSwinging 6.5
Four-Seam 88.91 2227.85 212.04 11.91 20.20 StrikeCalled 7.1
Slider 85.53 2482.57 175.92 -0.58 9.15 BallCalled 5.9
Curveball 79.99 2489.11 352.45 0.93 -5.83 StrikeSwinging 11.7
Four-Seam 91.22 2402.14 193.02 5.46 24.78 FoulBallNotFieldable 6.4
Four-Seam 90.23 2314.09 199.15 8.04 24.41 FoulBallNotFieldable 6.6
Slider 85.09 2438.64 187.58 1.32 11.06 FoulBallNotFieldable 6.3
Changeup 83.75 1997.06 244.06 16.19 9.15 FoulBallNotFieldable 8.1
Four-Seam 89.26 2147.52 213.97 11.14 17.67 InPlay 7.1
Four-Seam 88.31 2207.96 206.34 9.40 20.22 BallCalled 6.9
Slider 82.81 2384.97 155.50 -3.39 8.66 StrikeSwinging 5.2
Four-Seam 88.72 2246.26 187.93 2.78 21.14 BallCalled 6.3
Four-Seam 87.71 2128.79 178.23 -0.64 21.91 InPlay 5.9
# 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 Eshleman")
Summary Pitch Table for Eshleman
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 3 0 3 0.00% 100.00% 84.49 1983.30 7.49 16.51 249.40 8.3
Curveball 2 0 2 0.00% 100.00% 81.22 2533.06 -5.72 0.58 355.26 11.8
Cutter 1 1 0 100.00% 0.00% 86.29 2526.26 15.31 1.96 187.89 6.3
Four-Seam 26 10 16 38.46% 61.54% 89.94 2274.70 22.59 6.70 197.51 6.6
Slider 19 5 14 26.32% 73.68% 84.31 2511.62 6.99 -2.04 144.03 4.8
# Prepare data for plotting pitch locations
pitch_location_data <- Eshleman_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)) +
  labs(title = "Pitch Locations for Eshleman",
       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 Eshleman",
       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")
  )