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

# 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 Josh Keevan")
Detailed Pitch Table for Josh Keevan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 75.37 1706.36 110.70 -16.87 8.03 InPlay 5.32 -0.95 6.13 3.7
Changeup 73.70 1677.12 113.47 -12.64 7.03 StrikeCalled 5.29 -0.82 5.89 3.8
Changeup 76.44 1815.43 115.14 -17.61 9.72 InPlay 5.29 -0.92 6.08 3.8
Changeup 76.89 1802.84 109.45 -17.64 7.79 BallCalled 5.22 -0.87 6.18 3.6
Changeup 76.84 1813.51 118.05 -15.18 9.71 BallCalled 5.30 -0.87 5.82 3.9
Splitter 82.60 2122.69 137.01 -15.11 17.64 StrikeCalled 5.66 -0.47 6.28 4.6
Changeup 82.88 2205.21 137.54 -14.37 17.18 BallCalled 5.68 -0.53 6.20 4.6
Changeup 73.96 1714.26 125.42 -16.06 13.11 BallCalled 5.33 -0.77 6.10 4.2
Changeup 82.96 2073.38 126.49 -18.34 15.05 InPlay 5.78 -0.43 6.16 4.2
Changeup 75.87 1643.03 120.63 -16.06 11.14 BallCalled 5.30 -0.96 6.27 4.0
Changeup 82.35 2189.21 135.50 -15.57 17.41 BallCalled 5.76 -0.42 6.11 4.5
Changeup 83.13 2111.09 134.59 -17.22 18.51 StrikeSwinging 5.62 -0.50 6.16 4.5
Changeup 75.26 1549.80 105.89 -14.95 5.77 StrikeCalled 5.30 -0.91 6.12 3.5
Splitter 83.41 2185.09 139.47 -15.21 19.24 FoulBallNotFieldable 5.75 -0.34 6.13 4.6
Changeup 76.35 1778.36 119.94 -12.31 8.57 FoulBallNotFieldable 5.26 -0.82 6.63 4.0
Changeup 83.47 2223.19 128.33 -17.11 15.01 BallCalled 5.96 -0.19 5.98 4.3
Changeup 81.69 2069.75 130.75 -15.73 14.95 InPlay 5.55 -0.57 6.12 4.4
NA NA NA NA NA NA StrikeCalled NA NA NA NA
Changeup 76.13 1782.68 99.33 -13.54 3.57 StrikeCalled 5.27 -0.92 6.18 3.3
Changeup 83.35 2185.10 124.28 -16.01 12.23 StrikeSwinging 5.67 -0.46 6.12 4.1
Changeup 80.98 2154.31 134.79 -14.28 15.61 StrikeCalled 5.49 -0.74 6.13 4.5
Splitter 84.39 2223.58 147.25 -12.97 21.54 BallCalled 5.84 -0.44 5.91 4.9
Changeup 75.38 1618.26 126.59 -13.49 11.34 BallCalled 5.12 -0.92 6.19 4.2
Changeup 82.69 2216.06 124.19 -17.58 13.19 InPlay 5.44 -0.69 6.07 4.1
Changeup 83.68 2237.03 135.70 -14.27 16.00 BallCalled 5.81 -0.48 5.86 4.5
Four-Seam 83.14 2217.69 136.83 -13.67 15.82 StrikeCalled 5.58 -0.54 6.25 4.6
Changeup 75.58 1600.05 104.61 -12.67 4.75 BallCalled 5.17 -0.88 6.31 3.5
Changeup 82.98 2259.15 130.53 -15.74 14.65 BallCalled 5.62 -0.51 5.96 4.4
Changeup 81.15 2209.92 119.25 -18.15 11.30 StrikeSwinging 5.48 -0.48 6.23 4.0
Changeup 82.65 2243.26 136.28 -13.61 15.54 InPlay 5.55 -0.38 6.18 4.5
Changeup 82.73 2196.24 137.59 -13.12 15.67 InPlay 5.55 -0.53 6.24 4.6
Changeup 84.60 2216.92 132.24 -16.69 16.51 BallCalled 5.75 -0.37 6.26 4.4
Changeup 83.57 2232.07 122.01 -19.97 13.73 BallCalled 5.60 -0.54 6.13 4.1
Changeup 81.53 2234.07 134.34 -15.27 16.23 FoulBallNotFieldable 5.56 -0.40 6.26 4.5
Changeup 73.68 1668.77 119.07 -12.32 8.26 FoulBallNotFieldable 5.17 -0.70 6.55 4.0
Changeup 75.46 1618.96 105.29 -13.89 5.08 InPlay 5.16 -0.71 6.47 3.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:  36
# 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 86.11% 31 13 18 41.94% 58.06% 79.46 1969.21 12.02 -15.43 123.16 4.1 5.46 -0.65 6.16
Four-Seam 2.78% 1 0 1 0.00% 100.00% 83.14 2217.69 15.82 -13.67 136.83 4.6 5.58 -0.54 6.25
Splitter 8.33% 3 1 2 33.33% 66.67% 83.47 2177.12 19.47 -14.43 141.24 4.7 5.75 -0.42 6.11
NA 2.78% 1 0 1 0.00% 100.00% NaN NaN NaN NaN NaN NaN NaN NaN NaN
# 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:  83.14 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", "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
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # 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")
  )