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

# Create a detailed table for each pitch
detailed_pitch_table <- 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 Krupp")
Detailed Pitch Table for Krupp
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 75.77 2027.59 94.55 -6.01 2.03 FoulBall 3.66 2.61 6.14 3.2
Curveball 75.59 2092.08 67.95 -8.38 -1.75 BallinDirt 3.77 2.54 6.09 2.3
Curveball 75.55 2173.88 85.25 -8.30 0.79 InPlay 3.69 2.56 6.09 2.8
Changeup 86.23 2068.24 258.10 20.42 5.52 BallCalled 3.67 2.58 6.26 8.6
Changeup 86.20 2058.01 255.44 20.92 6.68 FoulBall 3.70 2.70 6.15 8.5
Changeup 85.76 2089.38 253.61 19.71 7.15 BallCalled 3.63 2.73 6.25 8.5
Curveball 76.27 2169.48 46.21 -5.75 -3.88 BallinDirt 3.65 2.64 6.23 1.5
Changeup 86.62 2029.46 245.13 18.18 9.83 FoulBall 3.65 2.67 6.12 8.2
Changeup 86.60 2109.17 250.04 21.28 9.06 StrikeSwinging 3.69 2.73 6.12 8.3
Changeup 86.22 2069.94 250.43 21.02 8.74 BallCalled 3.66 2.73 6.03 8.3
Sinker 85.91 2118.93 275.51 22.30 -0.85 InPlay 3.62 2.61 6.05 9.2
Curveball 75.94 2045.08 82.40 -4.20 0.81 BallCalled 3.70 2.60 6.32 2.7
Curveball 74.10 1906.25 117.77 -6.53 5.00 BallCalled 3.53 2.58 6.22 3.9
Changeup 86.44 2102.67 265.93 19.98 2.68 FoulBall 3.68 2.69 6.30 8.9
Sinker 86.38 2129.88 250.83 19.80 8.16 FoulBall 3.77 2.62 6.23 8.4
Curveball 76.48 2090.34 93.66 -10.48 2.35 BallCalled 3.63 2.57 5.92 3.1
Changeup 86.54 2063.04 261.73 18.63 4.04 InPlay 3.79 2.58 5.90 8.7
Changeup 85.51 2004.19 262.38 18.56 3.88 InPlay 3.87 2.50 6.22 8.7
# 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:  18 \n\n
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Krupp")
Summary Pitch Table for Krupp
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 50.00% 9 3 6 33.33% 66.67% 86.24 2066.01 6.40 19.86 255.87 8.5 3.70 2.66 6.15
Curveball 38.89% 7 3 4 42.86% 57.14% 75.67 2072.10 0.76 -7.09 83.97 2.8 3.66 2.59 6.14
Sinker 11.11% 2 0 2 0.00% 100.00% 86.14 2124.40 3.66 21.05 263.17 8.8 3.70 2.62 6.14
# 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('Krupp maximum FB velocity: ', max_fb_velocity, '\\n')
## Krupp maximum FB velocity:  86.38 \n
# Prepare data for plotting pitch locations
pitch_location_data <- 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) + # 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 Krupp',
       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 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')
  )