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

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_Foley_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 Dennis Helwig")
Detailed Pitch Table for Dennis Helwig
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 92.92 2289.03 209.55 11.32 21.15 BallCalled 6.39 1.64 5.96 7.0
Four-Seam 92.89 2339.37 206.86 10.89 22.62 BallCalled 6.22 1.71 6.05 6.9
Changeup 84.83 1508.47 255.67 12.01 4.27 StrikeCalled 5.94 2.03 5.95 8.5
Changeup 84.54 1385.09 268.95 12.40 1.42 StrikeCalled 5.92 1.90 5.97 9.0
Four-Seam 92.68 2285.67 213.32 11.71 18.86 InPlay 6.39 1.56 5.89 7.1
Curveball 80.97 2268.45 68.60 -17.27 -5.29 InPlay 6.00 2.48 5.53 2.3
Changeup 84.91 1517.46 238.17 14.07 9.89 StrikeCalled 5.92 1.62 5.98 7.9
Four-Seam 93.35 2305.43 223.46 15.33 17.27 BallCalled 6.26 1.32 5.92 7.4
Changeup 85.74 1532.63 250.77 15.83 6.66 StrikeCalled 5.91 1.85 5.91 8.4
Slider 82.74 2401.47 77.76 -13.96 -1.58 BallCalled 5.84 2.20 5.55 2.6
Changeup 86.18 1336.09 235.02 11.85 9.50 BallCalled 5.95 1.80 5.95 7.8
Four-Seam 92.94 2380.84 211.26 12.96 22.49 FoulBallNotFieldable 6.24 1.44 5.95 7.0
Four-Seam 91.47 2286.37 214.58 14.01 21.65 InPlay 6.38 1.49 5.92 7.2
Four-Seam 91.59 2275.49 213.41 13.47 21.60 StrikeCalled 6.39 1.33 5.64 7.1
Slider 80.09 2302.35 88.99 -7.26 1.35 StrikeCalled 6.11 2.22 5.23 3.0
Changeup 85.99 1527.10 254.86 15.97 5.57 StrikeSwinging 5.98 1.83 5.81 8.5
Curveball 78.63 2281.42 78.71 -18.45 -2.08 BallCalled 6.05 2.56 5.42 2.6
Four-Seam 92.07 2349.57 215.36 13.81 20.61 StrikeSwinging 6.27 1.64 6.10 7.2
Four-Seam 93.19 2273.71 215.20 13.61 20.37 StrikeSwinging 6.26 1.57 5.98 7.2
Four-Seam 94.17 2403.20 222.15 16.07 18.85 FoulBallNotFieldable 6.25 1.51 5.80 7.4
Changeup 86.80 1532.36 249.03 16.29 7.52 BallCalled 5.85 1.91 5.78 8.3
Slider 82.63 2273.71 76.03 -11.93 -1.57 BallCalled 5.81 2.36 5.65 2.5
Changeup 85.15 1567.85 258.13 18.07 5.00 StrikeSwinging 5.83 1.90 6.09 8.6
Four-Seam 92.99 2383.82 222.56 15.62 18.21 InPlay 6.32 1.64 5.84 7.4
Four-Seam 92.06 2312.73 211.35 11.76 20.46 BallCalled 6.39 2.31 6.14 7.0
Slider 81.47 2195.14 89.27 -11.57 1.22 StrikeCalled 5.85 3.07 5.41 3.0
Slider 81.54 2332.96 101.73 -14.09 4.23 BallCalled 5.77 3.20 5.64 3.4
Curveball 79.92 2226.86 81.96 -14.45 -0.67 BallCalled 5.90 3.36 5.67 2.7
Four-Seam 92.22 2259.17 207.54 11.59 23.39 StrikeCalled 6.33 2.39 6.02 6.9
Changeup 85.04 1441.31 248.31 18.06 8.40 BallCalled 5.86 2.54 6.04 8.3
Sinker 93.33 2315.62 224.86 16.98 18.17 InPlay 6.28 2.35 6.02 7.5
Sinker 92.01 2370.08 226.44 16.88 17.27 BallCalled 6.33 2.42 5.89 7.5
Slider 82.66 2340.67 77.88 -12.10 -1.25 BallCalled 5.78 3.38 5.67 2.6
Four-Seam 91.02 2337.98 217.70 14.57 20.11 InPlay 6.25 2.45 5.84 7.3
Changeup 85.15 1468.89 225.97 13.46 14.29 BallCalled 5.91 2.97 5.96 7.5
Changeup 83.92 1517.59 262.92 19.15 3.65 StrikeSwinging 5.82 2.88 6.23 8.8
Four-Seam 90.97 2268.88 214.43 13.02 20.20 BallCalled 6.20 2.35 5.94 7.1
Changeup 84.04 1485.05 240.41 16.23 10.48 BallCalled 5.88 2.86 6.10 8.0
Sinker 89.80 2196.31 228.69 15.92 15.17 BallCalled 6.20 2.63 5.91 7.6
Sinker 90.95 2254.71 229.10 17.90 16.81 FoulBallNotFieldable 6.34 2.22 5.84 7.6
Slider 83.00 1178.71 73.07 -9.10 -1.28 BallCalled 5.91 3.42 5.87 2.4
Four-Seam 90.61 2251.18 215.07 14.55 22.01 InPlay 6.32 2.44 5.71 7.2
# 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 %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # 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")
## Total number of pitches thrown:  42
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Dennis Helwig")
Summary Pitch Table for Dennis Helwig
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 28.57% 12 5 7 41.67% 58.33% 85.19 1484.99 7.22 15.28 249.02 8.3 5.90 2.17 5.98
Curveball 7.14% 3 2 1 66.67% 33.33% 79.84 2258.91 -2.68 -16.72 76.42 2.5 5.98 2.80 5.54
Four-Seam 38.10% 16 5 11 31.25% 68.75% 92.32 2312.65 20.62 13.39 214.61 7.2 6.30 1.80 5.92
Sinker 9.52% 4 2 2 50.00% 50.00% 91.52 2284.18 16.86 16.92 227.27 7.6 6.29 2.41 5.92
Slider 16.67% 7 5 2 71.43% 28.57% 82.02 2146.43 0.16 -11.43 83.53 2.8 5.87 2.84 5.57
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Dennis Helwig maximum FB velocity: ", max_fb_velocity, "mph\n")
## Dennis Helwig maximum FB velocity:  94.17 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_data %>%
  select(AutoPitchType, PlateLocHeight, PlateLocSide, PitchCall) %>%
  rename(
    PitchHeight = PlateLocHeight,
    PitchSide = PlateLocSide
  ) %>%
  mutate(
    SwingTake = ifelse(PitchCall %in% c("StrikeSwinging", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | 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) +
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -0.85, xmax = 0.85, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) +
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) +
  labs(title = "Pitch Locations for Dennis Helwig",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) +
  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
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement"), 
       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")
  )