# 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
Changeup 84.22 1355.35 265.52 14.79 2.32 InPlay 5.60 1.85 6.60 8.9
Sinker 92.80 2291.58 226.52 15.71 15.90 FoulBallNotFieldable 6.12 1.23 6.74 7.6
Slider 83.74 2328.11 85.16 -5.46 0.75 StrikeCalled 5.63 2.18 6.50 2.8
Slider 82.90 2477.37 114.83 -6.94 4.35 InPlay 5.37 2.20 6.44 3.8
Four-Seam 92.73 2325.86 217.57 12.74 17.55 BallCalled 5.85 1.62 6.86 7.3
Four-Seam 93.09 2359.35 220.12 13.67 17.23 FoulBallNotFieldable 5.88 1.73 6.77 7.3
Changeup 84.80 1347.87 259.84 13.91 3.61 BallCalled 5.34 1.98 6.87 8.7
Four-Seam 92.21 2284.24 217.28 13.62 18.94 StrikeSwinging 5.97 1.61 6.50 7.2
Four-Seam 94.68 2399.11 214.87 12.23 18.56 FoulBallNotFieldable 5.93 1.48 6.88 7.2
Changeup 87.43 1430.93 240.71 12.95 8.36 InPlay 5.54 1.82 6.89 8.0
Four-Seam 92.09 2285.95 206.22 9.57 20.60 FoulBallNotFieldable 5.92 2.37 6.45 6.9
Four-Seam 91.87 2281.95 211.17 12.57 21.98 InPlay 5.97 2.12 6.41 7.0
Slider 83.04 2347.77 148.74 -6.60 12.07 StrikeSwinging 5.53 3.13 6.28 5.0
Four-Seam 92.79 2353.40 224.77 16.66 17.91 FoulBallNotFieldable 5.89 2.17 6.67 7.5
Slider 83.26 2313.66 114.33 -6.49 4.26 BallinDirt 5.16 3.04 6.67 3.8
Four-Seam 92.33 2322.35 213.94 13.39 20.94 StrikeSwinging 6.04 2.00 6.47 7.1
Changeup 83.84 1341.60 258.34 13.07 3.81 BallCalled 5.62 1.49 6.57 8.6
Four-Seam 89.58 2184.38 216.92 13.63 19.22 InPlay 5.93 1.53 6.54 7.2
Changeup 85.22 1234.13 240.87 11.79 7.68 BallCalled 5.52 2.79 6.58 8.0
Sinker 90.69 2216.63 226.84 15.71 15.75 BallCalled 5.91 2.44 6.59 7.6
Four-Seam 91.11 2192.59 218.94 13.78 18.11 StrikeCalled 5.84 2.19 6.57 7.3
Four-Seam 89.64 2090.34 220.71 13.76 17.06 FoulBallNotFieldable 5.90 2.29 6.69 7.4
Changeup 85.16 1248.16 241.33 11.51 7.41 FoulBallNotFieldable 5.46 2.53 6.62 8.0
Four-Seam 90.25 2156.26 220.44 13.34 16.61 BallCalled 5.93 2.41 6.76 7.3
Four-Seam 91.69 2278.55 219.54 13.46 17.24 BallCalled 5.77 2.38 6.81 7.3
Slider 83.08 2202.94 90.15 -8.04 1.27 BallinDirt 5.55 3.01 6.47 3.0
Four-Seam 91.23 2152.07 220.66 13.13 16.31 FoulBallNotFieldable 6.00 2.09 6.43 7.4
Four-Seam 91.57 2258.31 218.76 13.49 17.93 BallCalled 6.02 2.13 6.53 7.3
Slider 83.20 2265.15 96.66 -6.33 2.16 StrikeCalled 5.54 3.05 6.47 3.2
Slider 81.19 2180.39 70.35 -6.76 -0.93 InPlay 5.54 2.96 6.33 2.3
Four-Seam 91.59 2246.53 216.10 12.90 18.82 StrikeSwinging 6.05 2.13 6.54 7.2
Four-Seam 92.44 2215.70 220.09 13.13 16.70 BallCalled 5.95 2.21 6.67 7.3
Slider 82.05 2185.56 89.89 -8.69 1.43 StrikeSwinging 5.51 2.95 6.36 3.0
Slider 81.86 2231.24 90.63 -11.14 1.56 InPlay 5.47 3.17 6.30 3.0
Changeup 84.29 1340.00 245.42 11.06 6.39 BallCalled 5.41 2.63 6.69 8.2
Sinker 90.15 2170.45 231.08 14.12 12.59 InPlay 5.92 2.22 6.70 7.7
Slider 84.79 2235.49 83.62 -6.26 0.62 InPlay 5.55 2.92 6.48 2.8
Four-Seam 91.73 2327.33 221.48 15.13 18.30 BallCalled 5.83 2.45 6.65 7.4
Changeup 84.13 1321.32 245.97 14.95 7.83 StrikeSwinging 5.40 2.74 6.79 8.2
Changeup 84.65 1267.18 258.14 17.07 4.76 BallCalled 5.50 2.81 6.43 8.6
Sinker 90.05 2263.98 225.40 16.38 17.35 BallinDirt 5.89 2.35 6.67 7.5
Sinker 88.03 2141.77 225.60 15.14 16.09 BallCalled 5.84 2.46 6.62 7.5
# 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")
## 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 21.43% 9 5 4 55.56% 44.44% 84.86 1320.73 5.80 13.46 250.68 8.4 5.49 2.29 6.67
Four-Seam 42.86% 18 6 12 33.33% 66.67% 91.81 2261.90 18.33 13.34 217.75 7.3 5.93 2.05 6.62
Sinker 11.90% 5 2 3 40.00% 60.00% 90.34 2216.88 15.54 15.41 227.09 7.6 5.94 2.14 6.66
Slider 23.81% 10 0 10 0.00% 100.00% 82.91 2276.77 2.75 -7.27 98.44 3.3 5.48 2.86 6.43
# 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("Dennis Helwig maximum FB velocity: ", max_fb_velocity, "mph\n")
## Dennis Helwig maximum FB velocity:  94.68 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", "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) +
  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.75, xmax = 0.75, 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")
  )