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

# Create a detailed table for each pitch
detailed_pitch_table <- Aidan_Krupp_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
Changeup 85.19 1915.64 262.74 18.62 3.81 BallCalled 3.99 2.65 6.14 8.8
Changeup 86.88 1901.32 246.92 20.15 10.04 FoulBallNotFieldable 4.10 2.63 5.91 8.2
Changeup 83.83 1870.46 248.37 21.17 9.95 BallCalled 4.02 2.58 5.98 8.3
Changeup 86.76 1942.71 246.72 21.04 10.53 FoulBallNotFieldable 4.05 2.66 5.90 8.2
Curveball 76.22 2054.22 99.30 -11.32 3.61 BallCalled 3.86 2.63 5.79 3.3
Changeup 87.03 2063.82 261.78 19.89 4.25 InPlay 4.07 2.70 5.99 8.7
Changeup 87.92 2097.69 251.27 19.48 7.94 BallCalled 3.96 2.73 5.90 8.4
Sinker 87.20 2088.83 248.20 20.70 9.65 StrikeCalled 3.89 2.60 5.94 8.3
Curveball 75.80 2138.50 86.91 -9.63 1.19 BallCalled 3.69 2.80 5.57 2.9
Sinker 86.58 2119.45 245.71 20.27 10.52 FoulBallNotFieldable 3.84 2.72 5.89 8.2
Curveball 74.99 2059.91 56.96 -6.20 -2.33 BallinDirt 3.81 2.58 6.04 1.9
Changeup 87.26 1965.64 263.89 19.62 3.34 BallinDirt 3.82 2.53 5.99 8.8
Changeup 86.11 1965.31 251.58 20.10 7.96 BallCalled 3.91 2.60 6.28 8.4
Changeup 85.58 1939.92 256.01 20.12 6.24 FoulBallNotFieldable 3.88 2.65 5.95 8.5
Changeup 86.72 1882.17 267.74 17.24 1.79 BallCalled 3.88 2.54 6.15 8.9
Changeup 86.03 1863.58 223.40 13.27 15.20 StrikeSwinging 3.73 2.70 6.12 7.4
Changeup 86.49 1858.51 269.61 16.86 1.26 StrikeSwinging 3.84 2.46 6.06 9.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")
## Total number of pitches thrown:  17
# 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 70.59% 12 5 7 41.67% 58.33% 86.32 1938.90 6.86 18.96 254.17 8.5 3.94 2.62 6.03
Curveball 17.65% 3 2 1 66.67% 33.33% 75.67 2084.21 0.82 -9.05 81.06 2.7 3.79 2.67 5.80
Sinker 11.76% 2 0 2 0.00% 100.00% 86.89 2104.14 10.09 20.48 246.95 8.2 3.87 2.66 5.92
# 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:  87.2 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Aidan_Krupp_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 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)) +
  scale_y_continuous(limits = c(-25, 25)) +
  labs(title = "Pitch Movement for Aidan Krupp",
       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")
  )