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

# 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 Brian Foley")
Detailed Pitch Table for Brian Foley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 94.46 2224.95 240.40 13.41 8.67 BallCalled 5.84 1.47 6.93 8.0
Four-Seam 94.06 2590.78 202.18 5.83 15.35 StrikeCalled 5.86 1.60 6.92 6.7
Sinker 92.88 2347.59 248.05 14.84 7.11 BallCalled 5.73 1.63 7.03 8.3
Four-Seam 92.79 2567.23 201.79 5.27 14.15 StrikeCalled 5.91 1.58 7.04 6.7
Four-Seam 94.40 2403.40 205.47 6.16 13.96 BallCalled 6.00 1.53 7.01 6.8
Four-Seam 94.12 2470.43 193.59 2.82 12.65 InPlay 5.87 1.36 7.10 6.5
Four-Seam 94.22 2312.30 203.39 4.59 11.54 StrikeCalled 6.05 1.37 7.20 6.8
Slider 81.79 2877.71 98.05 -15.13 3.56 BallCalled 5.62 1.61 6.32 3.3
Four-Seam 94.90 2436.92 202.27 5.69 14.90 StrikeCalled 6.05 1.08 7.02 6.7
Slider 79.10 2785.20 89.36 -19.50 1.43 BallCalled 5.60 1.72 6.43 3.0
Four-Seam 94.87 2425.80 181.50 0.46 18.64 InPlay 6.04 1.19 7.10 6.0
Four-Seam 94.19 2394.30 199.01 3.89 12.28 BallCalled 5.97 1.56 7.03 6.6
Four-Seam 93.31 2479.80 214.99 8.84 13.75 StrikeCalled 5.81 1.57 6.86 7.2
Four-Seam 94.54 2505.95 182.21 0.51 14.25 StrikeSwinging 5.99 1.32 7.10 6.1
Four-Seam 96.23 2615.22 195.87 3.82 14.34 FoulBallNotFieldable 6.07 1.30 7.00 6.5
Slider 83.40 2905.80 84.81 -19.60 -0.19 HitByPitch 5.55 1.77 6.34 2.8
Four-Seam 94.31 2472.32 206.51 7.80 16.55 BallCalled 5.90 1.41 7.00 6.9
Four-Seam 92.67 2534.74 200.30 5.82 16.62 BallinDirt 5.80 1.47 7.17 6.7
Sinker 92.25 2402.95 239.00 13.44 9.03 StrikeSwinging 5.77 1.53 7.02 8.0
Sinker 93.94 2321.59 242.83 12.16 7.18 BallCalled 5.74 1.67 6.92 8.1
Four-Seam 92.21 2464.40 185.89 1.37 14.22 InPlay 5.94 1.28 7.07 6.2
Four-Seam 92.54 2429.46 205.31 4.13 9.81 BallCalled 6.05 1.47 7.03 6.8
Four-Seam 92.67 2379.88 211.98 6.54 11.56 BallCalled 6.01 1.40 7.08 7.1
Four-Seam 91.52 2525.53 202.11 5.76 15.21 StrikeSwinging 5.98 1.52 7.28 6.7
Four-Seam 93.97 2551.98 201.24 5.86 16.07 BallCalled 5.92 1.29 7.23 6.7
Four-Seam 92.85 2474.32 202.72 7.14 18.19 StrikeSwinging 5.88 1.57 6.97 6.8
Four-Seam 93.43 2375.12 208.74 6.60 13.11 BallCalled 6.01 1.36 7.19 7.0
Four-Seam 92.41 2421.97 190.75 2.68 15.09 StrikeCalled 6.04 1.39 7.17 6.4
Four-Seam 93.12 2261.11 221.00 11.28 14.13 BallCalled 5.78 1.57 6.96 7.4
Four-Seam 93.37 2441.83 212.08 6.39 11.21 BallCalled 6.09 1.33 7.28 7.1
Four-Seam 92.08 2318.89 195.49 3.67 14.35 InPlay 5.97 1.37 6.98 6.5
# 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:  31
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Brian Foley")
Summary Pitch Table for Brian Foley
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Four-Seam 77.42% 24 9 15 37.50% 62.50% 93.53 2452.24 14.25 5.12 201.10 6.7 5.96 1.41 7.07
Sinker 12.90% 4 3 1 75.00% 25.00% 93.38 2324.27 8.00 13.46 242.57 8.1 5.77 1.57 6.97
Slider 9.68% 3 2 1 66.67% 33.33% 81.43 2856.24 1.60 -18.08 90.74 3.0 5.59 1.70 6.36
# 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("Brian Foley maximum FB velocity: ", max_fb_velocity, "mph\n")
## Brian Foley maximum FB velocity:  96.23 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 Brian Foley",
       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 Brian Foley",
       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")
  )