# 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 80.55 2169.82 131.35 -18.81 18.12 FoulBallNotFieldable 5.65 -0.45 6.07 4.4
Changeup 76.31 1821.81 104.86 -19.82 6.77 StrikeSwinging 5.22 -0.74 6.43 3.5
Changeup 74.92 1806.50 139.96 -11.93 15.70 BallCalled 5.27 -0.58 6.17 4.7
Changeup 82.89 2290.72 148.46 -12.42 21.53 FoulBallNotFieldable 5.67 -0.41 6.15 4.9
Changeup 74.80 1678.80 100.03 -15.84 4.34 InPlay 5.25 -0.52 6.74 3.3
Splitter 83.81 2286.92 138.57 -17.79 21.52 StrikeCalled 5.61 -0.29 6.00 4.6
Changeup 83.22 2282.62 131.50 -22.22 21.20 StrikeCalled 5.67 -0.25 5.97 4.4
Changeup 82.85 2285.87 129.10 -20.89 18.50 BallCalled 5.69 -0.17 5.99 4.3
Changeup 75.71 1894.28 126.95 -14.82 12.70 BallCalled 5.11 -0.60 6.46 4.2
Slider 83.78 2360.89 139.24 -16.28 20.32 InPlay 5.75 -0.13 5.91 4.6
Splitter 83.56 2150.74 138.86 -15.45 19.03 StrikeSwinging 5.72 -0.16 5.99 4.6
Changeup 76.91 1766.59 109.94 -14.48 6.61 StrikeCalled 5.28 -0.61 6.50 3.7
Slider 81.35 2257.62 134.61 -16.31 17.51 BallCalled 5.60 -0.09 6.11 4.5
Changeup 82.25 2203.78 134.41 -16.17 17.30 BallCalled 5.62 -0.29 6.03 4.5
Changeup 74.88 1689.94 115.29 -15.64 8.87 StrikeSwinging 5.32 -0.56 6.86 3.8
Changeup 84.31 2287.91 129.35 -19.88 17.60 StrikeCalled 5.59 -0.43 6.15 4.3
Curveball 74.30 2038.59 233.96 4.04 4.41 StrikeSwinging 5.20 -0.79 5.55 7.8
Changeup 83.07 2394.42 136.06 -18.41 20.50 InPlay 5.70 -0.31 6.05 4.5
Curveball 71.89 2131.88 315.87 6.31 -4.45 StrikeCalled 5.16 -0.90 5.60 10.5
Curveball 73.37 2087.80 288.07 5.71 -0.04 StrikeCalled 5.20 -0.73 5.61 9.6
Curveball 72.68 2181.75 296.64 7.32 -1.86 BallCalled 5.23 -0.55 5.45 9.9
Slider 83.93 2321.10 139.43 -16.70 20.78 BallCalled 5.78 -0.15 6.22 4.6
Curveball 70.87 2233.17 322.57 4.43 -4.10 BallCalled 5.03 -0.89 5.63 10.8
Changeup 82.33 2262.25 132.82 -18.26 18.20 FoulBallNotFieldable 5.51 -0.40 6.07 4.4
Changeup 70.82 1584.45 109.76 -12.44 6.17 InPlay 5.08 -0.62 6.83 3.7
Curveball 72.25 1815.89 232.76 24.95 20.49 StrikeCalled 5.22 -0.55 6.64 7.8
Changeup 83.93 2218.98 117.58 -18.65 11.16 BallCalled 5.52 -0.36 6.08 3.9
Changeup 72.98 1738.74 118.75 -13.72 9.19 InPlay 5.10 -0.69 6.70 4.0
Curveball 72.95 2281.01 288.73 0.42 1.66 BallCalled 5.22 -0.62 5.39 9.6
Splitter 84.39 2254.30 134.68 -17.80 19.03 StrikeCalled 5.72 -0.23 6.04 4.5
Changeup 81.82 2249.84 129.95 -18.40 16.84 StrikeCalled 5.50 -0.58 6.16 4.3
Changeup 81.12 2139.59 133.08 -18.06 18.39 StrikeCalled 5.59 -0.25 6.34 4.4
Changeup 73.50 1673.55 94.95 -15.80 2.87 BallCalled 5.18 -0.87 6.75 3.2
Changeup 83.46 2269.05 131.03 -17.18 16.31 StrikeCalled 5.74 -0.46 5.83 4.4
Splitter 83.37 2267.47 136.11 -17.29 19.34 StrikeCalled 5.70 -0.40 5.96 4.5
Changeup 70.91 1589.43 101.74 -15.01 4.72 BallCalled 5.09 -0.88 6.72 3.4
Splitter 83.33 2306.12 138.06 -17.96 21.37 FoulBallNotFieldable 5.61 -0.31 6.20 4.6
Splitter 82.76 2278.34 134.68 -18.52 19.91 FoulBallNotFieldable 5.58 -0.28 6.07 4.5
Changeup 81.93 2287.90 137.77 -18.08 21.38 FoulBallNotFieldable 5.60 -0.32 6.33 4.6
Changeup 74.22 1703.63 109.47 -13.30 6.16 BallCalled 5.28 -0.66 6.75 3.6
Changeup 73.29 1639.14 109.53 -14.53 6.69 StrikeSwinging 5.14 -0.60 6.69 3.7
Splitter 83.69 2314.49 136.01 -16.51 18.55 BallCalled 5.76 -0.19 5.95 4.5
Splitter 82.85 2202.88 141.12 -15.12 20.22 StrikeCalled 5.59 -0.33 6.11 4.7
Curveball 73.43 2256.67 315.25 4.36 -2.70 BallCalled 5.17 -0.74 5.73 10.5
Changeup 82.66 2317.19 133.57 -17.52 18.05 BallCalled 5.60 -0.16 6.20 4.5
Changeup 79.96 2105.04 137.27 -15.45 18.17 FoulBallNotFieldable 5.51 -0.32 6.33 4.6
Splitter 82.24 2182.95 141.72 -14.18 19.36 StrikeSwinging 5.60 -0.27 6.17 4.7
Four-Seam 85.30 2288.58 142.97 -14.58 20.65 FoulBallNotFieldable 5.70 -0.26 6.12 4.8
Changeup 75.67 1786.88 96.69 -17.07 3.47 BallCalled 5.11 -0.78 6.62 3.2
Splitter 83.15 2191.94 136.00 -16.42 18.43 FoulBallFieldable 5.68 -0.29 6.17 4.5
Slider 82.33 2278.58 151.55 -11.02 21.66 InPlay 5.79 -0.05 6.19 5.1
# 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:  51
# 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 54.90% 28 10 18 35.71% 64.29% 78.62 2004.95 13.13 -16.60 122.54 4.1 5.41 -0.50 6.36
Curveball 15.69% 8 4 4 50.00% 50.00% 72.72 2128.35 1.68 7.19 286.73 9.6 5.18 -0.72 5.70
Four-Seam 1.96% 1 0 1 0.00% 100.00% 85.30 2288.58 20.65 -14.58 142.97 4.8 5.70 -0.26 6.12
Slider 7.84% 4 2 2 50.00% 50.00% 82.85 2304.55 20.07 -15.08 141.21 4.7 5.73 -0.10 6.11
Splitter 19.61% 10 1 9 10.00% 90.00% 83.32 2243.61 19.68 -16.70 137.58 4.6 5.66 -0.28 6.07
# 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("Josh Keevan maximum FB velocity: ", max_fb_velocity, "mph\n")
## Josh Keevan maximum FB velocity:  85.3 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 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")
  )