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

# 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 Brandon Adams")
Detailed Pitch Table for Brandon Adams
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 89.51 2217.20 209.00 12.32 23.35 FoulBallNotFieldable 5.51 1.92 7.44 7.0
Slider 77.46 2312.71 78.54 -10.77 -0.55 FoulBallNotFieldable 5.36 2.31 6.47 2.6
Four-Seam 91.21 2085.94 212.56 13.64 22.43 StrikeSwinging 5.49 2.08 7.35 7.1
Slider 78.67 2393.47 74.65 -11.52 -1.79 BallinDirt 5.27 2.30 6.49 2.5
Four-Seam 89.85 2123.04 208.37 12.12 23.56 StrikeSwinging 5.42 2.12 7.42 6.9
Four-Seam 91.06 2212.74 205.72 10.50 22.93 FoulBallNotFieldable 5.51 1.82 7.21 6.9
Changeup 81.17 1874.86 247.18 16.20 8.09 InPlay 5.14 2.59 7.31 8.2
Four-Seam 90.91 2158.08 204.24 9.28 21.67 StrikeSwinging 5.49 2.03 7.37 6.8
Four-Seam 91.88 2240.80 212.58 12.95 21.31 FoulBallNotFieldable 5.53 1.78 7.44 7.1
Changeup 83.79 2078.73 238.73 17.20 11.71 InPlay 5.19 2.44 6.96 8.0
Curveball 77.92 2252.51 57.18 -5.77 -2.31 HitByPitch 5.36 2.38 6.60 1.9
Four-Seam 89.48 2067.08 203.97 9.81 23.18 BallCalled 5.43 1.84 7.47 6.8
Four-Seam 88.77 2063.38 205.43 10.02 22.22 FoulBallNotFieldable 5.42 1.95 7.59 6.8
Four-Seam 89.99 2125.97 201.42 8.43 22.63 StrikeSwinging 5.50 1.81 7.24 6.7
Four-Seam 90.72 2182.67 213.71 13.81 21.80 BallCalled 5.53 1.95 7.39 7.1
Curveball 78.85 2432.04 67.57 -9.44 -2.48 BallinDirt 5.38 1.88 6.46 2.3
Four-Seam 90.89 2147.78 214.33 13.04 20.13 StrikeCalled 5.43 1.85 7.41 7.1
Four-Seam 87.91 2069.41 211.93 11.30 19.22 StrikeCalled 5.50 1.93 7.38 7.1
Curveball 76.22 2406.52 47.57 -4.95 -3.11 StrikeCalled 5.37 2.19 6.47 1.6
Slider 78.81 2458.47 79.54 -7.72 -0.09 BallinDirt 5.39 2.15 6.49 2.7
Four-Seam 88.75 2105.16 212.03 12.47 21.03 FoulBallNotFieldable 5.46 2.02 7.44 7.1
Slider 77.96 2419.91 97.79 -10.76 2.82 BallCalled 5.41 2.09 6.31 3.3
Slider 78.25 2398.19 85.20 -10.30 0.65 FoulBallNotFieldable 5.39 2.20 6.43 2.8
Four-Seam 89.84 2129.60 203.45 10.16 24.61 BallCalled 5.59 1.82 7.28 6.8
Four-Seam 90.31 2105.25 209.86 12.27 22.55 InPlay 5.32 1.97 7.19 7.0
Four-Seam 88.81 2110.95 207.83 11.12 22.24 BallCalled 5.50 1.89 7.45 6.9
Four-Seam 89.35 2153.56 200.28 8.21 23.46 BallCalled 5.47 1.84 7.11 6.7
Four-Seam 88.92 2015.86 211.02 11.90 20.86 FoulBallNotFieldable 5.30 2.06 7.47 7.0
Four-Seam 89.40 2118.07 209.83 11.80 21.68 InPlay 5.39 2.04 7.52 7.0
Four-Seam 90.09 2043.06 210.21 11.40 20.73 HitByPitch 5.44 1.91 7.17 7.0
Four-Seam 88.86 2186.75 211.09 12.60 22.03 HitByPitch 5.46 2.23 7.38 7.0
Four-Seam 87.91 2071.65 212.81 12.77 20.97 FoulBallNotFieldable 5.39 2.16 7.42 7.1
Changeup 81.24 2043.46 245.90 18.59 9.74 BallCalled 5.17 2.52 7.08 8.2
Four-Seam 88.97 2209.50 203.80 10.04 23.99 StrikeSwinging 5.44 1.92 7.38 6.8
Four-Seam 87.62 2123.26 208.86 12.44 23.82 BallCalled 5.45 2.08 7.32 7.0
Four-Seam 87.49 2100.12 210.12 12.72 23.18 FoulBallNotFieldable 5.35 2.11 7.39 7.0
Slider 77.41 2344.99 109.49 -6.41 3.61 BallCalled 5.19 2.28 6.50 3.6
Four-Seam 88.62 2146.01 206.64 10.61 22.28 InPlay 5.25 2.17 7.39 6.9
# 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 == "BallCalled"),
    Strikes = sum(PitchCall != "BallCalled"), # 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:  38
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Brandon Adams")
Summary Pitch Table for Brandon Adams
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 7.89% 3 1 2 33.33% 66.67% 82.07 1999.02 9.85 17.33 243.94 8.1 5.17 2.52 7.12
Curveball 7.89% 3 0 3 0.00% 100.00% 77.66 2363.69 -2.63 -6.72 57.44 1.9 5.37 2.15 6.51
Four-Seam 68.42% 26 6 20 23.08% 76.92% 89.50 2127.42 22.23 11.45 208.50 7.0 5.44 1.97 7.37
Slider 15.79% 6 2 4 33.33% 66.67% 78.09 2387.96 0.77 -9.58 87.53 2.9 5.34 2.22 6.45
# 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("Brandon Adams maximum FB velocity: ", max_fb_velocity, "mph\n")
## Brandon Adams maximum FB velocity:  91.88 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", "FoulBallNonSwinging", "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 Brandon Adams",
       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")
  )