# Filter the data for the pitcher Carozza
Carozza_data <- Adams619 %>%
  filter(Pitcher == 'Carozza, Kyle')

# Create a detailed table for each pitch
detailed_pitch_table <- Carozza_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 Carozza')
Detailed Pitch Table for Carozza
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 79.09 1908.74 210.51 9.99 18.46 InPlay 6.05 2.69 5.38 7.0
Four-Seam 89.56 2355.10 208.34 9.99 19.72 BallCalled 6.08 1.16 6.13 6.9
Four-Seam 89.27 2308.89 219.72 13.19 17.07 BallCalled 6.02 1.13 6.33 7.3
Four-Seam 89.83 2394.58 218.26 13.65 18.49 BallCalled 6.08 1.27 6.30 7.3
Curveball 78.70 2103.80 14.27 -1.26 -3.38 StrikeCalled 5.83 1.21 5.80 0.5
Curveball 77.80 2188.66 21.54 -3.36 -6.99 StrikeCalled 5.79 1.38 5.67 0.7
Four-Seam 90.87 2400.48 217.68 12.69 17.59 StrikeCalled 5.96 1.17 5.95 7.3
Curveball 78.83 2157.40 35.32 -4.43 -4.75 InPlay 5.77 1.26 5.70 1.2
Four-Seam 88.30 2294.54 204.78 7.15 16.63 FoulBallNotFieldable 5.93 1.26 6.27 6.8
Four-Seam 90.27 2278.80 208.50 10.80 21.10 BallCalled 6.01 1.28 5.99 7.0
Four-Seam 88.45 2256.51 217.37 12.89 18.11 BallCalled 6.10 1.24 6.08 7.2
Curveball 76.11 2077.54 27.79 -3.10 -4.29 BallCalled 5.91 1.33 5.75 0.9
Four-Seam 89.53 2305.07 222.93 13.71 15.91 InPlay 5.98 1.38 6.05 7.4
Curveball 78.22 2037.69 14.23 -1.87 -5.88 BallCalled 5.76 1.28 5.78 0.5
Four-Seam 89.75 2353.87 205.21 9.36 21.09 StrikeSwinging 6.05 1.24 6.11 6.8
Four-Seam 90.36 2308.31 210.41 11.01 19.97 BallCalled 6.06 1.09 5.78 7.0
Curveball 78.42 2075.27 25.83 -3.53 -5.82 StrikeCalled 5.85 1.30 5.81 0.9
Four-Seam 91.25 2410.13 219.16 13.63 17.91 InPlay 5.98 1.12 6.02 7.3
Four-Seam 89.87 2313.39 209.49 10.84 20.38 FoulBallNotFieldable 5.98 0.98 6.05 7.0
Four-Seam 90.26 2375.16 205.95 8.06 17.74 InPlay 6.03 1.26 5.83 6.9
# 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)
## Total number of pitches thrown: 20
# Display the summary table
knitr::kable(pitch_summary, caption = 'Summary Pitch Table for Carozza')
Summary Pitch Table for Carozza
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 5.00% 1 0 1 0.00% 100.00% 79.09 1908.74 18.46 9.99 210.51 7.0 6.05 2.69 5.38
Curveball 30.00% 6 2 4 33.33% 66.67% 78.01 2106.73 -5.19 -2.92 23.16 0.8 5.82 1.29 5.75
Four-Seam 65.00% 13 6 7 46.15% 53.85% 89.81 2334.99 18.59 11.31 212.91 7.1 6.02 1.20 6.07
# 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('Carozza maximum FB velocity:', max_fb_velocity)
## Carozza maximum FB velocity: 91.25
# Prepare data for plotting pitch locations
pitch_location_data <- Carozza_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 Carozza',
       x = 'Horizontal Location (feet)',
       y = 'Vertical Location (feet)',
       color = 'Swing/Take',
       shape = 'Chase') +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each 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')
  )

# 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 Carozza',
       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')
  )