# 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 88.52 2109.83 207.09 9.94 20.58 StrikeCalled 5.95 2.18 6.66 6.9
Curveball 77.65 2357.77 51.65 -10.17 -6.64 StrikeCalled 5.88 2.61 5.59 1.7
Four-Seam 90.16 2158.91 212.93 11.80 19.32 BallCalled 6.03 2.30 6.44 7.1
Four-Seam 90.92 2218.76 214.84 12.48 19.05 BallCalled 5.94 2.35 6.39 7.2
Curveball 76.22 2495.74 57.06 -13.65 -7.47 InPlay 5.75 2.71 5.90 1.9
Four-Seam 89.21 2084.41 211.08 10.95 19.23 StrikeCalled 5.76 2.19 6.82 7.0
Four-Seam 89.30 2190.42 212.42 12.35 20.59 StrikeSwinging 6.00 2.27 6.55 7.1
Four-Seam 88.44 2103.88 206.57 10.60 22.37 BallCalled 5.87 2.44 6.62 6.9
Four-Seam 90.26 2164.48 213.48 12.72 20.38 FoulBallNotFieldable 5.87 2.34 6.55 7.1
Sinker 89.34 2069.71 221.48 14.55 17.51 StrikeCalled 5.94 2.41 6.89 7.4
Sinker 89.40 2124.64 233.34 17.59 14.13 FoulBallNotFieldable 5.74 2.60 6.77 7.8
Changeup 82.35 1923.70 248.75 16.96 7.94 StrikeSwinging 5.60 2.80 6.82 8.3
Changeup 83.35 1962.28 246.15 16.88 8.96 StrikeSwinging 5.66 2.96 6.23 8.2
Four-Seam 89.02 2241.56 217.83 12.24 17.01 BallCalled 5.75 2.61 6.86 7.3
Four-Seam 88.80 2144.85 216.02 13.97 20.49 InPlay 5.67 2.67 6.78 7.2
Curveball 77.24 2353.22 66.16 -5.01 -0.78 StrikeSwinging 5.76 2.54 6.11 2.2
Curveball 76.30 2425.25 63.51 -10.02 -3.45 StrikeSwinging 5.79 2.45 5.66 2.1
Changeup 82.19 2087.07 239.84 14.70 9.82 FoulBallNotFieldable 5.65 2.66 6.73 8.0
Four-Seam 90.02 2154.46 214.20 11.07 17.41 InPlay 5.92 2.22 6.43 7.1
Four-Seam 87.84 2127.33 218.35 13.09 17.81 BallCalled 5.73 2.62 6.56 7.3
Four-Seam 87.53 2119.48 211.42 11.06 19.40 BallCalled 5.70 2.61 6.48 7.0
Four-Seam 88.19 2146.28 221.31 14.47 17.71 StrikeSwinging 5.74 2.63 6.59 7.4
Four-Seam 88.37 2174.71 211.82 11.49 19.75 StrikeCalled 5.76 2.47 6.83 7.1
Four-Seam 89.90 2255.81 211.97 12.43 21.22 BallCalled 5.79 2.53 6.22 7.1
Changeup 84.18 2077.30 258.11 16.16 4.67 StrikeSwinging 5.41 2.77 6.78 8.6
Sinker 88.38 2081.37 220.26 13.86 17.56 StrikeCalled 5.74 2.34 6.88 7.3
Curveball 77.79 2330.16 48.01 -12.43 -9.65 BallCalled 5.65 2.65 5.85 1.6
Four-Seam 88.50 2138.55 206.14 9.98 21.54 InPlay 5.81 2.28 6.67 6.9
Four-Seam 89.54 2116.24 215.96 13.67 20.03 StrikeCalled 5.63 2.52 6.94 7.2
Changeup 83.23 2142.40 246.44 17.09 8.82 BallCalled 5.45 2.93 6.80 8.2
Four-Seam 88.67 2114.72 217.05 13.03 18.46 BallCalled 5.68 2.45 6.69 7.2
Four-Seam 88.73 2124.00 208.45 11.11 21.73 StrikeSwinging 5.77 2.43 6.65 6.9
Changeup 83.66 2143.36 256.44 17.78 5.62 BallCalled 5.43 2.97 6.54 8.5
Four-Seam 89.72 2211.65 219.25 14.12 18.41 StrikeCalled 5.69 2.41 6.77 7.3
# 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:  34
# 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 17.65% 6 2 4 33.33% 66.67% 83.16 2056.02 7.64 16.59 249.29 8.3 5.53 2.85 6.65
Curveball 14.71% 5 1 4 20.00% 80.00% 77.04 2392.43 -5.60 -10.26 57.28 1.9 5.77 2.59 5.82
Four-Seam 58.82% 20 8 12 40.00% 60.00% 89.08 2155.02 19.62 12.13 213.41 7.1 5.80 2.43 6.62
Sinker 8.82% 3 0 3 0.00% 100.00% 89.04 2091.91 16.40 15.33 225.03 7.5 5.81 2.45 6.85
# 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("Brandon Adams maximum FB velocity: ", max_fb_velocity, "mph\n")
## Brandon Adams maximum FB velocity:  90.92 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 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")
  )