# 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 82.36 2185.16 140.41 -14.54 19.07 StrikeCalled 5.72 -0.48 5.82 4.7
Curveball 72.51 2283.56 278.26 5.45 0.90 BallCalled 5.35 -0.60 4.97 9.3
Splitter 83.84 2215.44 140.78 -14.72 19.42 BallCalled 5.67 -0.53 5.94 4.7
Curveball 72.72 2110.81 333.33 3.30 -4.62 FoulBallNotFieldable 5.27 -0.97 5.27 11.1
Splitter 84.70 2157.23 142.90 -13.05 18.54 BallCalled 5.74 -0.38 5.80 4.8
Changeup 82.80 2114.87 142.68 -13.13 18.50 FoulBallNotFieldable 5.62 -0.31 5.96 4.8
Changeup 73.44 1544.88 112.11 -13.77 7.25 InPlay 5.25 -0.85 6.15 3.7
Changeup 75.31 1524.81 107.36 -14.81 6.18 StrikeCalled 5.32 -0.73 5.76 3.6
Changeup 78.11 1770.00 114.44 -14.76 8.13 StrikeCalled 5.26 -0.86 6.12 3.8
Changeup 77.06 1680.53 108.61 -15.75 6.74 BallCalled 5.26 -0.84 6.27 3.6
Changeup 80.29 1974.15 127.92 -18.34 15.75 StrikeSwinging 5.69 -0.39 5.94 4.3
Changeup 75.47 1656.25 114.96 -14.19 8.18 InPlay 5.38 -0.63 5.74 3.8
Changeup 75.50 1779.37 105.83 -16.10 6.04 StrikeCalled 5.25 -0.98 6.32 3.5
Changeup 82.89 2183.20 132.21 -19.88 19.52 BallCalled 5.60 -0.49 5.82 4.4
Changeup 75.85 1785.40 108.89 -18.60 7.87 InPlay 5.26 -0.82 6.10 3.6
Changeup 75.43 1780.95 104.61 -18.80 6.41 BallCalled 5.26 -0.77 5.86 3.5
Changeup 84.02 2232.95 125.20 -20.00 15.52 StrikeCalled 5.64 -0.33 5.72 4.2
Changeup 74.24 1569.64 111.47 -15.22 7.63 InPlay 5.11 -0.77 6.23 3.7
Changeup 83.59 2170.26 133.65 -18.35 19.03 StrikeCalled 5.69 -0.44 5.78 4.5
Changeup 76.19 1527.66 110.62 -15.30 7.29 BallCalled 5.22 -0.82 5.98 3.7
Sinker 85.93 2243.74 136.14 -17.04 19.15 BallCalled 5.74 -0.35 5.89 4.5
Splitter 82.44 2147.01 139.51 -15.08 19.22 StrikeSwinging 5.58 -0.64 5.84 4.7
Changeup 83.76 2158.68 126.70 -21.65 17.61 StrikeSwinging 5.79 -0.28 5.73 4.2
Changeup 75.93 1644.61 105.44 -16.98 6.17 StrikeCalled 5.29 -0.82 6.28 3.5
Splitter 83.49 2286.71 138.70 -17.36 21.25 BallCalled 5.83 -0.40 5.80 4.6
Changeup 76.77 1669.73 119.23 -16.29 10.61 BallCalled 5.26 -0.77 5.89 4.0
Changeup 84.26 2158.01 134.60 -17.71 18.88 FoulBallNotFieldable 5.62 -0.42 5.74 4.5
Changeup 83.29 2253.51 133.71 -17.08 17.67 FoulBallNotFieldable 5.75 -0.23 6.16 4.5
Changeup 75.05 1745.38 130.97 -11.64 11.70 InPlay 5.35 -0.49 5.99 4.4
Four-Seam 84.09 2300.00 143.72 -13.17 19.26 StrikeCalled 5.68 -0.33 5.97 4.8
Splitter 85.57 2353.50 137.12 -17.34 19.96 InPlay 5.67 -0.25 5.80 4.6
Changeup 74.66 1571.58 114.69 -13.62 7.88 StrikeCalled 5.31 -0.88 6.46 3.8
Changeup 76.29 1741.98 118.07 -9.63 6.54 BallCalled 5.19 -0.83 6.40 3.9
Slider 83.58 2219.97 133.04 -17.88 18.04 StrikeSwinging 5.68 -0.11 5.82 4.4
Changeup 74.77 1574.06 108.05 -13.62 5.96 FoulBallNotFieldable 5.23 -0.62 5.84 3.6
Changeup 78.55 2121.59 136.32 -16.72 19.06 BallCalled 5.62 -0.16 5.95 4.5
Changeup 81.73 2203.74 134.70 -16.97 18.24 BallCalled 5.73 -0.35 6.09 4.5
Changeup 81.75 2207.01 133.62 -16.97 17.60 InPlay 5.70 -0.27 6.14 4.5
Slider 74.40 2168.84 260.54 5.69 2.79 StrikeCalled 5.27 -0.78 5.53 8.7
Changeup 75.48 1653.41 115.50 -13.48 7.94 InPlay 5.13 -0.82 6.48 3.9
Splitter 82.85 2227.11 133.93 -17.72 18.48 StrikeSwinging 5.85 -0.15 5.84 4.5
Splitter 82.61 2257.23 132.96 -18.00 18.13 StrikeSwinging 5.60 -0.48 6.08 4.4
Slider 83.87 2310.52 137.51 -16.96 19.84 StrikeSwinging 5.87 0.00 5.94 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 %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:  43
# 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 65.12% 28 8 20 28.57% 71.43% 78.30 1857.08 11.64 -16.05 120.43 4.0 5.42 -0.61 6.03
Curveball 4.65% 2 1 1 50.00% 50.00% 72.62 2197.18 -1.86 4.38 305.79 10.2 5.31 -0.78 5.12
Four-Seam 2.33% 1 0 1 0.00% 100.00% 84.09 2300.00 19.26 -13.17 143.72 4.8 5.68 -0.33 5.97
Sinker 2.33% 1 1 0 100.00% 0.00% 85.93 2243.74 19.15 -17.04 136.14 4.5 5.74 -0.35 5.89
Slider 6.98% 3 0 3 0.00% 100.00% 80.62 2233.11 13.56 -9.72 177.03 5.9 5.61 -0.30 5.76
Splitter 18.60% 8 3 5 37.50% 62.50% 83.48 2228.67 19.26 -15.98 138.29 4.6 5.71 -0.41 5.86
# 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.93 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")
  )