# Filter the data for the pitcher Helwig
Helwig_data <- Sanford69 %>%
  filter(Pitcher == "Helwig, Dennis")

# Create a detailed table for each pitch
detailed_pitch_table <- Helwig_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 Helwig")
Detailed Pitch Table for Helwig
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide ClockTilt
Four-Seam 91.97 2206.78 211.56 12.14 20.83 InPlay 6.43 1.11 7.1
Four-Seam 92.79 2296.42 220.67 15.77 19.42 StrikeCalled 6.47 1.39 7.4
Four-Seam 92.54 2353.48 218.66 15.60 20.57 BallCalled 6.42 1.10 7.3
Curveball 82.98 2389.10 95.15 -17.04 2.82 BallCalled 6.16 2.11 3.2
Four-Seam 91.34 2148.28 208.92 9.61 18.43 InPlay 6.52 1.39 7.0
Curveball 80.22 2210.58 82.48 -15.14 -0.75 BallCalled 6.21 2.02 2.7
Four-Seam 92.11 2363.07 213.13 12.83 20.72 StrikeCalled 6.37 1.22 7.1
Four-Seam 91.78 2240.66 215.47 13.34 19.76 FoulBallNotFieldable 6.39 1.24 7.2
Four-Seam 93.19 2205.86 209.23 10.53 19.83 BallCalled 6.53 1.15 7.0
Slider 82.04 2353.50 102.52 -9.90 3.27 BallCalled 6.09 1.91 3.4
Four-Seam 91.95 2204.52 219.31 12.22 15.93 InPlay 6.31 1.54 7.3
Slider 81.32 2277.59 64.60 -8.10 -2.58 StrikeCalled 6.20 1.97 2.2
Slider 79.99 2325.17 86.94 -16.23 0.49 StrikeCalled 6.09 2.22 2.9
Four-Seam 93.20 2175.20 204.50 9.23 21.31 BallCalled 6.44 1.32 6.8
Slider 81.41 2324.73 99.06 -12.72 3.30 FoulBallNotFieldable 5.99 2.08 3.3
Four-Seam 92.77 2186.69 206.44 8.71 18.68 BallCalled 6.39 1.52 6.9
Four-Seam 91.42 2127.72 211.76 10.81 18.60 StrikeSwinging 6.32 1.43 7.1
Four-Seam 91.05 2124.58 213.57 11.23 18.09 StrikeCalled 6.42 1.37 7.1
Four-Seam 91.39 2100.43 212.09 10.02 17.13 InPlay 6.33 1.43 7.1
Slider 81.23 2324.00 89.73 -4.77 1.28 StrikeCalled 6.00 2.05 3.0
Slider 79.68 2359.11 71.99 -8.27 -1.21 BallCalled 6.04 2.13 2.4
Four-Seam 91.03 2149.21 211.93 11.41 19.44 BallCalled 6.45 1.33 7.1
Slider 79.57 2117.66 88.48 -12.64 1.21 StrikeCalled 6.14 2.00 2.9
Four-Seam 92.31 2214.13 210.25 11.55 20.94 StrikeCalled 6.44 1.43 7.0
Four-Seam 89.25 2098.25 211.15 12.26 21.51 InPlay 6.51 1.29 7.0
Slider 79.54 2316.53 113.43 -12.18 6.66 InPlay 6.06 2.23 3.8
Four-Seam 89.29 2141.51 209.88 10.36 19.23 StrikeCalled 6.27 1.76 7.0
Changeup 85.32 1388.57 239.87 15.52 10.19 BallCalled 5.88 1.75 8.0
Four-Seam 91.39 2229.99 217.20 14.64 20.49 FoulBallNotFieldable 6.37 1.73 7.2
Four-Seam 93.15 2272.39 207.32 10.52 21.49 BallCalled 6.34 1.31 6.9
Changeup 86.87 1367.60 250.34 14.72 6.45 BallCalled 5.91 1.61 8.3
Four-Seam 90.81 2122.15 204.40 9.20 21.48 FoulBallNotFieldable 6.38 1.63 6.8
Four-Seam 90.04 2043.49 209.43 10.81 20.33 BallCalled 6.41 1.73 7.0
Four-Seam 91.58 2165.17 205.85 9.78 21.32 BallCalled 6.43 2.07 6.9
Four-Seam 89.15 2093.96 200.32 7.49 21.40 InPlay 6.35 2.13 6.7
Four-Seam 90.89 2178.04 215.44 12.76 19.03 BallCalled 6.37 2.23 7.2
Four-Seam 90.63 2160.12 208.34 10.47 20.50 InPlay 6.22 2.32 6.9
Slider 79.79 2206.45 108.84 -9.00 4.35 BallCalled 6.18 2.16 3.6
Slider 81.33 2224.47 90.64 -9.63 1.55 FoulBallNotFieldable 6.28 2.22 3.0
Four-Seam 90.53 2056.19 213.53 11.94 19.23 FoulBallNotFieldable 6.33 1.35 7.1
Curveball 81.77 2202.44 45.44 -6.43 -4.94 InPlay 5.97 2.15 1.5
Curveball 80.94 2276.33 68.97 -11.20 -3.00 BallCalled 6.04 2.12 2.3
Four-Seam 90.24 2087.05 205.94 9.36 20.33 BallCalled 6.30 1.22 6.9
Four-Seam 89.30 2092.85 213.48 11.41 18.35 BallCalled 6.35 1.40 7.1
Four-Seam 88.73 2104.37 214.03 13.43 21.01 BallCalled 6.39 1.49 7.1
Four-Seam 90.29 2207.69 211.51 11.78 20.30 BallCalled 6.18 2.30 7.1
Four-Seam 88.46 2135.46 216.16 13.15 19.12 BallCalled 6.21 2.27 7.2
Four-Seam 88.75 2132.32 215.35 12.81 19.18 BallCalled 6.27 2.35 7.2
Four-Seam 89.18 2147.26 216.20 13.41 19.45 StrikeCalled 6.21 2.44 7.2
Four-Seam 88.87 2110.64 216.85 13.17 18.69 BallCalled 6.10 2.46 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 Helwig")
Summary Pitch Table for Helwig
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide
Changeup 2 2 0 100.00% 0.00% 86.10 1378.09 8.32 15.12 245.11 8.2 5.89 1.68
Curveball 4 3 1 75.00% 25.00% 81.48 2269.61 -1.47 -12.45 73.01 2.4 6.10 2.10
Four-Seam 34 16 18 47.06% 52.94% 90.92 2166.94 19.77 11.58 211.76 7.1 6.36 1.63
Slider 10 3 7 30.00% 70.00% 80.59 2282.92 1.83 -10.34 91.62 3.0 6.11 2.10
# Prepare data for plotting pitch locations
pitch_location_data <- Helwig_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 Helwig",
       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 Helwig",
       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")
  )