# 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 83.30 2193.82 128.11 -19.20 16.36 FoulBallNotFieldable 5.75 -0.91 6.36 4.3
Changeup 82.03 2215.14 126.28 -15.59 12.87 StrikeSwinging 5.72 -0.87 6.17 4.2
Splitter 84.92 2289.57 137.31 -17.05 19.96 BallCalled 6.04 -0.57 6.12 4.6
Changeup 76.17 1743.14 113.97 -14.08 7.87 BallCalled 5.22 -1.33 6.43 3.8
Four-Seam 84.74 2355.61 145.47 -11.82 18.63 FoulBallNotFieldable 5.93 -0.62 6.32 4.8
Changeup 76.71 1652.79 100.31 -16.83 4.65 InPlay 5.35 -1.05 6.23 3.3
Changeup 76.56 1689.37 119.73 -14.87 10.09 BallCalled 5.41 -1.12 6.48 4.0
Splitter 83.59 2255.18 135.77 -16.12 18.00 StrikeSwinging 5.94 -0.49 6.17 4.5
Changeup 83.38 2196.17 124.36 -14.81 11.57 FoulBallNotFieldable 5.78 -0.75 6.36 4.1
Changeup 84.77 2286.58 127.83 -18.60 16.00 BallCalled 5.97 -0.68 6.08 4.3
Changeup 76.13 1709.03 112.22 -17.73 8.86 InPlay 5.31 -1.08 6.27 3.7
Curveball 74.11 2245.78 325.83 3.19 -2.88 BallCalled 5.38 -1.04 5.39 10.9
Splitter 83.68 2245.23 131.91 -18.64 18.22 StrikeCalled 5.91 -0.69 6.33 4.4
Curveball 74.20 2061.05 315.77 3.28 -1.72 BallCalled 5.46 -0.93 5.92 10.5
Changeup 81.82 2213.57 129.30 -16.18 14.72 BallCalled 5.76 -0.73 6.50 4.3
Changeup 82.33 2207.21 129.33 -19.87 17.70 BallCalled 5.82 -0.81 6.07 4.3
Four-Seam 84.12 2227.26 137.82 -12.44 15.09 FoulBallNotFieldable 5.82 -0.69 6.23 4.6
Changeup 75.83 1506.76 81.81 -11.86 -0.32 StrikeCalled 5.34 -1.06 5.99 2.7
Changeup 77.02 2059.99 121.25 -18.83 12.96 BallCalled 5.78 -0.82 6.26 4.0
Changeup 75.80 1762.64 126.43 -14.45 12.24 StrikeSwinging 5.40 -0.91 6.22 4.2
Splitter 83.33 2242.46 135.45 -16.46 18.06 StrikeCalled 5.88 -0.84 6.17 4.5
Curveball 74.53 2131.28 218.08 1.25 3.40 StrikeCalled 5.40 -1.25 5.53 7.3
Slider 74.96 2228.05 248.95 3.85 3.45 BallCalled 5.53 -1.07 5.59 8.3
Splitter 83.50 2275.22 139.19 -16.28 20.31 InPlay 5.81 -0.63 6.31 4.6
Splitter 84.15 2191.39 137.51 -16.41 19.28 StrikeCalled 5.97 -0.58 6.27 4.6
Changeup 82.81 2196.50 128.73 -18.55 16.34 StrikeCalled 5.90 -0.76 6.37 4.3
Changeup 75.60 1666.55 117.22 -16.46 10.10 StrikeSwinging 5.34 -1.07 6.22 3.9
Changeup 81.92 2126.61 129.99 -12.94 12.27 InPlay 5.99 -0.63 6.27 4.3
# 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:  28
# 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 57.14% 16 6 10 37.50% 62.50% 79.51 1964.12 11.52 -16.30 119.80 4.0 5.62 -0.91 6.27
Curveball 10.71% 3 2 1 66.67% 33.33% 74.28 2146.04 -0.40 2.57 286.56 9.6 5.41 -1.07 5.61
Four-Seam 7.14% 2 0 2 0.00% 100.00% 84.43 2291.44 16.86 -12.13 141.64 4.7 5.88 -0.66 6.28
Slider 3.57% 1 1 0 100.00% 0.00% 74.96 2228.05 3.45 3.85 248.95 8.3 5.53 -1.07 5.59
Splitter 21.43% 6 1 5 16.67% 83.33% 83.86 2249.84 18.97 -16.83 136.19 4.5 5.92 -0.63 6.23
# 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:  84.74 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, 26.2)) +  # 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")
  )