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

# Create a detailed table for each pitch
detailed_pitch_table <- 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 Sibley")
Detailed Pitch Table for Sibley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 87.10 2393.02 144.81 -10.52 16.27 StrikeCalled 5.20 -0.82 6.21 4.8
Sinker 86.31 2281.30 137.51 -8.32 10.46 BallCalled 5.15 -0.78 6.21 4.6
Four-Seam 86.25 2276.39 148.14 -8.92 15.90 InPlay 4.98 -0.87 6.48 4.9
Changeup 86.00 2263.65 122.96 -16.04 11.93 BallCalled 4.93 -0.93 6.30 4.1
Changeup 82.23 1946.48 105.49 -18.31 6.62 BallCalled 4.93 -0.95 6.28 3.5
Four-Seam 87.05 2392.65 141.00 -11.87 16.04 FoulBall 5.09 -0.72 6.22 4.7
Curveball 77.25 2882.91 292.87 15.46 -4.89 HitByPitch 5.54 -0.52 5.69 9.8
Curveball 75.54 2825.47 308.60 8.30 -5.11 FoulBall 5.47 -0.71 5.64 10.3
Curveball 76.92 2770.72 318.90 7.65 -7.26 StrikeSwinging 5.45 -0.63 5.76 10.6
Curveball 77.04 2870.02 298.85 14.17 -6.16 StrikeCalled 5.50 -0.48 5.58 10.0
Changeup 84.12 2327.21 124.24 -14.29 10.98 InPlay 5.02 -0.81 6.36 4.1
Curveball 75.89 2836.47 298.79 12.22 -5.07 BallCalled 5.53 -0.38 5.52 10.0
Changeup 84.85 2287.35 128.34 -13.28 11.83 FoulBall 5.14 -0.71 6.24 4.3
Changeup 78.93 1990.71 110.78 -18.95 8.78 BallinDirt 4.95 -1.14 6.30 3.7
Changeup 84.55 2311.97 138.03 -8.93 11.29 BallinDirt 5.23 -0.70 6.11 4.6
Four-Seam 85.28 2348.16 146.67 -9.16 15.19 InPlay 5.26 -0.53 6.17 4.9
Changeup 84.59 2363.87 132.50 -12.48 12.76 BallCalled 5.04 -0.59 6.45 4.4
Curveball 76.61 2892.97 303.29 7.51 -3.40 InPlay 5.52 -0.44 5.50 10.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\\n')
## Total number of pitches thrown:  18 \n\n
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Sibley")
Summary Pitch Table for Sibley
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 38.89% 7 3 4 42.86% 57.14% 83.61 2213.03 10.60 -14.61 123.19 4.1 5.03 -0.83 6.29
Curveball 33.33% 6 1 5 16.67% 83.33% 76.54 2846.43 -5.32 10.88 303.55 10.1 5.50 -0.53 5.62
Four-Seam 22.22% 4 0 4 0.00% 100.00% 86.42 2352.55 15.85 -10.12 145.16 4.8 5.13 -0.74 6.27
Sinker 5.56% 1 1 0 100.00% 0.00% 86.31 2281.30 10.46 -8.32 137.51 4.6 5.15 -0.78 6.21
# 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('Sibley maximum FB velocity: ', max_fb_velocity, '\\n')
## Sibley maximum FB velocity:  87.1 \n
# 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')
  )