# Filter the data for the pitcher Foley
Foley_data <- Mystic612 %>%
  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, 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 Foley")
Detailed Pitch Table for Foley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 93.30 2223.83 233.05 15.35 12.64 StrikeCalled 5.93 1.61 6.97 7.8
Sinker 93.48 2431.64 243.57 16.70 9.36 StrikeCalled 5.99 1.41 7.19 8.1
Slider 87.25 2866.00 182.67 0.16 4.57 BallCalled 5.69 1.62 6.58 6.1
Sinker 92.57 2415.07 241.15 16.89 10.37 BallCalled 5.93 1.47 7.16 8.0
Slider 87.61 2730.94 183.95 0.32 5.69 StrikeSwinging 5.61 1.87 6.47 6.1
Sinker 93.01 2321.11 239.41 13.87 9.22 FoulBallNotFieldable 5.82 1.59 6.85 8.0
Slider 86.90 2804.30 147.59 -4.31 7.87 BallCalled 5.73 1.92 6.47 4.9
Sinker 92.12 2410.11 228.83 12.46 11.99 InPlay 5.74 1.54 6.94 7.6
Sinker 92.37 2423.35 244.22 19.71 10.67 BallinDirt 5.87 1.82 7.11 8.1
Four-Seam 92.05 2398.79 222.44 12.07 14.39 InPlay 5.79 1.61 6.92 7.4
Sinker 93.12 2335.16 237.55 15.94 11.25 FoulBallNotFieldable 5.71 1.91 6.88 7.9
Sinker 92.36 2371.07 235.60 16.55 12.60 BallCalled 5.81 1.79 6.74 7.9
Slider 87.45 2752.74 182.89 0.27 6.62 BallCalled 5.61 2.00 6.57 6.1
Sinker 92.74 2387.94 246.54 18.77 9.41 BallCalled 5.76 1.83 6.69 8.2
Slider 87.99 2626.02 210.03 2.98 6.26 StrikeCalled 5.54 1.94 6.76 7.0
Sinker 92.10 2459.69 234.62 13.60 10.83 FoulBallNotFieldable 5.71 1.71 7.06 7.8
Four-Seam 93.97 2466.95 229.16 14.12 13.28 BallCalled 5.84 1.64 6.96 7.6
Four-Seam 92.71 2395.35 229.10 14.44 13.58 FoulBallNotFieldable 5.90 1.17 7.03 7.6
Slider 88.04 2814.30 201.62 1.49 4.87 StrikeCalled 5.60 1.68 6.60 6.7
Curveball 77.01 2720.82 84.29 -17.43 -0.27 BallCalled 5.54 1.87 6.40 2.8
Four-Seam 93.74 2493.31 222.97 12.08 13.99 FoulBallNotFieldable 5.90 1.46 7.21 7.4
Slider 87.84 2844.57 144.92 -6.95 11.01 InPlay 5.61 1.74 6.57 4.8
Four-Seam 93.22 2554.23 224.08 12.40 14.09 BallCalled 5.77 1.67 6.73 7.5
Slider 87.11 2843.17 161.82 -1.81 6.68 StrikeSwinging 5.49 2.02 6.68 5.4
Sinker 93.52 2539.19 257.52 13.47 4.15 BallinDirt 5.74 1.81 7.11 8.6
Four-Seam 93.02 2460.83 224.52 13.78 15.34 BallCalled 5.77 1.62 6.74 7.5
Sinker 92.40 2381.12 234.30 13.50 10.92 StrikeCalled 5.78 1.72 6.97 7.8
Slider 87.67 2781.95 141.03 -5.35 7.86 BallCalled 5.54 2.00 6.58 4.7
Sinker 91.48 2550.97 241.13 13.79 8.92 BallCalled 5.71 1.72 7.08 8.0
Sinker 91.19 2509.10 229.52 11.72 11.22 FoulBallFieldable 5.66 1.87 7.01 7.7
Cutter 87.62 2749.69 158.17 -3.65 10.20 StrikeCalled 5.58 2.21 6.52 5.3
Sinker 93.78 2420.64 234.38 17.20 13.50 FoulBallNotFieldable 5.84 1.68 7.00 7.8
Slider 86.98 2826.90 171.54 -0.83 6.68 InPlay 5.49 2.09 6.65 5.7
Sinker 93.10 2511.12 234.16 13.94 11.15 InPlay 5.89 1.52 6.98 7.8
# 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\n")
## Total number of pitches thrown:  34
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Foley")
Summary Pitch Table for Foley
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Curveball 2.94% 1 1 0 100.00% 0.00% 77.01 2720.82 -0.27 -17.43 84.29 2.8 5.54 1.87 6.40
Cutter 2.94% 1 0 1 0.00% 100.00% 87.62 2749.69 10.20 -3.65 158.17 5.3 5.58 2.21 6.52
Four-Seam 17.65% 6 3 3 50.00% 50.00% 93.12 2461.58 14.11 13.15 225.38 7.5 5.83 1.53 6.93
Sinker 47.06% 16 4 12 25.00% 75.00% 92.67 2418.19 10.51 15.22 238.47 7.9 5.81 1.69 6.98
Slider 29.41% 10 4 6 40.00% 60.00% 87.48 2789.09 6.81 -1.40 172.81 5.8 5.59 1.89 6.59
# 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("Foley maximum FB velocity: ", max_fb_velocity, "mph\n")
## Foley maximum FB velocity:  93.97 mph
# 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")
  )