# Filter the data for the pitcher Helwig
Helwig_data <- Upper615 %>%
  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, Extension) %>%
  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),
    Extension = round(Extension, 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 Extension ClockTilt
Changeup 84.33 1325.08 245.43 17.50 9.27 StrikeSwinging 6.01 1.65 6.06 8.2
Slider 83.59 2515.02 86.72 -7.06 0.88 BallCalled 5.94 2.39 5.73 2.9
Four-Seam 93.32 2342.61 218.77 15.46 20.38 FoulBallNotFieldable 6.33 1.58 6.27 7.3
Changeup 85.96 1411.85 242.58 14.27 8.64 InPlay 5.94 1.82 6.11 8.1
Four-Seam 92.55 2169.54 214.63 12.41 19.12 BallCalled 6.32 1.69 6.11 7.2
Four-Seam 92.20 2243.06 219.69 15.24 19.55 StrikeSwinging 6.14 1.74 6.21 7.3
Four-Seam 92.50 2202.21 221.55 14.12 17.08 StrikeSwinging 6.23 1.70 6.37 7.4
Changeup 87.55 1355.13 231.78 14.54 12.58 BallCalled 5.83 2.17 6.26 7.7
Four-Seam 92.46 2205.88 213.90 11.81 18.65 BallCalled 6.28 1.57 6.46 7.1
Four-Seam 91.70 2233.45 214.21 12.54 19.61 FoulBallNotFieldable 6.11 1.58 6.39 7.1
Splitter 85.42 1321.94 240.93 14.64 9.42 InPlay 5.87 1.90 6.35 8.0
Slider 82.58 2281.10 97.82 -9.83 2.76 BallCalled 5.83 2.43 6.08 3.3
Splitter 84.91 1300.99 245.43 15.10 8.15 StrikeCalled 5.81 2.07 6.29 8.2
Four-Seam 92.40 2246.71 218.22 13.84 18.78 BallCalled 6.14 1.68 6.42 7.3
Four-Seam 91.81 2221.29 213.21 11.94 19.47 StrikeCalled 6.09 1.95 6.29 7.1
Changeup 86.58 1338.50 255.03 15.70 5.42 BallCalled 5.73 2.09 6.35 8.5
Four-Seam 92.24 2183.24 211.60 11.95 20.57 BallCalled 6.26 1.75 6.46 7.1
Four-Seam 90.27 2167.48 215.54 12.41 18.54 StrikeCalled 6.23 2.26 5.87 7.2
Slider 81.44 2285.70 92.12 -7.23 1.66 InPlay 5.70 3.37 5.87 3.1
Slider 83.60 2447.68 91.09 -7.94 1.43 StrikeCalled 6.11 2.11 5.81 3.0
Changeup 85.43 1321.84 267.43 17.91 2.10 StrikeSwinging 5.98 1.67 6.22 8.9
Sinker 94.15 2315.31 226.17 17.45 17.90 StrikeCalled 6.20 1.21 6.02 7.5
Four-Seam 92.74 2242.46 222.38 15.50 18.18 FoulBallNotFieldable 6.24 1.50 6.03 7.4
Four-Seam 93.18 2141.89 213.78 12.46 19.79 BallCalled 6.07 1.59 6.48 7.1
Changeup 86.64 1473.41 221.63 11.31 13.98 InPlay 5.88 1.93 6.32 7.4
Four-Seam 91.44 2204.88 218.64 14.66 19.54 BallCalled 6.36 2.33 5.70 7.3
Four-Seam 90.17 2186.80 216.07 13.80 20.21 StrikeCalled 6.27 2.31 6.03 7.2
Slider 81.68 2115.57 102.24 -11.03 3.86 BallCalled 5.83 3.25 5.95 3.4
Four-Seam 90.55 2143.73 216.62 13.37 19.29 StrikeCalled 6.20 2.51 6.08 7.2
Slider 81.99 2103.25 54.09 -3.75 -1.00 StrikeSwinging 5.71 3.23 5.89 1.8
Four-Seam 90.97 2189.68 215.01 13.76 20.95 BallCalled 6.26 2.36 5.88 7.2
Slider 81.83 2246.87 52.06 -6.20 -3.27 StrikeCalled 5.75 3.23 5.78 1.7
Slider 81.14 2278.32 106.40 -12.96 5.40 InPlay 5.68 3.20 5.89 3.5
Four-Seam 91.45 2272.87 222.02 15.70 18.59 BallCalled 6.15 1.65 6.23 7.4
Four-Seam 90.77 2200.83 215.13 13.44 20.33 BallCalled 6.04 1.74 6.02 7.2
Four-Seam 89.37 2166.82 220.01 14.91 18.93 BallCalled 5.94 1.80 6.44 7.3
Changeup 83.32 1324.45 272.96 17.53 0.45 BallCalled 5.76 1.86 6.06 9.1
Slider 80.40 2190.87 100.58 -9.09 3.08 StrikeCalled 5.74 3.25 5.85 3.4
Slider 81.56 2174.26 136.44 -5.03 6.84 StrikeSwinging 5.74 3.39 5.92 4.5
Changeup 83.32 1277.84 238.86 15.87 10.92 FoulBallNotFieldable 5.84 2.79 6.09 8.0
Slider 82.02 1581.27 68.47 -4.88 -0.46 BallCalled 5.59 3.11 5.86 2.3
Sinker 90.90 2188.75 225.51 18.73 19.58 InPlay 6.19 2.14 5.96 7.5
Sinker 89.22 2123.83 227.72 16.91 16.54 FoulBallNotFieldable 6.08 2.50 6.51 7.6
Four-Seam 89.54 2145.73 219.13 14.60 19.10 StrikeCalled 6.24 2.69 6.09 7.3
Changeup 84.07 1324.90 247.20 17.24 8.55 BallCalled 5.81 2.62 6.24 8.2
Changeup 83.65 1304.26 256.33 18.19 5.72 StrikeSwinging 5.82 2.81 6.25 8.5
Slider 81.46 2200.82 108.31 -9.26 4.47 BallCalled 5.57 3.44 6.02 3.6
Sinker 90.76 2149.39 224.11 16.75 18.41 StrikeCalled 6.18 2.58 5.83 7.5
Changeup 83.88 1347.54 275.13 14.85 -0.16 BallCalled 5.67 2.90 6.15 9.2
Four-Seam 91.04 2151.35 216.93 13.74 19.46 BallCalled 6.18 2.52 5.89 7.2
Sinker 89.48 2136.36 233.64 18.47 14.82 BallCalled 5.95 2.58 6.47 7.8
Slider 80.35 1137.11 72.88 -9.65 -1.51 BallCalled 5.82 3.26 5.89 2.4
Curveball 80.08 2226.21 73.11 -10.08 -1.71 StrikeCalled 5.74 3.17 5.82 2.4
Four-Seam 89.90 2208.67 217.60 14.36 19.81 BallCalled 5.98 2.35 6.48 7.3
Four-Seam 89.04 2129.40 213.79 12.34 19.69 StrikeSwinging 6.16 2.42 6.45 7.1
Slider 81.23 2156.37 88.00 -10.46 1.11 InPlay 5.65 3.16 5.76 2.9
Four-Seam 88.95 2064.51 208.17 10.63 21.11 BallCalled 6.18 1.44 6.38 6.9
Four-Seam 88.24 2125.36 209.61 10.90 20.41 InPlay 6.10 1.53 6.54 7.0
# Calculate the total number of pitches
total_pitches <- nrow(detailed_pitch_table)

# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    Usage = sprintf("%.2f%%", n() / total_pitches * 100),
    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),
    AvgExtension = round(mean(Extension, na.rm = TRUE), 2)
  ) %>%
  select(AutoPitchType, Usage, everything())

# Display the total number of pitches
cat("Total number of pitches thrown: ", total_pitches, "\n\n")
## Total number of pitches thrown:  58
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Helwig")
Summary Pitch Table for Helwig
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 18.97% 11 5 6 45.45% 54.55% 84.98 1345.89 7.04 15.90 250.40 8.3 5.84 2.21 6.19
Curveball 1.72% 1 0 1 0.00% 100.00% 80.08 2226.21 -1.71 -10.08 73.11 2.4 5.74 3.17 5.82
Four-Seam 43.10% 25 13 12 52.00% 48.00% 91.15 2191.62 19.49 13.44 216.25 7.2 6.18 1.93 6.22
Sinker 8.62% 5 1 4 20.00% 80.00% 90.90 2182.73 17.45 17.66 227.43 7.6 6.12 2.20 6.16
Slider 24.14% 14 6 8 42.86% 57.14% 81.78 2122.44 1.80 -8.17 89.80 3.0 5.76 3.06 5.88
Splitter 3.45% 2 0 2 0.00% 100.00% 85.16 1311.47 8.79 14.87 243.18 8.1 5.84 1.98 6.32
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Two-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Helwig maximum FB velocity: ", max_fb_velocity, "mph\n")
## Helwig maximum FB velocity:  94.15 mph
# 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")
  )