# Filter the data for the pitcher Eshleman
Eshleman_data <- Bristol610 %>%
  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 89.00 2279.54 191.35 4.09 21.68 FoulBallNotFieldable 6.04 1.09 6.25 6.4
Slider 85.48 2461.41 150.04 -5.26 10.29 BallCalled 5.81 1.27 6.26 5.0
Four-Seam 90.07 2302.30 192.60 4.88 23.08 BallCalled 5.97 1.15 6.47 6.4
Four-Seam 90.67 2296.08 196.04 6.57 24.13 StrikeCalled 5.96 1.27 6.07 6.5
Four-Seam 91.12 2376.14 204.93 9.23 21.09 InPlay 6.00 1.08 6.13 6.8
Four-Seam 91.06 2297.51 197.41 6.77 22.78 StrikeCalled 5.97 1.03 6.16 6.6
Cutter 85.43 2473.89 182.82 0.56 12.68 InPlay 5.85 1.34 6.17 6.1
Four-Seam 89.11 2188.80 208.81 9.59 18.63 BallCalled 5.90 1.24 6.32 7.0
Four-Seam 89.83 2309.61 204.02 8.14 19.45 StrikeSwinging 5.94 1.25 6.12 6.8
Four-Seam 90.25 2292.93 200.35 7.39 21.19 StrikeCalled 5.94 1.36 5.93 6.7
Four-Seam 90.71 2330.67 202.73 8.59 21.82 FoulBallNotFieldable 5.97 1.45 5.99 6.8
Four-Seam 89.97 2178.77 189.29 3.32 21.58 InPlay 5.95 1.15 6.26 6.3
Four-Seam 90.10 2137.60 189.67 3.70 22.95 StrikeCalled 6.05 0.96 6.07 6.3
Four-Seam 90.63 2199.39 188.54 3.01 21.32 BallCalled 6.05 1.22 6.14 6.3
Curveball 80.73 2499.85 356.62 0.45 -6.31 InPlay 6.00 1.12 5.92 11.9
Four-Seam 90.53 2261.31 191.37 4.20 22.05 StrikeSwinging 6.02 0.98 6.16 6.4
Four-Seam 90.30 2235.97 193.06 4.95 22.47 BallCalled 6.06 1.13 6.37 6.4
Four-Seam 91.06 2203.48 192.10 4.23 20.84 FoulBallNotFieldable 5.98 1.02 6.32 6.4
Changeup 85.19 2080.35 225.70 13.68 14.61 StrikeSwinging 5.61 1.42 6.43 7.5
Four-Seam 89.76 2082.94 195.33 5.73 22.12 StrikeCalled 5.99 1.18 6.33 6.5
Four-Seam 91.01 2296.73 202.85 8.96 22.47 StrikeSwinging 5.81 1.30 6.28 6.8
Slider 82.04 2498.55 14.66 -1.32 -3.87 BallCalled 5.92 0.90 6.08 0.5
Four-Seam 90.67 2273.32 199.98 6.37 18.62 BallCalled 5.98 1.01 6.33 6.7
Slider 82.77 2486.35 13.46 -1.26 -4.11 InPlay 5.95 1.03 6.18 0.4
Changeup 83.19 1943.58 228.44 14.93 14.64 BallCalled 5.58 1.59 6.48 7.6
Slider 83.17 2406.74 181.96 0.33 10.94 StrikeCalled 5.95 1.27 6.20 6.1
Slider 83.62 2349.73 190.78 2.15 12.55 BallCalled 5.92 1.28 6.19 6.4
Four-Seam 89.35 2264.29 191.74 4.54 23.12 BallCalled 5.94 1.25 6.35 6.4
Four-Seam 89.35 2253.56 195.72 5.78 21.80 FoulBallNotFieldable 6.02 1.20 6.21 6.5
Four-Seam 89.42 2218.41 191.45 4.02 21.08 FoulBallNotFieldable 5.95 1.30 6.20 6.4
Four-Seam 89.53 2160.12 201.58 8.03 21.42 FoulBallNotFieldable 5.87 1.17 6.42 6.7
Slider 83.33 2340.81 145.90 -2.18 4.33 StrikeCalled 5.86 1.15 6.24 4.9
Four-Seam 89.16 2115.16 194.01 5.03 21.24 BallCalled 5.97 1.05 6.28 6.5
Four-Seam 89.46 2228.25 200.31 7.07 20.30 BallCalled 5.99 1.23 6.08 6.7
Four-Seam 88.09 2077.62 192.01 4.02 20.07 FoulBallNotFieldable 5.98 1.10 6.34 6.4
Four-Seam 88.94 2128.39 186.22 2.10 20.39 BallCalled 5.92 0.96 6.26 6.2
Four-Seam 89.11 2164.58 186.14 2.19 21.42 InPlay 5.99 1.11 6.24 6.2
Four-Seam 88.78 2172.76 197.81 5.47 18.14 FoulBallNotFieldable 5.87 1.17 6.40 6.6
Four-Seam 89.27 2114.16 194.54 5.32 21.74 BallCalled 5.97 1.07 6.44 6.5
Four-Seam 89.10 2087.12 197.92 5.96 19.56 BallCalled 5.79 1.30 6.51 6.6
Four-Seam 90.06 2230.84 192.48 5.15 24.50 InPlay 5.91 1.26 6.45 6.4
Four-Seam 88.59 2171.37 176.04 -1.54 23.49 BallCalled 6.03 0.97 6.23 5.9
Four-Seam 88.66 2141.29 181.14 0.42 22.00 InPlay 5.93 1.19 6.39 6.0
Four-Seam 90.49 2163.74 178.92 -0.42 23.20 BallCalled 5.99 1.14 6.21 6.0
Four-Seam 90.27 2295.96 195.95 6.18 22.81 StrikeSwinging 5.89 1.17 6.17 6.5
Four-Seam 89.15 2152.48 171.23 -3.22 22.02 BallCalled 6.11 1.13 6.11 5.7
Four-Seam 88.36 2151.28 194.25 5.03 21.05 StrikeSwinging 6.07 1.17 6.34 6.5
Four-Seam 89.53 2180.86 181.86 0.68 22.12 StrikeSwinging 6.08 1.04 6.21 6.1
Four-Seam 89.49 2095.44 196.54 6.14 21.73 StrikeSwinging 5.94 1.23 6.12 6.6
Four-Seam 90.64 2182.32 204.20 8.53 20.17 FoulBallNotFieldable 5.95 1.25 5.94 6.8
Four-Seam 89.85 2253.86 198.72 7.53 23.50 BallCalled 6.03 1.24 5.78 6.6
Four-Seam 91.06 2296.34 191.92 4.23 21.25 FoulBallNotFieldable 5.93 1.24 6.10 6.4
Changeup 84.07 2011.06 240.90 17.65 11.08 BallinDirt 5.42 1.67 6.42 8.0
Four-Seam 89.79 2173.49 198.61 6.54 20.58 FoulBallNotFieldable 5.97 1.15 5.98 6.6
Changeup 82.76 1893.35 238.25 13.62 9.60 InPlay 5.53 1.59 6.47 7.9
Four-Seam 89.13 2156.45 186.50 2.31 21.36 StrikeCalled 6.05 0.90 6.19 6.2
Four-Seam 89.97 2161.66 206.98 9.43 19.67 FoulBallNotFieldable 5.92 1.22 6.09 6.9
Four-Seam 90.26 2235.88 193.46 4.92 21.67 StrikeSwinging 6.01 0.90 6.13 6.4
Four-Seam 89.98 2199.34 193.39 4.82 21.27 BallCalled 6.00 1.03 6.10 6.4
Changeup 83.99 1890.67 234.45 14.48 11.49 InPlay 5.64 1.36 6.67 7.8
Four-Seam 88.71 2090.44 199.79 7.43 21.73 StrikeCalled 6.04 1.10 6.14 6.7
Four-Seam 90.34 2294.12 199.60 7.75 22.89 StrikeCalled 5.97 1.23 5.88 6.7
Four-Seam 90.51 2195.57 204.52 9.03 20.90 FoulBallNotFieldable 5.86 1.32 5.86 6.8
Four-Seam 90.50 2287.63 203.35 9.42 23.07 FoulBallNotFieldable 6.05 1.03 5.90 6.8
Slider 82.67 2481.55 66.16 -11.09 -3.51 StrikeSwinging 5.66 1.29 5.97 2.2
Cutter 84.48 2478.69 184.10 0.99 14.96 FoulBallNotFieldable 6.06 1.20 5.96 6.1
Curveball 82.83 2520.32 341.65 2.21 -5.53 BallCalled 6.02 0.93 5.98 11.4
Curveball 82.43 2418.26 19.12 -1.76 -4.01 BallCalled 6.00 1.05 5.70 0.6
Slider 84.12 2430.50 153.21 -2.96 6.95 BallCalled 5.83 1.32 6.07 5.1
Four-Seam 88.26 2199.92 195.27 5.73 22.14 FoulBallNotFieldable 6.00 1.17 6.14 6.5
Four-Seam 88.92 2119.86 197.90 6.31 20.75 FoulBallNotFieldable 6.03 1.28 6.17 6.6
Slider 85.15 2478.30 163.97 -1.99 8.12 BallCalled 5.86 1.19 6.24 5.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:  72
# 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 6.94% 5 1 4 20.00% 80.00% 83.84 1963.80 12.28 14.87 233.55 7.8 5.56 1.53 6.49
Curveball 4.17% 3 2 1 66.67% 33.33% 82.00 2479.48 -5.28 0.30 239.13 8.0 6.01 1.03 5.87
Cutter 2.78% 2 0 2 0.00% 100.00% 84.96 2476.29 13.82 0.78 183.46 6.1 5.96 1.27 6.06
Four-Seam 73.61% 53 16 37 30.19% 69.81% 89.77 2206.75 21.56 5.31 194.58 6.5 5.97 1.15 6.19
Slider 12.50% 9 5 4 55.56% 44.44% 83.59 2437.10 4.63 -2.62 120.02 4.0 5.86 1.19 6.16
# 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, "mph\n")
## Eshleman maximum FB velocity:  91.12 mph
# 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")
  )