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

# Create a detailed table for each pitch
detailed_pitch_table <- Foley_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall, RelHeight, RelSide) %>%
  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),
    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 Foley")
Detailed Pitch Table for Foley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide ClockTilt
Four-Seam 92.48 2611.35 173.25 -1.62 14.67 BallCalled 6.30 1.69 5.8
Four-Seam 92.71 2438.08 226.98 13.55 13.72 StrikeSwinging 6.01 1.93 7.6
Four-Seam 93.00 2538.65 185.70 1.43 15.25 StrikeSwinging 6.25 1.58 6.2
Four-Seam 94.30 2511.29 199.90 5.60 16.41 InPlay 6.29 1.46 6.7
Sinker 93.71 2455.64 232.77 14.81 12.32 StrikeCalled 6.12 1.78 7.8
Cutter 89.72 2860.65 191.25 1.54 8.76 FoulBallNotFieldable 5.95 2.05 6.4
Slider 88.28 2766.18 153.67 -1.72 4.57 BallCalled 6.05 1.84 5.1
Four-Seam 93.67 2478.08 230.89 14.44 12.75 BallCalled 6.15 1.56 7.7
Four-Seam 94.19 2675.49 207.39 8.02 16.40 StrikeCalled 6.28 1.45 6.9
Four-Seam 92.67 2499.69 216.17 10.88 15.95 StrikeCalled 6.23 1.49 7.2
Cutter 89.07 2891.73 168.04 -0.93 5.47 BallCalled 5.94 1.88 5.6
Four-Seam 92.79 2579.72 222.89 13.84 15.99 BallCalled 6.18 1.47 7.4
Four-Seam 92.11 2510.65 185.18 1.40 16.49 BallCalled 6.22 1.52 6.2
Four-Seam 92.00 2500.82 187.68 2.12 16.83 StrikeSwinging 6.25 1.55 6.3
Sinker 92.62 2532.44 243.19 15.26 8.73 InPlay 6.00 1.75 8.1
Sinker 92.11 2461.44 227.79 11.37 11.30 FoulBallNotFieldable 5.95 1.93 7.6
Slider 80.29 2902.30 74.61 -21.77 -4.46 BallCalled 5.76 1.93 2.5
Four-Seam 93.98 2496.89 223.43 11.03 12.65 InPlay 6.11 1.63 7.4
Four-Seam 90.36 2475.72 202.55 4.66 12.36 BallCalled 6.26 1.65 6.8
Four-Seam 90.23 2484.32 218.91 10.95 14.71 StrikeCalled 6.04 1.95 7.3
Four-Seam 91.50 2451.31 229.54 13.67 12.79 StrikeSwinging 6.10 1.73 7.7
Slider 87.34 2807.98 167.52 -1.15 6.40 InPlay 6.03 2.11 5.6
Four-Seam 90.11 2447.55 191.07 3.54 19.27 BallCalled 6.35 1.65 6.4
Four-Seam 90.76 2524.26 207.46 10.28 21.00 StrikeSwinging 6.19 1.57 6.9
Four-Seam 91.90 2399.93 217.41 12.32 17.29 BallCalled 6.12 1.58 7.2
Sinker 92.72 2420.21 225.77 16.76 17.48 BallCalled 6.05 1.66 7.5
Four-Seam 92.35 2511.41 229.44 16.25 15.05 BallCalled 6.10 1.66 7.6
Slider 88.43 2681.68 210.40 2.44 5.33 BallCalled 5.97 1.98 7.0
Four-Seam 91.13 2450.00 220.20 8.78 11.52 InPlay 6.19 1.64 7.3
Four-Seam 92.31 2568.79 197.02 4.54 15.84 BallCalled 6.23 1.38 6.6
Slider 86.32 2925.34 171.11 -0.61 5.11 BallCalled 6.07 1.99 5.7
Sinker 91.69 2468.35 237.86 14.91 10.43 BallCalled 6.03 1.68 7.9
Four-Seam 91.48 2389.49 220.02 12.60 16.09 StrikeCalled 6.04 1.91 7.3
Sinker 90.80 2363.42 236.29 15.70 11.70 FoulBallNotFieldable 6.01 1.86 7.9
Four-Seam 92.88 2554.02 220.77 11.59 14.43 FoulBallNotFieldable 6.15 1.62 7.4
Sinker 92.35 2497.78 237.87 13.50 9.49 InPlay 5.99 1.63 7.9
# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    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)
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Foley")
Summary Pitch Table for Foley
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide
Cutter 2 1 1 50.00% 50.00% 89.39 2876.19 7.12 0.30 179.64 6.0 5.94 1.96
Four-Seam 22 9 13 40.91% 59.09% 92.22 2504.43 15.34 8.63 209.72 7.0 6.18 1.62
Sinker 7 2 5 28.57% 71.43% 92.29 2457.04 11.64 14.62 234.51 7.8 6.02 1.76
Slider 5 4 1 80.00% 20.00% 86.13 2816.70 3.39 -4.56 155.46 5.2 5.98 1.97
# Prepare data for plotting pitch locations
pitch_location_data <- 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) + # Increase point size
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + # Red box
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + # Black box
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) + # Gray box
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) + # Adjust ratio to shrink vertical distance
  labs(title = "Pitch Locations for Foley",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each pitch type
  geom_text(aes(x = -1.75, y = 5, label = "RHH"), color = "black", size = 3, hjust = 0) + # Label for RHH
  geom_text(aes(x = 1.75, y = 5, label = "LHH"), color = "black", size = 3, hjust = 1) + # Label for LHH
  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 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")
  )