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

# Create a detailed table for each pitch
detailed_pitch_table <- Brandon_Adams_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.37 2131.59 218.92 13.51 17.95 BallCalled 5.72 2.35 7.41 7.3
Four-Seam 88.76 2089.87 212.82 10.94 18.13 FoulBallNotFieldable 5.67 2.17 7.49 7.1
Changeup 81.15 1869.15 238.55 12.93 9.31 BallCalled 5.50 2.63 7.02 8.0
Four-Seam 89.75 2110.80 217.62 12.15 17.06 InPlay 5.63 2.16 7.41 7.3
Curveball 76.53 2145.50 66.86 -13.80 -4.33 BallCalled 5.67 2.21 6.34 2.2
Four-Seam 90.06 2136.30 214.28 11.22 17.65 InPlay 5.71 1.93 7.38 7.1
Curveball 76.28 2192.97 68.91 -9.14 -1.98 BallCalled 5.65 2.41 6.22 2.3
Curveball 75.82 2243.41 47.21 -9.16 -6.95 StrikeCalled 5.58 2.34 6.40 1.6
Four-Seam 90.35 2095.84 214.15 10.43 16.51 StrikeCalled 5.72 2.15 7.22 7.1
Slider 78.05 2292.88 54.76 -6.80 -3.43 BallinDirt 5.57 2.27 6.45 1.8
Four-Seam 89.63 2135.99 208.15 8.51 17.04 InPlay 5.72 2.12 7.45 6.9
Four-Seam 90.87 2170.75 217.64 12.28 17.15 StrikeCalled 5.71 2.08 7.22 7.3
Changeup 81.42 1921.16 236.84 13.92 10.48 InPlay 5.36 2.61 6.92 7.9
NA NA NA NA NA NA BallIntentional NA NA NA NA
Four-Seam 90.69 2220.05 217.39 13.34 18.69 FoulBallNotFieldable 5.71 2.21 7.35 7.2
Four-Seam 90.63 2164.37 207.35 9.67 19.91 InPlay 5.65 2.17 7.10 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:  16
# 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 12.50% 2 1 1 50.00% 50.00% 81.28 1895.16 9.89 13.43 237.70 8.0 5.43 2.62 6.97
Curveball 18.75% 3 2 1 66.67% 33.33% 76.21 2193.96 -4.42 -10.70 60.99 2.0 5.63 2.32 6.32
Four-Seam 56.25% 9 1 8 11.11% 88.89% 90.01 2139.51 17.79 11.34 214.26 7.1 5.69 2.15 7.34
Slider 6.25% 1 0 1 0.00% 100.00% 78.05 2292.88 -3.43 -6.80 54.76 1.8 5.57 2.27 6.45
NA 6.25% 1 0 1 0.00% 100.00% NaN NaN NaN NaN NaN NaN NaN NaN NaN
# 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:  90.87 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brandon_Adams_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
  labs(title = "Pitch Movement for Brandon Adams",
       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")
  )