# 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 76.13 1809.84 106.45 -17.72 6.71 BallCalled 5.04 -1.27 6.42 3.5
Four-Seam 84.70 2288.48 145.29 -12.18 18.93 BallCalled 5.63 -0.64 5.74 4.8
Splitter 83.06 2200.99 141.09 -13.78 18.49 BallCalled 5.67 -0.62 5.93 4.7
Changeup 83.59 2227.85 137.16 -15.04 17.62 StrikeCalled 5.55 -0.70 5.71 4.6
Four-Seam 84.65 2251.42 137.47 -14.42 17.06 StrikeSwinging 5.53 -0.83 5.87 4.6
Changeup 83.01 2235.72 134.79 -14.91 16.25 BallCalled 5.59 -0.86 5.97 4.5
Changeup 75.16 1811.95 116.59 -14.98 9.12 FoulBallNotFieldable 5.29 -0.89 6.14 3.9
Changeup 81.14 2187.52 129.06 -17.53 15.80 FoulBallNotFieldable 5.70 -0.79 5.84 4.3
Splitter 84.55 2325.27 132.37 -17.99 17.80 BallCalled 5.68 -0.55 6.00 4.4
Changeup 74.85 1688.68 149.35 -8.29 15.70 BallCalled 5.29 -0.85 6.25 5.0
Changeup 76.64 1686.15 125.06 -12.55 10.36 InPlay 5.26 -0.91 6.67 4.2
Changeup 83.74 2329.08 122.57 -20.03 14.34 StrikeCalled 5.67 -0.70 5.84 4.1
Changeup 84.84 2389.66 123.60 -19.78 14.60 StrikeSwinging 5.82 -0.38 5.71 4.1
Changeup 83.80 2372.06 132.82 -16.17 16.41 StrikeSwinging 5.72 -0.37 5.96 4.4
Changeup 83.73 2365.54 145.57 -14.40 22.40 StrikeSwinging 5.74 -0.46 5.90 4.9
Sinker 85.67 2371.82 132.44 -17.32 17.20 StrikeCalled 5.80 -0.28 5.76 4.4
Changeup 85.84 2294.50 129.40 -19.43 17.28 FoulBallNotFieldable 5.77 -0.49 5.86 4.3
Changeup 83.89 2312.80 127.77 -19.36 16.47 FoulBallNotFieldable 5.73 -0.46 5.93 4.3
Four-Seam 84.40 2282.85 154.06 -9.93 21.96 BallCalled 5.82 -0.17 5.93 5.1
Four-Seam 84.86 2284.39 144.20 -14.34 21.39 BallCalled 5.68 -0.54 5.76 4.8
Changeup 78.00 1830.56 112.11 -17.34 8.62 InPlay 5.22 -0.70 6.15 3.7
Changeup 84.71 2010.63 145.31 -10.84 17.01 BallCalled 5.78 -0.47 5.68 4.8
Splitter 83.40 2271.78 138.26 -15.95 19.42 FoulBallNotFieldable 5.64 -0.62 5.86 4.6
Four-Seam 85.04 2330.07 141.99 -14.47 19.94 BallCalled 5.73 -0.44 5.95 4.7
Four-Seam 84.77 2317.27 139.75 -14.96 19.07 BallCalled 5.56 -0.55 6.10 4.7
Changeup 81.33 2145.56 120.17 -19.17 12.60 StrikeCalled 5.48 -0.79 5.94 4.0
Splitter 83.85 2295.31 140.96 -14.20 18.92 InPlay 5.66 -0.49 5.94 4.7
Four-Seam 84.46 2293.12 137.59 -15.36 18.15 BallCalled 5.78 -0.53 5.89 4.6
Changeup 83.25 2300.39 135.77 -14.52 16.32 FoulBallNotFieldable 5.39 -0.73 6.03 4.5
Curveball 72.95 2135.41 319.20 3.80 -2.81 StrikeCalled 5.23 -0.91 5.33 10.6
Changeup 83.95 2270.17 151.27 -11.45 22.28 InPlay 5.86 -0.39 5.90 5.0
Splitter 83.45 2259.51 140.00 -15.01 19.38 StrikeSwinging 5.51 -0.79 5.91 4.7
Changeup 83.13 2221.56 129.19 -18.26 16.41 FoulBallNotFieldable 5.65 -0.91 5.65 4.3
Four-Seam 85.20 2297.60 139.53 -14.31 18.25 StrikeSwinging 5.66 -0.66 5.97 4.7
Changeup 83.75 2265.17 129.95 -16.32 15.14 StrikeSwinging 5.78 -0.50 5.77 4.3
Changeup 83.45 2324.37 130.83 -17.19 16.32 BallCalled 5.75 -0.43 5.85 4.4
Four-Seam 84.02 2299.42 147.85 -11.66 19.95 StrikeSwinging 5.70 -0.57 5.93 4.9
Changeup 84.38 2335.86 131.81 -17.11 16.72 FoulBallNotFieldable 5.79 -0.44 5.89 4.4
Four-Seam 84.14 2303.55 142.65 -13.42 19.07 InPlay 5.80 -0.38 5.97 4.8
Sinker 85.86 2346.28 134.28 -13.77 14.79 BallCalled 5.82 -0.33 6.23 4.5
Changeup 84.93 2352.89 130.74 -17.76 16.76 BallCalled 5.72 -0.49 6.09 4.4
Changeup 75.46 1629.51 121.87 -10.80 8.40 StrikeCalled 5.23 -0.99 6.29 4.1
Changeup 75.53 1721.38 104.38 -13.39 5.09 BallCalled 5.27 -0.69 6.66 3.5
Changeup 80.35 2114.48 143.26 -12.87 18.71 FoulBallNotFieldable 5.47 -0.59 6.12 4.8
Changeup 81.04 2102.44 143.36 -13.50 19.60 BallCalled 5.64 -0.57 5.88 4.8
Changeup 74.74 1567.22 93.48 -11.64 2.15 StrikeCalled 5.29 -1.03 6.01 3.1
Changeup 82.59 2343.98 139.25 -15.16 18.99 FoulBallNotFieldable 5.71 -0.54 5.75 4.6
Curveball 77.20 1847.39 35.82 -2.53 -2.05 BallCalled 5.20 -1.06 6.91 1.2
Changeup 84.04 2294.00 129.85 -17.34 15.96 FoulBallFieldable 5.73 -0.58 5.99 4.3
Changeup 75.08 1563.76 94.69 -14.58 2.82 StrikeSwinging 5.25 -1.00 6.61 3.2
Changeup 75.74 1688.51 111.54 -13.38 6.82 InPlay 5.26 -1.15 6.76 3.7
Changeup 82.91 2266.65 136.72 -14.47 16.87 InPlay 5.69 -0.55 5.96 4.6
Changeup 74.56 1593.21 102.97 -14.92 4.95 BallCalled 5.24 -1.07 6.46 3.4
# 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:  53
# 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 64.15% 34 9 25 26.47% 73.53% 80.86 2078.05 13.87 -15.36 127.02 4.2 5.54 -0.70 6.05
Curveball 3.77% 2 1 1 50.00% 50.00% 75.08 1991.40 -2.43 0.64 177.51 5.9 5.22 -0.99 6.12
Four-Seam 18.87% 10 6 4 60.00% 40.00% 84.62 2294.82 19.38 -13.51 143.04 4.8 5.69 -0.53 5.91
Sinker 3.77% 2 1 1 50.00% 50.00% 85.76 2359.05 15.99 -15.54 133.36 4.4 5.81 -0.31 6.00
Splitter 9.43% 5 2 3 40.00% 60.00% 83.66 2270.57 18.80 -15.39 138.54 4.6 5.63 -0.61 5.93
# 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.86 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")
  )