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

# Create a detailed table for each pitch
detailed_pitch_table <- Helwig_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 Helwig")
Detailed Pitch Table for Helwig
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ClockTilt
Four-Seam 92.65 2135.03 222.61 15.10 17.55 BallCalled 7.4
Four-Seam 92.67 2232.40 224.43 14.56 15.98 StrikeCalled 7.5
Slider 80.15 2429.50 77.63 -14.52 -1.74 BallCalled 2.6
Four-Seam 92.08 2338.58 215.69 13.66 20.19 FoulBallNotFieldable 7.2
Slider 82.40 2552.32 58.08 -6.24 -2.45 BallCalled 1.9
Curveball 78.80 2383.12 48.40 -5.61 -3.46 FoulBallNotFieldable 1.6
Four-Seam 93.06 2305.72 210.02 11.53 21.02 StrikeSwinging 7.0
Four-Seam 91.81 2315.66 214.33 14.18 21.88 BallCalled 7.1
Four-Seam 91.42 2274.59 212.84 12.58 20.61 InPlay 7.1
Four-Seam 92.90 2198.59 208.49 10.69 20.77 BallCalled 6.9
Changeup 85.97 1326.23 255.17 16.15 5.44 StrikeCalled 8.5
Changeup 85.12 1386.62 254.48 15.61 5.51 BallCalled 8.5
Four-Seam 94.34 2301.57 210.82 10.68 18.96 InPlay 7.0
Four-Seam 90.86 2255.29 208.02 11.38 22.49 InPlay 6.9
Four-Seam 92.03 2267.24 208.78 11.35 21.74 BallCalled 7.0
Slider 81.83 1176.23 49.74 -5.38 -3.25 BallCalled 1.7
Four-Seam 91.01 2223.01 210.17 11.48 20.82 StrikeCalled 7.0
Slider 82.05 2365.83 67.99 -4.23 -0.37 StrikeCalled 2.3
Slider 79.52 2442.69 62.30 -4.64 -1.12 FoulBallNotFieldable 2.1
Four-Seam 92.36 2308.48 210.47 10.74 19.46 InPlay 7.0
# 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 Helwig")
Summary Pitch Table for Helwig
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 2 1 1 50.00% 50.00% 85.54 1356.42 5.47 15.88 254.82 8.5
Curveball 1 0 1 0.00% 100.00% 78.80 2383.12 -3.46 -5.61 48.40 1.6
Four-Seam 12 4 8 33.33% 66.67% 92.27 2263.01 20.12 12.33 213.06 7.1
Slider 5 3 2 60.00% 40.00% 81.19 2193.31 -1.79 -7.00 63.15 2.1
# 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)) +
  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")
  )