# Filter the data for the pitcher Joshua Sibley
Joshua_Sibley_data <- data %>%
  filter(Pitcher == "Sibley, Joshua")

# Create a detailed table for each pitch
detailed_pitch_table <- Joshua_Sibley_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 Joshua Sibley")
Detailed Pitch Table for Joshua Sibley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 76.07 2847.02 308.44 13.23 -8.86 StrikeCalled 5.86 -0.15 5.10 10.3
Cutter 85.22 2330.26 170.88 -1.36 9.80 StrikeSwinging 5.59 -0.42 5.50 5.7
Curveball 76.42 2793.15 329.04 9.94 -15.01 BallCalled 5.95 -0.24 4.99 11.0
Curveball 76.91 2758.68 302.54 12.27 -6.42 StrikeSwinging 5.92 -0.30 5.23 10.1
Sinker 86.00 2287.85 144.26 -6.62 10.33 FoulBallNotFieldable 5.65 -0.54 5.40 4.8
Changeup 81.80 2009.80 101.01 -19.95 5.38 StrikeCalled 5.45 -0.89 5.47 3.4
Curveball 77.50 2869.67 323.26 11.87 -14.20 BallCalled 5.80 -0.26 4.79 10.8
Changeup 82.60 2115.27 121.38 -19.88 13.69 StrikeSwinging 5.51 -0.55 5.31 4.0
Curveball 78.37 2785.28 320.88 9.89 -10.60 StrikeSwinging 5.83 -0.31 5.03 10.7
Curveball 78.46 2983.00 324.72 8.20 -10.14 StrikeSwinging 5.79 -0.39 5.18 10.8
Cutter 86.85 2261.49 146.27 -6.29 10.68 InPlay 5.78 -0.21 5.64 4.9
Slider 85.76 2274.58 128.42 -6.79 6.74 FoulBallNotFieldable 5.70 -0.44 5.24 4.3
Changeup 81.23 2012.10 108.08 -19.51 7.93 BallCalled 5.48 -0.58 5.29 3.6
Sinker 83.96 2203.15 158.34 -4.92 13.71 BallCalled 5.70 -0.33 5.41 5.3
Four-Seam 85.15 2321.92 151.09 -7.80 15.45 BallCalled 5.64 -0.61 5.60 5.0
Four-Seam 86.03 2357.21 156.03 -6.06 14.85 InPlay 5.58 -0.49 5.32 5.2
Curveball 77.97 2770.38 345.35 2.58 -8.43 FoulBallNotFieldable 5.87 -0.18 5.17 11.5
Curveball 77.99 2785.96 327.05 8.30 -11.27 InPlay 5.86 -0.12 5.28 10.9
Curveball 77.63 2863.31 307.15 9.32 -5.48 StrikeCalled 5.85 -0.25 5.03 10.2
Changeup 86.27 2446.35 146.51 -7.41 12.48 FoulBallNotFieldable 5.66 -0.34 5.47 4.9
Curveball 78.32 2827.32 306.48 9.78 -5.63 InPlay 5.89 -0.02 4.84 10.2
Changeup 80.89 1899.12 113.20 -17.64 8.99 StrikeSwinging 5.57 -0.61 5.42 3.8
Changeup 82.29 2012.38 147.98 -9.75 17.08 BallCalled 5.57 -0.59 5.26 4.9
Sinker 84.48 2250.18 149.55 -6.70 12.70 InPlay 5.68 -0.29 5.44 5.0
Curveball 75.63 2569.56 331.65 19.08 -33.80 HitByPitch 5.76 -0.45 5.13 11.1
# 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:  25
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Joshua Sibley")
Summary Pitch Table for Joshua Sibley
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 24.00% 6 2 4 33.33% 66.67% 82.51 2082.50 10.92 -15.69 123.03 4.1 5.54 -0.59 5.37
Curveball 44.00% 11 2 9 18.18% 81.82% 77.39 2804.85 -11.80 10.41 320.60 10.7 5.85 -0.24 5.07
Cutter 8.00% 2 0 2 0.00% 100.00% 86.03 2295.88 10.24 -3.83 158.57 5.3 5.69 -0.32 5.57
Four-Seam 8.00% 2 1 1 50.00% 50.00% 85.59 2339.56 15.15 -6.93 153.56 5.1 5.61 -0.55 5.46
Sinker 12.00% 3 1 2 33.33% 66.67% 84.81 2247.06 12.25 -6.08 150.72 5.0 5.68 -0.39 5.42
Slider 4.00% 1 0 1 0.00% 100.00% 85.76 2274.58 6.74 -6.79 128.42 4.3 5.70 -0.44 5.24
# 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("Joshua Sibley maximum FB velocity: ", max_fb_velocity, "mph\n")
## Joshua Sibley maximum FB velocity:  86.85 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Joshua_Sibley_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 Joshua Sibley",
       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
  labs(title = "Pitch Movement for Joshua Sibley",
       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")
  )