# Filter the data for the pitcher Martinez
Martinez_data <- Adams619 %>%
  filter(Pitcher == 'Martinez, Matthew')

# Create a detailed table for each pitch
detailed_pitch_table <- Martinez_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 Martinez')
Detailed Pitch Table for Martinez
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 89.00 1705.01 225.03 10.73 11.84 BallCalled 6.42 1.21 4.11 7.5
Changeup 78.05 1797.40 261.56 14.80 3.73 BallCalled 6.15 1.67 3.83 8.7
Changeup 78.33 1782.10 257.37 15.72 5.08 StrikeCalled 6.13 1.80 4.00 8.6
Curveball 77.53 2615.24 62.65 -14.19 -5.84 StrikeCalled 6.29 1.59 3.70 2.1
Sinker 89.91 1863.03 228.93 19.48 18.19 BallCalled 6.46 1.04 4.09 7.6
Changeup 80.23 1802.23 252.10 15.62 6.53 InPlay 6.14 1.72 4.14 8.4
Curveball 75.51 2447.47 45.18 -7.94 -6.29 BallCalled 6.27 1.66 3.73 1.5
Curveball 76.87 2509.85 25.38 -4.78 -8.52 BallCalled 6.29 1.59 3.89 0.8
Sinker 89.47 2037.57 225.69 14.25 15.15 StrikeCalled 6.39 1.21 4.29 7.5
Changeup 80.23 1689.52 256.39 15.41 5.19 StrikeSwinging 6.14 1.72 4.08 8.5
Four-Seam 89.36 1972.40 211.89 9.55 16.54 InPlay 6.46 0.95 4.18 7.1
Changeup 79.43 1614.67 264.54 16.75 3.11 StrikeCalled 6.18 1.56 3.93 8.8
Curveball 78.28 2578.41 45.78 -11.36 -9.47 StrikeCalled 6.24 1.50 3.89 1.5
Curveball 79.90 2529.69 35.21 -7.13 -8.64 BallCalled 6.28 1.51 4.05 1.2
Sinker 90.43 2069.82 230.34 16.05 14.58 FoulBallNotFieldable 6.35 0.94 4.02 7.7
Curveball 77.91 2581.58 45.21 -9.77 -8.13 InPlay 6.17 1.42 4.04 1.5
Changeup 82.06 1632.67 262.28 13.37 3.13 StrikeSwinging 6.15 1.53 4.80 8.7
Changeup 81.23 1568.83 250.59 15.66 6.98 InPlay 6.13 1.47 4.53 8.4
# 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: 18
# Display the summary table
knitr::kable(pitch_summary, caption = 'Summary Pitch Table for Martinez')
Summary Pitch Table for Martinez
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 44.44% 8 2 6 25.00% 75.00% 81.07 1699.05 5.70 14.76 253.73 8.4 6.18 1.58 4.18
Curveball 33.33% 6 3 3 50.00% 50.00% 77.67 2543.71 -7.82 -9.20 43.24 1.4 6.26 1.54 3.88
Four-Seam 5.56% 1 0 1 0.00% 100.00% 89.36 1972.40 16.54 9.55 211.89 7.1 6.46 0.95 4.18
Sinker 16.67% 3 1 2 33.33% 66.67% 89.94 1990.14 15.97 16.59 228.32 7.6 6.40 1.06 4.13
# 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('Martinez maximum FB velocity:', max_fb_velocity)
## Martinez maximum FB velocity: 90.43
# Prepare data for plotting pitch locations
pitch_location_data <- Martinez_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 Martinez',
       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 Martinez',
       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')
  )