# Filter the data for the pitcher Alekson
Alekson_data <- Mystic612 %>%
  filter(Pitcher == "Alekson, Ben")

# Create a detailed table for each pitch
detailed_pitch_table <- Alekson_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 Alekson")
Detailed Pitch Table for Alekson
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 89.63 2168.16 213.48 12.85 20.72 StrikeSwinging 5.75 0.80 6.12 7.1
Four-Seam 90.57 2240.51 217.00 14.92 21.05 StrikeCalled 5.76 0.82 6.16 7.2
Slider 80.78 2137.85 123.65 -3.69 3.78 BallCalled 5.82 0.90 5.71 4.1
Slider 81.70 1996.89 153.35 -2.73 6.88 FoulBallNotFieldable 5.82 0.80 5.84 5.1
Four-Seam 90.81 2133.84 212.19 10.73 18.29 BallCalled 5.86 0.88 5.95 7.1
Changeup 78.89 1499.86 234.56 11.38 9.66 BallCalled 5.86 0.93 6.00 7.8
Four-Seam 89.44 2078.90 209.02 10.83 20.85 BallCalled 5.83 0.68 5.92 7.0
Four-Seam 89.57 2080.14 210.69 11.67 20.97 StrikeCalled 5.79 0.90 6.04 7.0
Changeup 81.12 1259.48 236.60 11.91 9.33 BallCalled 5.77 1.07 5.83 7.9
Four-Seam 88.17 2020.61 217.40 13.57 19.09 FoulBallNotFieldable 5.75 0.89 6.05 7.2
Four-Seam 90.53 2095.56 220.14 14.13 17.96 BallCalled 5.64 1.12 5.90 7.3
Changeup 80.48 1397.57 246.14 17.07 9.09 InPlay 5.71 1.05 5.88 8.2
Four-Seam 90.07 2179.67 200.48 8.71 24.55 StrikeCalled 5.74 0.91 6.01 6.7
Slider 81.83 2037.11 169.06 -1.22 7.80 BallCalled 5.76 0.87 5.95 5.6
Slider 81.33 1936.90 132.96 -6.38 7.21 FoulBallNotFieldable 5.75 0.90 5.86 4.4
Four-Seam 90.52 2132.31 204.73 8.48 19.79 StrikeSwinging 5.78 0.82 6.06 6.8
Four-Seam 91.06 2120.21 208.26 11.02 21.94 FoulBallNotFieldable 5.66 0.94 6.01 6.9
Changeup 83.48 1401.13 251.56 12.27 5.53 InPlay 5.61 1.02 6.00 8.4
Four-Seam 89.46 2044.57 209.91 10.27 19.12 StrikeSwinging 5.81 0.71 5.99 7.0
Changeup 83.02 1808.66 219.39 5.77 8.43 StrikeCalled 5.70 0.83 5.99 7.3
Changeup 82.18 1435.72 252.25 17.11 7.07 StrikeSwinging 5.79 0.78 5.79 8.4
Four-Seam 89.79 2067.74 210.62 11.33 20.40 BallCalled 5.72 0.89 6.14 7.0
Changeup 79.51 1650.81 243.71 16.05 9.58 BallCalled 5.76 0.84 5.85 8.1
Four-Seam 89.30 2080.97 213.92 13.49 21.35 StrikeSwinging 5.78 0.71 6.09 7.1
Sinker 89.65 2111.71 223.77 16.20 18.20 FoulBallNotFieldable 5.79 0.85 5.97 7.5
Changeup 81.77 1582.60 238.82 12.29 9.00 FoulBallFieldable 5.76 0.79 5.89 8.0
Changeup 82.84 1818.29 243.19 16.30 9.67 FoulBallNotFieldable 5.70 0.97 6.03 8.1
Four-Seam 90.92 2108.42 208.33 10.47 20.72 FoulBallFieldable 5.75 0.80 6.12 6.9
Four-Seam 90.64 2140.70 215.07 12.70 19.46 BallCalled 5.74 0.98 6.11 7.2
Four-Seam 89.68 2111.01 220.58 14.25 18.01 InPlay 5.74 0.72 6.07 7.4
Four-Seam 90.97 2122.15 215.22 12.67 19.24 StrikeCalled 5.62 0.87 6.26 7.2
Changeup 80.58 1556.27 232.75 10.19 9.32 BallCalled 5.61 0.89 5.76 7.8
Changeup 81.87 1607.45 248.30 15.46 7.63 StrikeCalled 5.76 0.86 5.92 8.3
Four-Seam 90.87 2134.29 207.67 11.37 23.06 BallCalled 5.72 0.83 6.03 6.9
Four-Seam 90.94 2186.90 214.87 11.86 18.30 StrikeSwinging 5.63 0.98 6.25 7.2
Four-Seam 88.52 2054.12 212.53 12.74 21.38 InPlay 5.63 0.79 6.17 7.1
Slider 80.39 1835.92 139.44 -3.77 6.01 BallCalled 5.69 0.91 5.81 4.6
Sinker 89.62 2138.36 221.02 16.58 20.44 StrikeCalled 5.69 0.73 6.28 7.4
Four-Seam 89.82 2131.61 221.10 13.43 16.72 StrikeSwinging 5.63 0.83 6.00 7.4
Four-Seam 89.90 2132.19 212.45 11.89 20.04 FoulBallNotFieldable 5.77 0.70 6.04 7.1
Changeup 82.12 1524.63 241.05 15.34 9.94 InPlay 5.83 0.73 5.90 8.0
Four-Seam 88.28 2108.01 213.93 12.80 20.45 StrikeSwinging 5.72 0.94 6.11 7.1
Changeup 81.67 1410.19 243.16 13.52 8.33 BallCalled 5.66 1.14 5.87 8.1
Four-Seam 88.48 2116.07 204.04 9.69 23.22 StrikeSwinging 5.68 0.90 5.94 6.8
Changeup 82.11 1439.87 239.05 16.79 11.50 FoulBallFieldable 5.72 0.87 6.07 8.0
Curveball 77.97 1995.00 144.98 -3.92 7.30 StrikeCalled 5.81 0.98 5.78 4.8
Four-Seam 89.52 2156.93 205.28 10.39 23.31 StrikeSwinging 5.75 0.67 6.05 6.8
Four-Seam 90.21 2204.92 202.70 9.53 24.16 StrikeSwinging 5.86 0.85 5.72 6.8
Sinker 89.33 2091.45 216.67 13.42 19.36 FoulBallNotFieldable 5.72 1.00 6.15 7.2
Four-Seam 89.47 2189.54 219.71 14.05 18.31 FoulBallNotFieldable 5.77 0.87 6.05 7.3
Sinker 89.22 2050.39 217.20 14.90 20.87 FoulBallNotFieldable 5.81 0.81 5.93 7.2
Changeup 82.78 1654.55 237.73 15.59 11.33 BallCalled 5.76 0.93 5.78 7.9
Four-Seam 90.65 2163.21 210.74 12.93 23.16 StrikeSwinging 5.77 0.88 5.93 7.0
Four-Seam 89.73 2139.78 218.22 13.77 18.81 InPlay 5.83 0.72 6.07 7.3
Four-Seam 88.93 2093.48 208.66 11.55 22.49 StrikeCalled 5.73 1.00 6.13 7.0
Sinker 89.53 2134.09 219.03 15.08 19.94 FoulBallNotFieldable 5.71 0.91 6.10 7.3
Four-Seam 89.22 2142.69 204.49 10.02 23.41 StrikeSwinging 5.80 0.83 5.88 6.8
Four-Seam 90.35 2212.29 214.72 12.49 19.35 BallCalled 5.72 0.91 5.95 7.2
Four-Seam 89.44 2120.60 201.64 8.62 23.19 StrikeSwinging 5.72 0.92 5.96 6.7
Changeup 82.91 1317.16 240.06 15.38 10.26 BallCalled 5.70 0.97 6.08 8.0
Four-Seam 89.78 2120.11 208.25 10.64 21.22 FoulBallNotFieldable 5.68 0.97 6.08 6.9
Four-Seam 91.20 2129.76 207.48 9.27 19.09 BallCalled 5.74 0.82 6.14 6.9
Four-Seam 88.98 2011.87 212.74 11.09 18.60 InPlay 5.74 0.80 6.12 7.1
Four-Seam 89.43 2064.14 211.42 12.21 21.36 StrikeSwinging 5.73 0.80 6.02 7.0
Changeup 81.90 1423.50 237.84 15.59 11.38 FoulBallNotFieldable 5.78 0.93 5.81 7.9
Sinker 89.40 2100.47 218.83 14.48 19.36 FoulBallNotFieldable 5.77 0.90 5.96 7.3
Sinker 90.27 2055.27 221.42 16.15 19.55 FoulBallNotFieldable 5.80 0.87 6.03 7.4
Changeup 82.74 1387.22 256.58 16.02 5.28 FoulBallNotFieldable 5.73 0.85 5.83 8.6
Changeup 82.44 1489.66 232.13 10.99 10.02 InPlay 5.78 0.95 5.76 7.7
# 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:  69
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Alekson")
Summary Pitch Table for Alekson
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 27.54% 19 7 12 36.84% 63.16% 81.81 1508.66 9.07 13.95 240.78 8.0 5.74 0.92 5.90
Curveball 1.45% 1 0 1 0.00% 100.00% 77.97 1995.00 7.30 -3.92 144.98 4.8 5.81 0.98 5.78
Four-Seam 53.62% 37 8 29 21.62% 78.38% 89.86 2122.65 20.63 11.69 211.34 7.0 5.74 0.85 6.04
Sinker 10.14% 7 0 7 0.00% 100.00% 89.57 2097.39 19.67 15.26 219.71 7.3 5.76 0.87 6.06
Slider 7.25% 5 3 2 60.00% 40.00% 81.21 1988.93 6.34 -3.56 143.69 4.8 5.77 0.88 5.83
# 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("Alekson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Alekson maximum FB velocity:  91.2 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Alekson_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 Alekson",
       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 Alekson",
       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")
  )