# Filter the data for the pitcher Foley
Foley_data <- Keene616 %>%
  filter(Pitcher == "Foley, Brian")

# Create a detailed table for each pitch
detailed_pitch_table <- Foley_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 Foley")
Detailed Pitch Table for Foley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 92.22 2656.97 206.79 5.42 11.74 FoulBallNotFieldable 5.90 1.69 6.49 6.9
Sinker 92.83 2450.67 249.70 18.07 7.83 StrikeCalled 5.88 1.68 6.57 8.3
Four-Seam 94.51 2579.34 198.94 6.00 18.50 BallCalled 6.07 1.41 6.41 6.6
Four-Seam 92.89 2609.97 185.83 1.51 15.90 BallCalled 5.92 1.66 6.41 6.2
Four-Seam 91.59 2558.44 216.46 8.18 12.21 BallCalled 5.83 1.65 6.24 7.2
Four-Seam 94.42 2670.03 192.13 3.56 17.61 StrikeSwinging 6.02 1.38 6.35 6.4
Four-Seam 93.28 2572.62 215.28 8.12 12.46 BallCalled 6.04 1.44 6.33 7.2
Four-Seam 92.57 2652.01 202.69 7.10 18.04 FoulBallNotFieldable 5.96 1.55 6.37 6.8
Sinker 92.92 2316.16 237.49 17.86 12.53 StrikeCalled 5.97 1.46 6.24 7.9
Slider 80.66 2767.98 82.43 -17.70 -0.94 StrikeCalled 5.81 1.92 5.75 2.7
Four-Seam 93.42 2619.61 187.99 2.43 18.33 BallCalled 6.08 1.53 6.42 6.3
Four-Seam 92.09 2581.01 197.58 3.89 13.30 BallCalled 5.96 1.61 6.38 6.6
Four-Seam 91.77 2582.28 222.33 9.90 11.88 FoulBallNotFieldable 5.88 1.49 6.65 7.4
Sinker 92.53 2458.13 247.08 17.22 8.37 InPlay 5.83 1.60 6.12 8.2
Sinker 90.59 2418.60 236.08 15.65 11.55 FoulBallNotFieldable 5.74 1.90 6.24 7.9
Four-Seam 92.97 2540.03 215.57 11.53 17.21 BallCalled 6.09 1.58 6.24 7.2
Four-Seam 93.02 2465.42 228.19 14.36 13.89 StrikeCalled 5.84 1.56 6.27 7.6
Slider 77.70 2713.79 81.56 -22.57 -1.77 BallCalled 5.62 1.93 5.89 2.7
Sinker 93.46 2539.73 240.44 14.75 9.39 StrikeSwinging 5.84 1.71 6.20 8.0
Four-Seam 94.12 2573.47 227.82 14.58 14.26 InPlay 5.81 1.74 6.27 7.6
# 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:  20 \n\n
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Foley")
Summary Pitch Table for Foley
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Four-Seam 65.00% 13 7 6 53.85% 46.15% 92.99 2589.32 15.03 7.43 207.51 6.9 5.95 1.56 6.37
Sinker 25.00% 5 0 5 0.00% 100.00% 92.47 2436.66 9.93 16.71 242.16 8.1 5.85 1.67 6.27
Slider 10.00% 2 1 1 50.00% 50.00% 79.18 2740.89 -1.35 -20.13 82.00 2.7 5.72 1.92 5.82
# 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('Foley maximum FB velocity: ', max_fb_velocity, 'mph\\n')
## Foley maximum FB velocity:  94.51 mph\n
# Prepare data for plotting pitch locations
pitch_location_data <- Foley_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 Foley',
       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 Foley',
       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')
  )