# Filter the data for the pitcher Eshleman
Eshleman_data <- Keene616 %>%
  filter(Pitcher == "Eshleman, Jack")

# Create a detailed table for each pitch
detailed_pitch_table <- Eshleman_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 Eshleman")
Detailed Pitch Table for Eshleman
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 91.08 2333.34 191.40 4.93 25.65 StrikeSwinging 6.10 1.46 5.52 6.4
Four-Seam 92.03 2363.44 192.18 5.39 26.22 StrikeCalled 6.05 1.28 5.54 6.4
Changeup 84.75 2115.39 229.76 16.38 15.18 BallCalled 5.65 1.66 5.69 7.7
Four-Seam 91.86 2220.72 187.82 3.14 24.16 InPlay 6.09 1.25 5.58 6.3
Four-Seam 90.30 2309.56 202.65 8.76 22.31 BallCalled 6.15 1.48 5.59 6.8
Slider 86.10 2476.07 151.02 -4.80 9.95 BallCalled 5.87 1.55 5.42 5.0
Four-Seam 90.25 2355.06 203.47 9.03 22.15 StrikeCalled 6.10 1.41 5.53 6.8
Four-Seam 90.33 2315.55 204.82 8.46 19.63 BallCalled 6.10 1.53 5.49 6.8
Four-Seam 90.14 2288.31 205.90 8.95 19.82 InPlay 6.09 1.52 5.28 6.9
Curveball 81.59 2544.55 44.04 -6.83 -5.41 StrikeCalled 6.20 1.22 5.20 1.5
Cutter 87.22 2512.38 169.48 -2.36 13.96 FoulBallNotFieldable 5.90 1.67 5.45 5.6
Slider 83.78 2503.44 72.55 -6.28 -0.95 BallCalled 6.08 1.27 5.32 2.4
Slider 84.59 2532.64 73.72 -6.16 -0.54 FoulBallNotFieldable 5.72 1.81 5.41 2.5
Four-Seam 91.47 2218.28 195.79 6.57 24.35 BallCalled 6.22 1.18 5.66 6.5
Curveball 82.01 2489.87 11.45 -1.84 -7.80 BallCalled 6.05 1.23 5.41 0.4
Four-Seam 91.19 2173.41 190.21 3.84 22.46 FoulBallNotFieldable 6.19 1.13 5.59 6.3
Slider 82.53 2559.05 39.73 -3.97 -3.39 InPlay 6.08 0.94 5.17 1.3
Four-Seam 90.52 2245.68 187.53 3.05 24.34 StrikeSwinging 6.24 1.11 5.24 6.3
Four-Seam 91.07 2273.54 180.57 0.22 23.40 FoulBallNotFieldable 6.19 1.17 5.45 6.0
Four-Seam 91.59 2274.13 197.37 7.26 24.41 InPlay 6.27 1.17 5.35 6.6
Four-Seam 89.92 2216.56 192.42 5.08 24.38 BallCalled 6.11 1.42 5.43 6.4
Four-Seam 90.84 2133.00 200.69 7.85 22.09 StrikeCalled 6.00 1.51 5.39 6.7
Changeup 83.41 2021.36 240.20 17.52 11.46 BallCalled 5.66 1.84 5.56 8.0
Four-Seam 91.41 2213.22 210.02 10.94 20.18 BallCalled 6.12 1.46 5.53 7.0
Four-Seam 90.20 2176.12 204.05 8.63 20.62 FoulBallNotFieldable 6.12 1.16 5.46 6.8
Four-Seam 90.09 2175.41 201.91 7.27 19.30 FoulBallNotFieldable 6.14 1.10 5.41 6.7
Four-Seam 90.83 2163.98 191.23 4.48 23.80 FoulBallNotFieldable 6.15 1.21 5.51 6.4
Four-Seam 90.26 2089.86 191.23 3.94 21.18 InPlay 6.11 1.38 5.35 6.4
Four-Seam 90.18 2261.14 190.71 4.33 24.34 BallCalled 6.12 1.33 5.32 6.4
Four-Seam 90.78 2199.16 189.43 3.72 23.86 FoulBallNotFieldable 6.19 1.33 5.28 6.3
Four-Seam 90.91 2087.95 200.84 7.13 20.15 StrikeCalled 6.09 1.33 5.37 6.7
Four-Seam 90.89 2092.29 199.77 6.88 20.53 BallCalled 6.18 1.23 5.40 6.7
Four-Seam 91.05 2203.12 191.49 4.67 24.36 FoulBallNotFieldable 6.18 1.31 5.41 6.4
Four-Seam 90.32 2186.88 198.88 7.30 22.59 FoulBallNotFieldable 6.14 1.54 5.36 6.6
Four-Seam 90.00 2195.60 199.91 8.19 23.93 StrikeSwinging 6.14 1.42 5.37 6.7
Four-Seam 90.29 2181.36 199.56 7.43 22.33 StrikeCalled 6.18 1.22 5.44 6.7
Four-Seam 90.85 2243.62 194.05 5.68 24.07 StrikeCalled 6.07 1.32 5.39 6.5
Four-Seam 91.20 2268.38 192.24 5.17 25.22 BallCalled 6.19 1.14 5.40 6.4
Changeup 83.88 2006.08 223.77 13.77 15.83 BallCalled 5.80 1.58 5.76 7.5
Four-Seam 90.88 2203.21 200.62 7.83 22.20 FoulBallNotFieldable 6.21 1.10 5.43 6.7
Four-Seam 91.75 2177.61 197.52 7.06 23.54 FoulBallNotFieldable 6.08 1.21 5.75 6.6
Four-Seam 91.55 2322.72 196.14 6.44 23.41 FoulBallNotFieldable 6.10 1.27 5.53 6.5
Slider 86.66 2362.62 178.83 -0.16 9.19 BallCalled 5.91 1.57 5.58 6.0
Four-Seam 91.06 2256.70 192.50 5.33 25.30 BallCalled 6.07 1.23 5.52 6.4
Four-Seam 89.21 2172.38 190.63 4.45 24.95 StrikeCalled 6.20 1.12 5.59 6.4
Four-Seam 90.89 2299.13 201.07 8.24 22.68 BallCalled 6.20 1.21 5.61 6.7
Four-Seam 89.30 2154.61 189.29 3.73 24.05 StrikeCalled 6.21 1.27 5.73 6.3
Curveball 81.00 2578.15 21.74 -3.71 -8.00 BallCalled 6.20 0.95 5.22 0.7
Four-Seam 90.83 2238.58 193.76 5.70 24.53 HitByPitch 6.23 1.15 5.39 6.5
Four-Seam 88.84 2016.57 202.79 8.35 21.19 BallCalled 6.23 1.27 5.74 6.8
Four-Seam 89.43 2116.64 194.25 5.41 22.59 StrikeCalled 6.15 1.30 5.63 6.5
Slider 84.49 2379.13 172.13 -1.40 11.47 StrikeCalled 6.12 1.54 5.58 5.7
Four-Seam 90.05 2239.61 198.05 6.88 22.44 FoulBallNotFieldable 6.16 1.12 5.58 6.6
Four-Seam 91.07 2198.56 194.61 6.01 24.28 StrikeSwinging 6.23 1.11 5.65 6.5
Four-Seam 90.41 2074.00 199.50 7.21 21.65 FoulBallNotFieldable 6.20 1.33 5.39 6.7
Four-Seam 90.44 2166.15 205.26 8.56 19.45 InPlay 6.05 1.37 5.45 6.8
Four-Seam 89.42 2125.41 201.60 8.28 22.12 StrikeCalled 6.21 1.22 5.68 6.7
Four-Seam 91.60 2067.11 206.08 8.55 18.63 StrikeSwinging 6.10 1.37 5.48 6.9
Four-Seam 92.40 2217.59 191.20 4.42 23.48 FoulBallNotFieldable 6.17 1.23 5.65 6.4
Four-Seam 91.75 2231.53 212.23 11.42 19.40 InPlay 6.12 1.23 5.67 7.1
Four-Seam 90.90 2172.65 193.78 5.78 24.82 InPlay 6.24 1.11 5.42 6.5
Four-Seam 90.21 2161.55 195.20 5.53 21.61 InPlay 6.23 1.24 5.49 6.5
Four-Seam 91.49 2298.25 204.19 8.84 20.92 StrikeCalled 6.00 1.31 5.69 6.8
Four-Seam 88.29 2450.71 199.12 4.82 15.14 InPlay 6.00 1.40 5.66 6.6
Four-Seam 90.56 2242.31 212.41 11.96 20.09 StrikeCalled 6.09 1.36 5.45 7.1
Four-Seam 90.69 2255.08 213.83 11.41 18.26 FoulBallNotFieldable 6.05 1.31 5.51 7.1
Four-Seam 90.77 2198.22 199.97 7.55 22.05 BallCalled 6.19 1.28 5.54 6.7
Slider 82.28 2462.44 45.02 -2.83 -1.51 StrikeCalled 6.14 1.22 5.35 1.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:  68 \n\n
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Eshleman")
Summary Pitch Table for Eshleman
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 4.41% 3 3 0 100.00% 0.00% 84.01 2047.61 14.16 15.89 231.24 7.7 5.70 1.69 5.67
Curveball 4.41% 3 2 1 66.67% 33.33% 81.53 2537.52 -7.07 -4.13 25.74 0.9 6.15 1.13 5.28
Cutter 1.47% 1 0 1 0.00% 100.00% 87.22 2512.38 13.96 -2.36 169.48 5.6 5.90 1.67 5.45
Four-Seam 79.41% 54 12 42 22.22% 77.78% 90.66 2213.25 22.42 6.63 197.66 6.6 6.14 1.28 5.50
Slider 10.29% 7 3 4 42.86% 57.14% 84.35 2467.91 3.46 -3.66 104.71 3.5 5.99 1.41 5.40
# 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('Eshleman maximum FB velocity: ', max_fb_velocity)
## Eshleman maximum FB velocity:  92.4
# Prepare data for plotting pitch locations
pitch_location_data <- Eshleman_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 Eshleman',
       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 Eshleman',
       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')
  )