# 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
Splitter 84.36 2261.77 134.54 -16.45 17.63 StrikeCalled 5.65 -0.44 5.79 4.5
Changeup 76.01 1667.05 113.67 -13.10 7.28 FoulBallNotFieldable 5.11 -0.95 6.60 3.8
Four-Seam 85.67 2005.16 141.03 -13.16 17.52 BallCalled 5.82 -0.35 5.59 4.7
Changeup 83.10 2102.02 139.56 -15.38 19.57 FoulBallNotFieldable 5.61 -0.69 5.64 4.7
Sinker 85.31 2214.53 139.52 -14.81 18.67 FoulBallNotFieldable 5.75 -0.42 5.78 4.7
Changeup 75.87 1953.61 103.37 -19.71 6.26 BallCalled 5.13 -0.86 5.69 3.4
Changeup 77.53 1830.59 137.25 -5.75 7.66 BallCalled 5.12 -1.00 5.89 4.6
Sinker 85.31 2151.89 145.82 -13.64 21.39 StrikeSwinging 5.70 -0.37 5.67 4.9
Changeup 84.22 2287.55 130.86 -17.25 16.26 StrikeSwinging 5.56 -0.46 5.89 4.4
Four-Seam 85.91 2308.43 137.79 -14.94 17.79 BallCalled 5.70 -0.41 5.67 4.6
Changeup 84.11 2291.77 129.74 -18.27 16.59 FoulBallNotFieldable 5.63 -0.24 5.62 4.3
Sinker 85.15 2243.26 139.90 -13.60 17.60 FoulBallNotFieldable 5.70 -0.36 5.98 4.7
Changeup 77.16 1719.38 111.52 -18.40 8.81 StrikeCalled 5.07 -0.79 6.28 3.7
Changeup 83.68 2304.17 129.04 -19.16 16.96 BallCalled 5.51 -0.36 5.90 4.3
Splitter 84.93 2274.53 139.15 -15.89 19.78 BallCalled 5.58 -0.34 6.06 4.6
Splitter 83.11 2207.41 130.52 -18.30 17.10 BallCalled 5.54 -0.40 6.04 4.4
Splitter 83.97 2185.17 139.11 -15.98 19.88 BallCalled 5.48 -0.48 5.92 4.6
Splitter 83.97 2267.68 131.93 -17.99 17.59 FoulBallNotFieldable 5.59 -0.57 5.83 4.4
Changeup 84.06 2177.30 130.47 -17.26 16.02 InPlay 5.54 -0.46 5.78 4.3
Changeup 75.15 1726.61 113.41 -17.06 8.91 StrikeCalled 5.07 -1.04 6.11 3.8
Changeup 76.91 1836.87 112.52 -13.71 7.10 FoulBallNotFieldable 5.19 -0.78 6.13 3.8
Changeup 79.58 2148.35 137.59 -15.43 18.37 StrikeSwinging 5.58 -0.40 6.07 4.6
# 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:  22
# 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.55% 12 3 9 25.00% 75.00% 79.78 2003.77 12.48 -15.87 124.08 4.1 5.34 -0.67 5.97
Four-Seam 9.09% 2 2 0 100.00% 0.00% 85.79 2156.80 17.66 -14.05 139.41 4.7 5.76 -0.38 5.63
Sinker 13.64% 3 0 3 0.00% 100.00% 85.26 2203.23 19.22 -14.02 141.75 4.8 5.72 -0.38 5.81
Splitter 22.73% 5 3 2 60.00% 40.00% 84.07 2239.31 18.40 -16.92 135.05 4.5 5.57 -0.45 5.93
# 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:  85.91 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")
  )