# Filter the data for the pitcher Mulligan
Mulligan_data <- Adams619 %>%
  filter(Pitcher == 'Mulligan, Sean')

# Create a detailed table for each pitch
detailed_pitch_table <- Mulligan_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 Mulligan')
Detailed Pitch Table for Mulligan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 88.88 2117.03 227.96 16.28 15.79 FoulBallNotFieldable 6.23 1.96 5.96 7.6
Curveball 72.37 2457.39 49.24 -13.90 -10.14 BallCalled 6.21 1.78 5.50 1.6
Curveball 70.87 2403.18 52.33 -15.59 -10.16 StrikeCalled 6.19 1.71 5.74 1.7
Curveball 69.80 2484.33 37.86 -14.20 -16.15 BallCalled 6.26 1.87 5.59 1.3
Sinker 88.63 1916.84 225.70 15.79 16.54 InPlay 6.35 1.97 5.97 7.5
Changeup 78.68 1411.22 206.31 6.98 15.54 BallCalled 6.03 2.18 5.94 6.9
Sinker 87.44 2027.75 236.73 18.95 13.60 InPlay 6.24 2.02 5.95 7.9
Changeup 85.73 1912.12 225.06 14.47 15.67 FoulBallNotFieldable 6.21 1.90 5.94 7.5
Curveball 69.17 2378.60 60.27 -17.03 -7.75 BallCalled 6.23 1.68 5.43 2.0
Sinker 88.26 2133.23 230.52 14.66 13.24 BallCalled 6.29 1.83 5.88 7.7
Sinker 87.42 2147.79 223.78 15.43 17.29 BallCalled 6.26 1.76 6.04 7.5
Sinker 87.33 2185.75 234.22 17.03 13.47 FoulBallNotFieldable 6.27 2.03 5.79 7.8
Sinker 86.95 2071.91 231.87 18.05 15.41 InPlay 6.25 1.99 5.84 7.7
Sinker 86.17 2046.67 230.94 17.81 15.67 BallCalled 6.16 2.11 5.92 7.7
Changeup 85.59 2055.69 242.10 18.57 11.10 StrikeCalled 6.05 2.13 6.25 8.1
Curveball 71.18 2504.35 56.69 -18.21 -10.12 BallCalled 6.16 1.99 5.42 1.9
Sinker 87.48 2168.13 225.99 11.33 12.05 BallCalled 6.11 1.86 6.32 7.5
Sinker 87.32 2106.76 221.57 14.93 18.01 BallCalled 6.15 2.03 6.32 7.4
Changeup 85.90 1972.70 240.84 17.56 11.10 BallCalled 6.20 1.97 6.06 8.0
Sinker 86.48 2064.18 224.90 15.61 16.94 StrikeCalled 6.28 2.02 5.93 7.5
Curveball 72.20 2556.69 44.29 -15.06 -13.60 BallCalled 6.17 1.92 5.07 1.5
Sinker 87.83 2204.90 225.85 16.37 17.13 InPlay 6.29 1.79 5.91 7.5
# 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: 22
# Display the summary table
knitr::kable(pitch_summary, caption = 'Summary Pitch Table for Mulligan')
Summary Pitch Table for Mulligan
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 18.18% 4 2 2 50.00% 50.00% 83.98 1837.93 13.35 14.39 228.58 7.6 6.12 2.04 6.05
Curveball 27.27% 6 5 1 83.33% 16.67% 70.93 2464.09 -11.32 -15.67 50.11 1.7 6.20 1.82 5.46
Sinker 54.55% 12 5 7 41.67% 58.33% 87.52 2099.24 15.43 16.02 228.34 7.6 6.24 1.95 5.99
# 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('Mulligan maximum FB velocity:', max_fb_velocity)
## Mulligan maximum FB velocity: 88.88
# Prepare data for plotting pitch locations
pitch_location_data <- Mulligan_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 Mulligan',
       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 Mulligan',
       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')
  )