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

# Create a detailed table for each pitch
detailed_pitch_table <- Josh_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 Josh Keevan")
Detailed Pitch Table for Josh Keevan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 74.91 1669.79 109.40 -16.54 7.32 StrikeSwinging 5.22 -0.95 6.58 3.6
Changeup 75.67 1721.38 123.50 -15.31 11.68 FoulBallNotFieldable 5.21 -0.97 6.42 4.1
Splitter 82.41 2158.80 141.93 -15.68 21.52 BallCalled 5.77 -0.23 5.94 4.7
Changeup 76.08 1666.39 106.82 -14.82 5.92 InPlay 5.19 -0.71 6.67 3.6
Changeup 76.79 1706.65 95.17 -15.49 2.79 StrikeCalled 5.19 -0.96 6.75 3.2
Splitter 83.92 2185.12 138.95 -15.53 19.25 StrikeSwinging 5.69 -0.37 5.89 4.6
Changeup 83.27 2134.38 151.74 -10.36 20.73 HitByPitch 5.78 -0.28 6.06 5.1
Changeup 77.02 1802.31 114.37 -17.93 9.77 BallCalled 5.17 -0.99 6.42 3.8
Changeup 77.23 1770.43 111.79 -15.05 7.64 StrikeSwinging 5.17 -0.83 6.56 3.7
Splitter 84.23 2287.54 135.59 -15.74 17.60 StrikeSwinging 5.76 -0.58 5.98 4.5
Changeup 82.62 2322.16 131.91 -16.78 16.67 BallCalled 5.69 -0.37 5.83 4.4
Changeup 76.16 1698.89 104.81 -19.24 6.73 BallCalled 5.12 -0.79 6.48 3.5
Changeup 79.36 2070.30 137.58 -16.36 19.64 StrikeCalled 5.56 -0.57 6.14 4.6
Curveball 70.35 2278.77 301.33 8.91 -3.45 BallCalled 5.20 -0.81 5.33 10.0
Splitter 85.11 2312.87 137.39 -17.00 20.00 StrikeCalled 5.55 -0.48 5.88 4.6
Changeup 76.85 1731.87 112.68 -16.95 8.62 BallCalled 5.17 -0.61 6.81 3.8
Changeup 82.45 2233.73 126.17 -18.60 15.20 FoulBallNotFieldable 5.52 -0.48 6.10 4.2
Changeup 83.36 2356.99 126.39 -21.46 17.46 BallCalled 5.66 -0.53 6.00 4.2
Changeup 83.43 2341.89 142.61 -15.86 22.39 FoulBallNotFieldable 5.65 -0.47 6.00 4.8
Changeup 81.02 2181.82 142.25 -13.86 19.51 InPlay 5.54 -0.44 5.91 4.7
Changeup 81.54 2276.12 136.10 -16.75 18.92 BallCalled 5.68 -0.53 5.94 4.5
Splitter 82.98 2288.49 133.69 -18.02 18.67 StrikeCalled 5.63 -0.59 6.01 4.5
Four-Seam 84.21 2276.03 147.07 -12.41 20.63 StrikeSwinging 5.66 -0.43 5.82 4.9
Changeup 78.81 2205.56 131.90 -18.35 18.03 BallCalled 5.78 -0.23 6.41 4.4
Changeup 76.14 1710.50 116.21 -16.00 9.34 FoulBallNotFieldable 5.16 -0.79 6.54 3.9
Splitter 83.42 2275.73 135.06 -17.72 19.24 StrikeSwinging 5.68 -0.58 6.22 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")
## Total number of pitches thrown:  26
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Josh Keevan")
Summary Pitch Table for Josh Keevan
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 69.23% 18 7 11 38.89% 61.11% 79.04 1977.84 13.24 -16.43 123.41 4.1 5.41 -0.64 6.31
Curveball 3.85% 1 1 0 100.00% 0.00% 70.35 2278.77 -3.45 8.91 301.33 10.0 5.20 -0.81 5.33
Four-Seam 3.85% 1 0 1 0.00% 100.00% 84.21 2276.03 20.63 -12.41 147.07 4.9 5.66 -0.43 5.82
Splitter 23.08% 6 1 5 16.67% 83.33% 83.68 2251.43 19.38 -16.61 137.10 4.6 5.68 -0.47 5.99
# 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("Josh Keevan maximum FB velocity: ", max_fb_velocity, "mph\n")
## Josh Keevan maximum FB velocity:  84.21 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Josh_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) +
  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.75, xmax = 0.75, 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 Josh Keevan",
       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
  labs(title = "Pitch Movement for Josh 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")
  )