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

# 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 Ben Alekson")
Detailed Pitch Table for Ben Alekson
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 89.81 2209.22 226.40 16.13 16.67 StrikeSwinging 5.97 0.99 4.87 7.5
Sinker 88.30 2141.08 228.14 16.50 16.13 FoulBallNotFieldable 5.83 1.17 4.84 7.6
Changeup 83.46 1678.83 249.14 15.33 7.11 FoulBallNotFieldable 5.88 1.19 4.82 8.3
Four-Seam 89.76 2213.55 217.45 14.85 20.67 BallCalled 5.94 1.05 4.87 7.2
Changeup 82.32 1580.74 254.99 12.77 4.76 InPlay 5.86 1.16 4.69 8.5
Four-Seam 90.20 2255.82 220.61 16.02 19.98 BallCalled 5.90 0.99 4.89 7.4
Sinker 89.94 2274.95 223.72 16.34 18.33 FoulBallNotFieldable 5.92 0.89 5.01 7.5
Changeup 82.95 1683.64 253.48 12.90 5.12 StrikeSwinging 5.93 1.08 4.90 8.4
Sinker 89.41 2193.80 225.08 14.94 16.07 StrikeSwinging 6.04 1.13 4.97 7.5
Sinker 90.24 2165.02 222.96 15.36 17.65 FoulBallNotFieldable 6.01 0.79 4.88 7.4
Four-Seam 90.56 2200.95 222.14 15.67 18.49 BallCalled 6.01 0.87 4.75 7.4
Four-Seam 90.52 2227.10 220.85 14.63 18.08 StrikeCalled 5.98 0.89 4.88 7.4
Four-Seam 90.54 2211.01 220.18 15.57 19.63 FoulBallNotFieldable 6.04 0.70 4.70 7.3
Four-Seam 90.25 2234.60 201.26 7.93 21.54 BallCalled 6.04 0.76 4.96 6.7
Changeup 83.70 1687.56 262.48 18.91 3.71 BallCalled 6.00 0.91 4.78 8.7
Four-Seam 90.48 2156.43 208.87 10.51 20.20 BallCalled 5.97 0.85 4.80 7.0
Four-Seam 89.37 2131.18 202.97 8.26 20.64 StrikeSwinging 5.96 0.88 5.03 6.8
Sinker 89.00 2111.69 229.81 17.48 15.91 FoulBallNotFieldable 5.98 0.87 5.02 7.7
Changeup 83.15 1381.76 261.35 11.27 2.99 InPlay 5.96 1.04 5.00 8.7
Sinker 88.66 2173.39 230.37 16.63 15.03 FoulBallNotFieldable 5.87 0.99 5.12 7.7
Changeup 82.09 1782.29 253.27 13.14 5.29 InPlay 5.79 1.02 4.86 8.4
Sinker 87.92 2131.45 220.86 14.53 18.00 StrikeCalled 5.88 0.97 4.96 7.4
Four-Seam 88.37 2183.94 221.27 14.61 17.82 StrikeCalled 5.94 0.86 5.03 7.4
Sinker 88.81 2183.88 225.91 15.88 16.54 InPlay 5.87 0.87 5.08 7.5
Sinker 90.58 2221.69 224.02 15.09 16.74 BallCalled 5.97 0.88 4.87 7.5
Four-Seam 88.96 2163.61 211.22 10.78 18.94 BallCalled 5.95 0.89 5.11 7.0
Changeup 82.49 2067.39 245.52 7.31 4.71 StrikeSwinging 5.96 0.93 4.91 8.2
Slider 82.29 2005.75 183.08 0.25 6.31 StrikeSwinging 5.92 0.94 4.83 6.1
Sinker 86.86 2096.10 226.25 20.18 20.69 BallCalled 6.00 0.78 5.03 7.5
Four-Seam 86.57 2096.27 217.82 14.17 19.57 BallCalled 6.06 0.77 4.99 7.3
Four-Seam 87.12 2096.25 210.56 11.89 21.43 FoulBallNotFieldable 5.98 0.78 5.06 7.0
Sinker 87.93 2082.23 220.04 14.80 18.88 BallCalled 6.00 0.70 5.12 7.3
Four-Seam 86.98 2080.09 221.09 14.86 18.31 BallCalled 6.01 0.79 5.12 7.4
Four-Seam 87.57 2107.69 216.97 13.89 19.80 StrikeCalled 5.89 0.98 5.11 7.2
Sinker 87.88 2141.03 228.77 18.07 17.14 BallCalled 5.87 0.96 5.09 7.6
Sinker 87.77 2108.56 221.27 14.73 18.09 BallCalled 5.89 0.89 5.10 7.4
Four-Seam 87.15 2086.23 217.44 13.30 18.67 StrikeSwinging 5.91 0.87 5.14 7.2
Sinker 88.39 2132.30 225.57 14.89 15.87 InPlay 6.00 0.82 5.08 7.5
Four-Seam 87.99 2141.20 220.01 15.31 19.56 StrikeCalled 5.95 0.87 5.06 7.3
Changeup 80.15 1430.34 235.33 12.12 9.78 BallCalled 5.91 1.08 5.13 7.8
Sinker 88.24 2134.60 227.85 16.94 16.68 FoulBallNotFieldable 5.83 0.81 5.11 7.6
Sinker 88.74 2197.78 235.04 16.52 12.90 BallCalled 5.89 1.00 4.88 7.8
Changeup 81.39 1469.83 257.10 12.84 4.31 InPlay 5.91 0.93 4.94 8.6
Four-Seam 87.21 2098.62 209.61 11.25 21.13 InPlay 5.98 0.97 5.07 7.0
Slider 79.57 2135.85 117.60 -1.72 2.44 BallCalled 5.86 1.04 4.81 3.9
Sinker 87.95 2108.77 220.02 14.89 19.08 BallCalled 5.95 0.83 5.14 7.3
Sinker 86.94 2089.78 223.81 13.70 15.65 BallCalled 6.00 0.83 5.05 7.5
Sinker 87.22 2110.54 224.18 14.77 16.55 StrikeCalled 5.94 0.74 5.18 7.5
Sinker 87.33 2142.47 221.64 15.39 18.61 InPlay 5.99 0.82 5.19 7.4
Four-Seam 88.99 2207.04 214.54 13.01 20.19 BallCalled 5.99 0.82 5.05 7.2
Slider 78.51 2167.42 119.68 -1.93 2.86 StrikeSwinging 5.93 0.97 4.93 4.0
Sinker 88.86 2224.55 225.57 16.70 17.67 FoulBallNotFieldable 5.94 0.86 5.16 7.5
Slider 81.72 2082.36 142.11 -4.74 7.92 FoulBallNotFieldable 5.88 0.95 4.93 4.7
Four-Seam 88.08 2128.55 214.88 13.21 20.26 BallCalled 5.98 0.78 5.07 7.2
Four-Seam 89.50 2168.21 209.64 11.69 21.82 StrikeCalled 5.96 0.73 4.96 7.0
# 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:  55
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Ben Alekson")
Summary Pitch Table for Ben Alekson
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 16.36% 9 2 7 22.22% 77.78% 82.41 1640.26 5.31 12.95 252.52 8.4 5.91 1.04 4.89
Four-Seam 36.36% 20 10 10 50.00% 50.00% 88.81 2159.42 19.84 13.07 214.97 7.2 5.97 0.86 4.98
Sinker 40.00% 22 8 14 36.36% 63.64% 88.49 2153.40 17.04 15.93 225.33 7.5 5.94 0.89 5.03
Slider 7.27% 4 1 3 25.00% 75.00% 80.52 2097.85 4.88 -2.04 140.62 4.7 5.90 0.98 4.88
# 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("Ben Alekson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Ben Alekson maximum FB velocity:  90.58 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 Ben Alekson",
       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, 25)) +  # 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")
  )