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

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_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 Jack Eshleman")
Detailed Pitch Table for Jack Eshleman
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 89.82 2241.02 192.61 4.64 21.99 BallCalled 6.08 1.28 6.00 6.4
Four-Seam 89.39 2263.31 189.71 3.33 20.77 BallCalled 5.94 1.19 6.00 6.3
Four-Seam 88.41 2220.55 188.99 3.55 23.68 StrikeSwinging 5.99 1.15 5.85 6.3
Four-Seam 90.15 2343.43 202.96 8.39 21.02 BallCalled 6.00 1.27 5.45 6.8
Four-Seam 88.61 2199.44 198.65 6.90 21.74 FoulBallNotFieldable 6.01 1.12 5.78 6.6
Four-Seam 89.73 2212.50 187.61 3.24 25.60 BallCalled 6.09 1.20 5.58 6.3
Four-Seam 89.11 2265.98 193.04 4.84 22.16 BallCalled 6.00 1.35 5.83 6.4
Four-Seam 88.40 2273.17 190.68 4.19 23.54 InPlay 6.02 1.15 5.83 6.4
Slider 81.60 2337.80 161.24 -2.21 7.88 FoulBallNotFieldable 6.01 1.42 5.52 5.4
Slider 81.75 2458.93 138.59 -9.24 11.91 BallCalled 5.93 1.45 5.71 4.6
Four-Seam 90.48 2307.39 196.15 6.31 23.17 StrikeSwinging 6.09 1.36 5.40 6.5
Slider 83.71 2425.18 153.99 -4.94 11.59 StrikeSwinging 5.94 1.39 5.50 5.1
Four-Seam 90.94 2260.33 194.37 5.56 23.05 BallCalled 6.07 1.23 5.44 6.5
Four-Seam 88.51 2295.92 202.48 8.69 22.37 BallCalled 6.01 1.22 5.73 6.7
Four-Seam 89.60 2333.25 208.71 10.82 21.05 BallCalled 6.08 1.30 5.64 7.0
Four-Seam 88.94 2285.73 193.04 4.67 21.48 StrikeCalled 6.09 1.14 5.78 6.4
Four-Seam 88.82 2260.44 205.21 9.17 20.80 FoulBallNotFieldable 6.00 1.27 5.80 6.8
Changeup 82.52 1748.11 216.48 9.56 14.30 BallCalled 5.64 1.61 5.93 7.2
Four-Seam 90.16 2338.29 187.01 2.62 22.48 StrikeCalled 6.12 1.06 5.63 6.2
Slider 84.21 2429.32 134.47 -5.22 6.19 FoulBallNotFieldable 5.89 1.23 5.69 4.5
Slider 84.46 2364.80 164.01 -1.80 7.55 InPlay 5.96 1.24 5.73 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 %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # 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")
## Total number of pitches thrown:  21
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Jack Eshleman")
Summary Pitch Table for Jack Eshleman
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 4.76% 1 1 0 100.00% 0.00% 82.52 1748.11 14.30 9.56 216.48 7.2 5.64 1.61 5.93
Four-Seam 71.43% 15 8 7 53.33% 46.67% 89.40 2273.38 22.33 5.79 195.41 6.5 6.04 1.22 5.72
Slider 23.81% 5 1 4 20.00% 80.00% 83.15 2403.21 9.02 -4.68 150.46 5.0 5.95 1.35 5.63
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Jack Eshleman maximum FB velocity: ", max_fb_velocity, "mph\n")
## Jack Eshleman maximum FB velocity:  90.94 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_data %>%
  select(AutoPitchType, PlateLocHeight, PlateLocSide, PitchCall) %>%
  rename(
    PitchHeight = PlateLocHeight,
    PitchSide = PlateLocSide
  ) %>%
  mutate(
    SwingTake = ifelse(PitchCall %in% c("StrikeSwinging", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | 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) +
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -0.85, xmax = 0.85, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) +
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) +
  labs(title = "Pitch Locations for Jack Eshleman",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) +
  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
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 26)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement"), 
       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")
  )