# 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.29 2191.46 218.67 12.11 16.28 StrikeCalled 5.82 2.14 6.55 7.3
Curveball 78.19 2349.49 54.81 -6.97 -3.57 BallCalled 5.77 2.28 5.67 1.8
Four-Seam 89.68 2145.68 215.36 11.37 17.07 StrikeSwinging 5.84 2.12 6.46 7.2
Four-Seam 90.45 2232.36 212.05 11.62 19.65 StrikeSwinging 5.86 2.06 6.25 7.1
Four-Seam 90.23 2105.68 210.44 10.99 19.81 BallCalled 5.96 2.26 6.26 7.0
Four-Seam 89.79 2167.73 217.30 13.38 18.68 BallCalled 5.82 2.29 6.15 7.2
Four-Seam 88.60 2148.04 215.23 11.91 18.01 InPlay 5.77 2.28 6.41 7.2
Changeup 81.69 1992.42 260.45 18.02 4.30 StrikeCalled 5.51 2.65 6.31 8.7
Changeup 81.46 1913.26 254.60 17.95 6.25 InPlay 5.52 2.65 6.15 8.5
Four-Seam 88.33 1975.12 217.16 13.30 18.68 StrikeCalled 5.62 2.33 6.44 7.2
Changeup 80.37 1912.48 248.28 16.78 8.03 BallCalled 5.47 2.59 6.11 8.3
Four-Seam 89.23 2113.65 220.31 13.89 17.50 StrikeSwinging 5.68 2.40 6.44 7.3
Four-Seam 91.32 2059.96 206.55 8.12 17.23 BallCalled 5.68 2.31 6.35 6.9
Changeup 81.59 1920.84 254.82 18.16 6.19 StrikeSwinging 5.31 2.61 6.75 8.5
Four-Seam 89.18 2168.29 217.93 13.12 18.07 StrikeCalled 5.84 2.20 6.49 7.3
Changeup 80.44 2034.46 251.59 16.16 6.90 BallCalled 5.56 2.79 6.32 8.4
Four-Seam 89.25 2079.25 220.00 12.40 15.94 FoulBallNotFieldable 5.82 2.22 6.60 7.3
Changeup 83.45 2084.79 247.68 15.72 7.70 StrikeSwinging 5.54 2.80 6.47 8.3
Curveball 77.67 2314.34 59.30 -8.52 -3.60 BallCalled 5.54 2.49 5.56 2.0
Slider 76.46 2350.07 80.16 -14.04 -1.04 StrikeCalled 5.53 2.45 5.42 2.7
Curveball 78.20 2378.49 66.48 -11.39 -3.59 StrikeCalled 5.57 2.29 5.61 2.2
Slider 79.25 2523.61 71.49 -11.04 -2.36 BallCalled 5.68 2.32 5.68 2.4
Four-Seam 91.13 2138.99 216.92 13.03 18.46 StrikeSwinging 5.75 2.22 6.35 7.2
Changeup 82.42 2069.52 246.12 18.51 9.56 StrikeCalled 5.56 2.75 6.78 8.2
Sinker 90.23 2217.35 221.54 15.51 18.68 StrikeSwinging 5.59 2.43 6.23 7.4
Four-Seam 90.89 2121.87 214.50 12.68 19.58 BallCalled 5.65 2.29 6.32 7.2
Changeup 83.99 2109.77 258.44 19.30 5.24 StrikeSwinging 5.36 2.68 6.67 8.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 == "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:  27
# 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 29.63% 8 2 6 25.00% 75.00% 81.93 2004.69 6.77 17.58 252.75 8.4 5.48 2.69 6.44
Curveball 11.11% 3 2 1 66.67% 33.33% 78.02 2347.44 -3.59 -8.96 60.20 2.0 5.63 2.35 5.61
Four-Seam 48.15% 13 4 9 30.77% 69.23% 89.80 2126.78 18.07 12.15 215.57 7.2 5.78 2.24 6.39
Sinker 3.70% 1 0 1 0.00% 100.00% 90.23 2217.35 18.68 15.51 221.54 7.4 5.59 2.43 6.23
Slider 7.41% 2 1 1 50.00% 50.00% 77.85 2436.84 -1.70 -12.54 75.82 2.5 5.61 2.38 5.55
# 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.32 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.75 | PitchSide > 0.75 | 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.75, xmax = 0.75, 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")
  )