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

# 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 Aidan Krupp")
Detailed Pitch Table for Aidan Krupp
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 75.60 2033.59 80.81 -7.89 0.43 BallCalled 3.76 2.51 6.14 2.7
Sinker 88.47 2067.20 246.73 21.25 10.35 BallCalled 4.05 2.42 6.12 8.2
Changeup 86.19 1958.24 250.44 21.58 8.96 BallCalled 3.92 2.53 6.19 8.3
Changeup 87.38 2024.79 249.05 20.87 9.24 StrikeCalled 3.87 2.45 6.23 8.3
Sinker 87.87 2018.96 245.13 22.42 11.70 BallCalled 3.95 2.49 6.29 8.2
Sinker 87.13 2093.54 245.09 20.10 10.68 StrikeCalled 3.71 2.42 6.54 8.2
Curveball 75.88 2012.35 84.39 -9.17 0.79 BallCalled 3.81 2.43 5.80 2.8
Sinker 88.41 2116.00 250.55 21.88 8.95 StrikeSwinging 3.80 2.60 6.17 8.4
Changeup 86.99 2005.02 246.89 21.42 10.45 InPlay 3.83 2.42 6.09 8.2
Sinker 87.34 2012.54 238.57 20.03 13.63 FoulBallNotFieldable 4.02 2.39 6.12 8.0
Changeup 85.58 1903.48 242.15 18.74 11.32 BallCalled 3.83 2.52 5.97 8.1
Changeup 85.12 1939.17 253.55 21.31 7.65 BallCalled 3.80 2.52 6.15 8.5
Changeup 86.39 1976.11 247.22 20.15 9.84 BallCalled 3.90 2.63 6.00 8.2
Sinker 88.37 2020.91 244.85 19.58 10.43 FoulBallNotFieldable 3.79 2.59 6.13 8.2
Changeup 86.28 1990.54 247.73 20.03 9.68 InPlay 3.76 2.58 6.05 8.3
Curveball 75.23 2144.01 96.47 -12.22 3.11 BallCalled 3.65 2.64 6.17 3.2
Sinker 88.37 2132.28 247.29 23.42 11.30 InPlay 3.86 2.49 6.01 8.2
Curveball 75.11 2129.56 103.37 -12.45 4.79 StrikeSwinging 3.51 2.43 6.00 3.4
Curveball 74.42 1996.39 88.29 -11.94 1.50 FoulBallNotFieldable 3.55 2.53 6.08 2.9
Changeup 87.07 2062.00 252.64 21.10 8.00 BallCalled 3.77 2.53 6.27 8.4
Curveball 76.83 2009.35 88.13 -11.96 1.42 BallCalled 3.64 2.52 6.07 2.9
Changeup 87.19 2052.12 246.00 20.88 10.66 StrikeSwinging 3.76 2.59 6.18 8.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 == "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:  22
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Aidan Krupp")
Summary Pitch Table for Aidan Krupp
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 40.91% 9 5 4 55.56% 44.44% 86.47 1990.16 9.53 20.68 248.41 8.3 3.83 2.53 6.13
Curveball 27.27% 6 4 2 66.67% 33.33% 75.51 2054.21 2.01 -10.94 90.24 3.0 3.65 2.51 6.04
Sinker 31.82% 7 2 5 28.57% 71.43% 87.99 2065.92 11.01 21.24 245.46 8.2 3.88 2.49 6.20
# 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("Aidan Krupp maximum FB velocity: ", max_fb_velocity, "mph\n")
## Aidan Krupp maximum FB velocity:  88.47 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.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 Aidan Krupp",
       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")
  )