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

# Create a detailed table for each pitch
detailed_pitch_table <- Sibley_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 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 Sibley")
Detailed Pitch Table for Sibley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ClockTilt
Sinker 84.17 2198.25 148.19 -5.01 9.37 BallCalled 4.9
Changeup 83.94 2271.44 133.49 -8.29 9.17 FoulBallNotFieldable 4.4
Curveball 75.52 2847.29 281.66 10.17 -0.49 FoulBallNotFieldable 9.4
Changeup 80.91 1710.38 110.63 -15.05 7.07 InPlay 3.7
Curveball 74.80 2741.84 287.15 9.59 -1.42 BallCalled 9.6
Curveball 75.69 2795.27 301.07 10.58 -4.80 StrikeSwinging 10.0
Changeup 84.38 2360.13 150.06 -7.87 14.86 FoulBallNotFieldable 5.0
Curveball 75.33 2746.61 313.83 9.52 -7.28 BallCalled 10.5
Four-Seam 83.66 2368.68 163.77 -5.51 20.31 StrikeCalled 5.5
Four-Seam 82.83 2262.12 160.53 -6.48 19.70 StrikeCalled 5.4
Curveball 75.55 2765.93 261.70 13.81 3.77 InPlay 8.7
Four-Seam 82.87 2267.07 143.75 -10.68 15.98 StrikeSwinging 4.8
Changeup 82.60 2212.83 106.65 -16.59 6.38 BallCalled 3.6
Curveball 75.56 2682.68 247.81 7.48 4.64 StrikeSwinging 8.3
Curveball 75.44 2785.61 258.54 9.33 3.45 StrikeSwinging 8.6
Changeup 84.56 2276.86 121.35 -12.64 9.03 FoulBallNotFieldable 4.0
Changeup 81.27 1899.43 97.52 -17.98 3.83 StrikeSwinging 3.3
Changeup 81.92 1910.19 117.43 -15.85 9.74 BallCalled 3.9
Four-Seam 84.00 2257.55 153.92 -8.15 17.93 BallCalled 5.1
Changeup 84.16 2284.79 126.13 -10.32 8.90 FoulBallNotFieldable 4.2
Changeup 81.32 1905.57 126.63 -13.70 11.76 StrikeCalled 4.2
Curveball 75.88 2720.01 291.58 9.09 -1.94 StrikeCalled 9.7
Sinker 84.89 2280.86 146.31 -6.65 11.33 FoulBallNotFieldable 4.9
Sinker 85.88 2299.79 142.39 -8.71 12.62 FoulBallNotFieldable 4.7
Changeup 82.67 1880.01 132.20 -13.29 13.56 InPlay 4.4
# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    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
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Sibley")
Summary Pitch Table for Sibley
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 10 2 8 20.00% 80.00% 82.77 2071.16 9.43 -13.16 122.21 4.1
Curveball 8 2 6 25.00% 75.00% 75.47 2760.66 -0.51 9.95 280.42 9.3
Four-Seam 4 1 3 25.00% 75.00% 83.34 2288.86 18.48 -7.70 155.49 5.2
Sinker 3 1 2 33.33% 66.67% 84.98 2259.63 11.11 -6.79 145.63 4.8
# Prepare data for plotting pitch locations
pitch_location_data <- 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) + # 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 Sibley",
       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 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")
  )