# 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
Curveball 76.13 2354.88 65.71 -12.88 -4.33 StrikeCalled 5.64 2.63 5.60 2.2
Curveball 76.58 2396.97 66.11 -11.43 -3.68 BallCalled 5.80 2.42 5.87 2.2
Four-Seam 89.33 2243.61 217.88 12.47 17.15 FoulBallNotFieldable 5.90 2.13 6.60 7.3
Curveball 77.61 2354.30 67.26 -12.83 -4.04 BallCalled 5.78 2.24 6.11 2.2
Sinker 89.01 2122.87 222.82 12.64 14.73 StrikeCalled 5.93 1.99 6.55 7.4
Slider 77.37 2323.16 77.19 -8.45 -0.64 StrikeCalled 5.59 2.39 6.19 2.6
Curveball 77.47 2307.88 59.82 -6.33 -2.41 StrikeCalled 5.77 2.34 5.99 2.0
Slider 79.16 2382.14 64.18 -8.62 -2.79 BallCalled 5.80 2.30 5.54 2.1
Four-Seam 89.39 2158.91 216.73 12.18 17.39 FoulBallNotFieldable 5.94 2.11 6.66 7.2
Four-Seam 89.33 2153.11 215.67 12.50 18.48 InPlay 5.92 2.19 6.56 7.2
Curveball 77.28 2333.25 57.76 -10.46 -5.21 BallCalled 5.54 2.46 6.26 1.9
Four-Seam 88.81 2167.70 201.19 6.49 17.83 StrikeSwinging 5.89 2.09 6.66 6.7
Curveball 77.72 2465.90 52.04 -8.58 -5.40 FoulBallNotFieldable 5.62 2.36 5.82 1.7
Slider 77.48 2412.78 79.94 -12.54 -0.83 StrikeSwinging 5.79 2.26 6.00 2.7
Curveball 74.87 2272.97 62.96 -17.28 -7.30 InPlay 5.66 2.45 5.95 2.1
Four-Seam 87.29 2076.82 203.39 7.70 19.01 FoulBallNotFieldable 5.87 2.45 6.64 6.8
Curveball 75.80 2264.73 60.09 -12.83 -6.02 FoulBallNotFieldable 5.63 2.36 5.92 2.0
Curveball 77.52 2309.27 73.49 -11.82 -2.28 BallCalled 5.69 2.37 6.17 2.4
Four-Seam 89.57 2202.62 208.98 9.64 18.52 FoulBallNotFieldable 5.81 2.29 6.71 7.0
Four-Seam 89.54 2173.07 213.02 11.81 19.28 InPlay 5.86 2.28 6.58 7.1
Four-Seam 88.90 2214.17 208.95 9.56 18.36 BallCalled 5.80 2.12 6.99 7.0
Four-Seam 88.00 2090.64 210.71 8.75 15.89 BallCalled 5.79 2.05 6.78 7.0
Four-Seam 88.25 2125.26 204.88 8.64 19.78 StrikeCalled 5.84 2.19 6.69 6.8
Slider 77.15 2430.37 76.47 -8.69 -0.78 FoulBallNotFieldable 5.57 2.43 5.89 2.5
Curveball 77.18 2357.06 67.44 -11.97 -3.55 BallCalled 5.66 2.38 6.19 2.2
Four-Seam 89.02 2172.40 209.98 9.15 16.99 StrikeSwinging 5.82 2.16 6.72 7.0
Four-Seam 87.58 2157.31 206.13 8.71 19.01 BallCalled 5.73 2.29 6.96 6.9
Curveball 77.26 2481.39 74.56 -9.12 -1.05 BallCalled 5.56 2.41 6.19 2.5
Slider 77.59 2355.07 71.54 -9.73 -1.84 BallCalled 5.62 2.54 6.05 2.4
Four-Seam 88.64 2161.03 204.96 8.14 18.72 BallCalled 5.81 2.19 6.68 6.8
Slider 76.42 2288.93 87.03 -7.29 1.05 StrikeCalled 5.59 2.56 6.01 2.9
Slider 76.61 2325.65 86.77 -14.49 0.69 BallCalled 5.65 2.39 5.72 2.9
Four-Seam 87.99 2149.92 212.13 10.34 17.67 InPlay 5.72 2.26 6.78 7.1
# 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:  33
# 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
Curveball 33.33% 11 6 5 54.55% 45.45% 76.86 2354.42 -4.12 -11.41 64.29 2.1 5.67 2.40 6.01
Four-Seam 42.42% 14 4 10 28.57% 71.43% 88.69 2160.47 18.15 9.72 209.61 7.0 5.84 2.20 6.72
Sinker 3.03% 1 0 1 0.00% 100.00% 89.01 2122.87 14.73 12.64 222.82 7.4 5.93 1.99 6.55
Slider 21.21% 7 3 4 42.86% 57.14% 77.40 2359.73 -0.73 -9.97 77.59 2.6 5.66 2.41 5.91
# 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:  89.57 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")
  )