# 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 84.82 2199.50 131.79 -17.24 16.82 StrikeCalled 5.66 -0.49 5.75 4.4
Curveball 75.32 2123.69 309.20 7.19 -4.31 StrikeCalled 5.29 -0.61 4.95 10.3
Curveball 75.39 2289.44 278.73 12.74 -0.22 BallCalled 5.39 -0.70 5.09 9.3
Sinker 86.00 2273.69 133.87 -17.27 17.96 FoulBallNotFieldable 5.59 -0.46 5.73 4.5
Changeup 84.43 2184.21 130.50 -16.78 15.72 StrikeCalled 5.61 -0.41 5.85 4.3
Four-Seam 85.68 2292.89 140.57 -14.02 18.45 StrikeSwinging 5.70 -0.43 5.74 4.7
Changeup 74.47 1716.86 132.10 -12.95 13.24 InPlay 5.09 -0.68 6.28 4.4
Changeup 84.10 2208.87 131.19 -17.22 16.44 InPlay 5.68 -0.25 5.82 4.4
Curveball 77.81 2077.15 208.15 0.23 2.01 BallCalled 5.35 -0.60 5.40 6.9
Changeup 83.69 2176.59 120.06 -20.72 13.38 StrikeCalled 5.63 -0.43 5.75 4.0
Changeup 84.84 2178.02 121.81 -18.62 12.92 BallCalled 5.74 -0.37 5.80 4.1
Curveball 73.54 2008.23 231.42 4.33 4.87 BallCalled 5.14 -0.84 5.37 7.7
Changeup 82.95 2053.70 123.05 -20.12 14.54 BallCalled 5.55 -0.58 5.84 4.1
Changeup 83.98 2190.87 129.78 -17.20 15.73 StrikeSwinging 5.64 -0.50 5.81 4.3
Sinker 86.08 2279.63 136.26 -15.93 17.97 BallCalled 5.79 -0.43 5.86 4.5
Changeup 85.08 2187.04 131.26 -16.31 15.68 StrikeSwinging 5.55 -0.52 5.88 4.4
Changeup 84.91 2205.36 130.01 -18.49 16.94 FoulBallNotFieldable 5.73 -0.22 5.86 4.3
Splitter 85.64 2344.36 133.25 -18.24 18.54 InPlay 5.72 -0.29 5.76 4.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:  18
# 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 55.56% 10 2 8 20.00% 80.00% 83.33 2130.10 15.14 -17.56 128.16 4.3 5.59 -0.44 5.86
Curveball 22.22% 4 3 1 75.00% 25.00% 75.52 2124.63 0.59 6.12 256.88 8.6 5.29 -0.69 5.20
Four-Seam 5.56% 1 0 1 0.00% 100.00% 85.68 2292.89 18.45 -14.02 140.57 4.7 5.70 -0.43 5.74
Sinker 11.11% 2 1 1 50.00% 50.00% 86.04 2276.66 17.96 -16.60 135.06 4.5 5.69 -0.44 5.80
Splitter 5.56% 1 0 1 0.00% 100.00% 85.64 2344.36 18.54 -18.24 133.25 4.4 5.72 -0.29 5.76
# 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:  86.08 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")
  )