# Filter the data for the pitcher Keegan Burke
Brian_Foley_data <- data %>%
  filter(Pitcher == "Burke, Keegan")

# 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 Keegan Burke")
Detailed Pitch Table for Keegan Burke
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 86.93 2128.47 199.86 6.07 17.98 BallCalled 6.17 0.95 6.11 6.7
Four-Seam 86.73 2101.34 204.44 6.63 15.72 FoulBallNotFieldable 6.18 0.99 5.99 6.8
Slider 80.28 2186.19 123.81 -2.50 3.04 StrikeSwinging 5.86 1.26 5.58 4.1
Slider 81.11 2268.76 113.17 -5.96 4.02 BallCalled 5.75 1.37 5.64 3.8
Four-Seam 87.29 2176.14 197.44 5.57 19.01 BallCalled 6.14 0.87 6.10 6.6
Four-Seam 87.43 2183.89 190.48 2.67 15.73 FoulBallNotFieldable 6.11 0.94 6.14 6.3
Slider 80.37 2221.39 148.03 -2.43 5.18 InPlay 5.89 1.41 5.59 4.9
Slider 78.37 2112.40 137.59 -0.91 2.47 BallCalled 5.81 0.90 5.84 4.6
Four-Seam 85.07 2085.49 198.90 5.40 17.06 StrikeCalled 6.21 0.70 6.08 6.6
Slider 80.18 2139.65 121.77 -4.70 4.29 StrikeSwinging 5.84 0.99 5.85 4.1
Curveball 77.63 2279.22 11.75 -2.61 -10.99 BallCalled 6.18 0.83 5.75 0.4
Four-Seam 86.52 2174.30 197.72 5.12 17.27 FoulBallNotFieldable 6.12 0.61 6.10 6.6
Curveball 78.78 2262.88 357.99 0.36 -8.75 InPlay 6.04 1.05 5.53 11.9
Four-Seam 86.75 1960.75 211.94 7.09 12.45 StrikeCalled 6.16 0.56 6.11 7.1
Slider 81.78 2181.39 104.92 -4.48 2.62 InPlay 5.81 1.03 5.74 3.5
Four-Seam 86.05 2055.12 200.77 5.59 15.91 StrikeCalled 6.19 0.92 6.03 6.7
Slider 82.06 2268.88 142.06 -4.07 6.42 BallCalled 5.76 1.25 5.62 4.7
Slider 81.79 2084.27 87.24 -0.44 1.33 BallCalled 5.80 1.23 5.82 2.9
Four-Seam 86.76 2151.73 207.19 8.87 18.50 BallCalled 6.20 0.83 5.79 6.9
Four-Seam 86.25 2131.46 204.94 8.08 18.61 InPlay 6.11 0.88 5.89 6.8
Four-Seam 86.03 2226.99 194.87 4.51 18.18 BallCalled 6.08 0.38 6.30 6.5
Slider 81.13 2186.58 185.07 0.28 4.43 BallCalled 5.78 1.05 5.84 6.2
Four-Seam 87.17 2099.50 201.27 6.09 16.75 BallCalled 6.08 0.54 6.03 6.7
Slider 81.70 2224.80 127.60 -4.30 4.57 StrikeCalled 5.81 1.00 5.75 4.3
Slider 79.88 2199.77 109.33 -4.35 2.85 StrikeCalled 5.90 1.08 5.76 3.6
Four-Seam 86.47 1940.68 195.24 4.28 16.86 FoulBallNotFieldable 6.10 0.46 6.10 6.5
Four-Seam 86.55 2143.98 184.58 1.53 20.39 BallCalled 6.16 0.60 5.81 6.2
Slider 81.44 2205.98 132.94 -6.26 7.28 BallCalled 5.89 1.39 5.56 4.4
Four-Seam 86.56 2172.14 199.01 4.93 15.56 FoulBallNotFieldable 6.10 0.79 6.14 6.6
Four-Seam 86.72 2240.35 203.88 6.55 16.07 BallCalled 6.03 0.68 6.20 6.8
Four-Seam 86.13 2186.93 195.37 4.62 18.01 InPlay 6.06 0.81 6.19 6.5
# 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:  31
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Keegan Burke")
Summary Pitch Table for Keegan Burke
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Curveball 6.45% 2 1 1 50.00% 50.00% 78.20 2271.05 -9.87 -1.12 184.87 6.2 6.11 0.94 5.64
Four-Seam 54.84% 17 7 10 41.18% 58.82% 86.55 2127.02 17.06 5.51 199.29 6.6 6.13 0.74 6.07
Slider 38.71% 12 6 6 50.00% 50.00% 80.84 2190.00 4.04 -3.34 127.79 4.3 5.82 1.16 5.72
# 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("Keegan Burke maximum FB velocity: ", max_fb_velocity, "mph\n")
## Keegan Burke maximum FB velocity:  87.43 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 Keegan Burke",
       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")
  )