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

# 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 Yoon Chae")
Detailed Pitch Table for Yoon Chae
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 75.82 1866.30 226.63 15.38 16.23 StrikeSwinging 5.60 0.88 5.55 7.6
Four-Seam 84.35 2107.44 207.42 12.68 25.95 BallCalled 5.62 0.90 5.97 6.9
Changeup 75.71 1805.66 227.84 14.81 15.09 InPlay 5.55 1.29 5.44 7.6
Curveball 73.10 1709.33 134.42 -7.15 8.89 StrikeCalled 5.70 0.73 5.18 4.5
Curveball 68.69 1767.08 68.98 -11.23 -1.81 InPlay 3.81 2.06 4.92 2.3
Four-Seam 82.88 2160.96 205.50 10.05 22.57 BallCalled 5.76 0.51 5.84 6.8
Curveball 73.36 1903.76 90.86 -9.38 1.99 InPlay 5.59 0.72 5.21 3.0
Four-Seam 83.79 2161.84 205.89 10.14 22.29 BallCalled 5.71 0.69 5.82 6.9
Curveball 73.53 1833.99 69.79 -9.58 -1.36 StrikeCalled 5.55 0.77 5.43 2.3
Four-Seam 84.18 2245.12 203.68 8.99 21.98 BallCalled 5.69 0.90 5.91 6.8
Curveball 73.87 1771.06 91.09 -10.79 2.23 BallinDirt 5.67 0.71 5.60 3.0
Four-Seam 84.10 2192.78 197.44 7.26 24.54 BallCalled 5.68 0.59 6.12 6.6
Four-Seam 83.60 2172.71 201.61 9.18 24.67 BallCalled 5.60 0.78 6.03 6.7
Four-Seam 83.95 2272.01 212.00 13.19 22.61 FoulBallNotFieldable 5.68 0.73 6.03 7.1
Curveball 73.63 1677.14 84.35 -4.89 1.72 StrikeSwinging 5.62 0.81 5.46 2.8
Four-Seam 83.86 2172.16 194.20 5.70 24.08 HitByPitch 5.72 0.86 6.10 6.5
Curveball 72.08 1841.71 238.09 14.02 10.76 BallinDirt 5.50 1.03 5.38 7.9
Changeup 82.93 2026.58 210.04 9.89 18.60 FoulBallNotFieldable 5.62 0.88 6.04 7.0
Changeup 75.66 1665.37 240.96 11.63 8.10 StrikeSwinging 5.55 1.02 5.71 8.0
Changeup 76.50 1654.27 235.91 11.95 9.63 InPlay 5.56 1.00 5.63 7.9
Four-Seam 83.81 2182.87 210.64 12.44 22.39 InPlay 5.71 0.70 5.87 7.0
Curveball 74.65 1929.77 35.09 -4.46 -4.15 StrikeCalled 5.50 0.82 5.32 1.2
Changeup 75.94 1809.18 227.40 11.61 12.27 FoulBallNotFieldable 5.55 1.01 5.71 7.6
Changeup 82.74 2001.33 236.06 15.91 12.30 BallCalled 4.55 1.94 5.66 7.9
Changeup 75.73 1641.25 228.42 11.77 12.14 InPlay 5.49 0.98 5.63 7.6
Changeup 75.89 1753.79 254.68 12.81 5.29 BallCalled 5.48 0.95 5.49 8.5
Four-Seam 84.37 2090.26 208.81 11.13 21.72 StrikeCalled 5.68 0.86 5.92 7.0
Curveball 75.14 1864.04 110.20 -11.21 6.08 StrikeCalled 5.53 0.83 5.25 3.7
Changeup 76.84 1788.62 229.89 13.76 13.31 InPlay 5.40 1.10 5.71 7.7
Curveball 73.30 1816.09 131.73 -5.76 7.11 StrikeCalled 5.53 1.07 5.29 4.4
Four-Seam 83.29 2176.05 212.80 12.76 21.33 BallCalled 5.58 0.94 5.93 7.1
Curveball 74.57 1821.52 122.59 -6.93 6.12 FoulBallNotFieldable 5.48 0.95 5.40 4.1
Changeup 75.53 1681.43 242.39 11.20 7.56 BallCalled 5.48 1.31 5.72 8.1
Four-Seam 84.82 2243.61 207.45 10.28 21.12 BallCalled 5.59 0.80 6.14 6.9
Curveball 73.94 1890.49 106.73 -7.25 4.09 InPlay 5.49 0.91 5.30 3.6
Four-Seam 84.94 2146.71 202.70 8.81 22.49 StrikeCalled 5.62 0.65 5.94 6.8
Curveball 76.42 1979.69 151.82 -3.31 7.94 BallCalled 5.42 0.88 5.24 5.1
Curveball 75.89 1853.08 146.71 -3.53 7.22 StrikeCalled 5.49 0.95 5.26 4.9
Curveball 77.14 1998.94 57.73 -4.44 -0.78 InPlay 5.48 0.85 5.34 1.9
# 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:  39
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Yoon Chae")
Summary Pitch Table for Yoon Chae
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 28.21% 11 3 8 27.27% 72.73% 77.21 1790.34 11.87 12.79 232.75 7.8 5.44 1.12 5.66
Curveball 38.46% 15 3 12 20.00% 80.00% 73.95 1843.85 3.74 -5.73 109.35 3.6 5.42 0.94 5.31
Four-Seam 33.33% 13 8 5 61.54% 38.46% 84.00 2178.81 22.90 10.20 205.40 6.9 5.66 0.76 5.97
# 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("Yoon Chae maximum FB velocity: ", max_fb_velocity, "mph\n")
## Yoon Chae maximum FB velocity:  84.94 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 Yoon Chae",
       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, 26)) +  # 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")
  )