# Filter the data for the pitcher Keevan
Keevan_data <- Keene616 %>%
  filter(Pitcher == "Keevan, Josh")

# Create a detailed table for each pitch
detailed_pitch_table <- Keevan_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 Keevan")
Detailed Pitch Table for Keevan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 85.09 2172.11 124.96 -18.53 14.45 StrikeCalled 5.85 -0.59 5.68 4.2
Changeup 76.80 1665.57 102.58 -17.00 5.38 StrikeCalled 5.22 -0.87 6.32 3.4
Changeup 77.60 1867.61 85.41 -27.65 -0.51 BallCalled 4.61 -1.62 5.81 2.8
Changeup 85.99 2242.63 126.34 -19.23 15.61 BallCalled 5.66 -0.49 5.79 4.2
Changeup 77.45 1604.51 98.75 -17.29 4.21 InPlay 5.08 -0.85 6.39 3.3
Changeup 84.29 2161.30 132.88 -19.20 19.21 StrikeCalled 5.63 -0.58 5.82 4.4
Curveball 74.26 2165.84 199.91 0.38 2.87 BallCalled 5.24 -0.92 4.95 6.7
Sinker 86.00 2217.12 134.41 -17.36 18.44 BallCalled 5.87 -0.25 5.87 4.5
Changeup 84.84 2145.37 126.84 -19.31 15.95 BallCalled 5.59 -0.51 5.97 4.2
Changeup 80.76 2102.23 129.42 -16.55 15.17 FoulBallNotFieldable 5.47 -0.55 5.89 4.3
Changeup 84.04 2076.15 133.52 -18.01 18.53 BallCalled 5.80 -0.48 5.87 4.5
Changeup 75.78 1518.44 111.29 -14.31 7.12 BallCalled 5.21 -0.87 5.88 3.7
Changeup 76.50 1364.43 82.62 -12.00 -0.17 StrikeCalled 5.06 -0.92 6.28 2.8
Changeup 82.07 2090.67 131.44 -18.94 18.16 StrikeSwinging 5.51 -0.60 5.93 4.4
Changeup 73.88 1510.72 102.93 -17.47 5.68 FoulBallNotFieldable 5.50 -0.77 5.82 3.4
Changeup 84.99 2221.36 130.63 -14.85 14.23 StrikeSwinging 5.65 -0.31 5.74 4.4
Splitter 85.00 2247.18 133.13 -18.01 18.33 BallCalled 5.73 -0.35 5.65 4.4
Changeup 76.98 1628.99 107.35 -18.72 7.43 BallCalled 5.17 -0.94 6.28 3.6
Splitter 84.89 2348.11 135.45 -18.75 20.50 StrikeSwinging 5.62 -0.55 5.84 4.5
Changeup 85.27 2395.38 132.05 -17.53 17.27 BallCalled 5.66 -0.44 5.77 4.4
Changeup 79.81 2268.07 121.54 -21.05 14.55 BallCalled 5.37 -0.40 5.84 4.1
Changeup 83.24 2223.97 134.23 -15.73 16.83 StrikeSwinging 5.62 -0.62 5.71 4.5
Four-Seam 85.39 2384.15 140.73 -15.08 19.89 BallCalled 5.71 -0.49 5.81 4.7
Changeup 86.05 2327.71 127.38 -18.83 15.85 StrikeSwinging 5.68 -0.57 5.80 4.2
Splitter 84.23 2200.59 135.60 -17.57 19.40 BallCalled 5.67 -0.69 5.94 4.5
Splitter 84.48 2228.92 139.10 -16.52 20.47 BallCalled 5.84 -0.26 5.79 4.6
Splitter 83.44 2287.49 133.58 -17.78 18.40 InPlay 5.73 -0.32 5.81 4.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, '\\n\\n')
## Total number of pitches thrown:  27 \n\n
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Keevan")
Summary Pitch Table for Keevan
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 70.37% 19 8 11 42.11% 57.89% 81.13 1978.27 11.84 -18.01 118.01 3.9 5.44 -0.68 5.93
Curveball 3.70% 1 1 0 100.00% 0.00% 74.26 2165.84 2.87 0.38 199.91 6.7 5.24 -0.92 4.95
Four-Seam 3.70% 1 1 0 100.00% 0.00% 85.39 2384.15 19.89 -15.08 140.73 4.7 5.71 -0.49 5.81
Sinker 3.70% 1 1 0 100.00% 0.00% 86.00 2217.12 18.44 -17.36 134.41 4.5 5.87 -0.25 5.87
Splitter 18.52% 5 3 2 60.00% 40.00% 84.41 2262.46 19.42 -17.73 135.37 4.5 5.72 -0.43 5.81
# 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('Keevan maximum FB velocity: ', max_fb_velocity, 'mph\\n')
## Keevan maximum FB velocity:  86 mph\n
# Prepare data for plotting pitch locations
pitch_location_data <- Keevan_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 Keevan',
       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 Keevan',
       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')
  )