# Filter the data for the pitcher Jaun
Jaun_data <- Danbury618 %>%
  filter(Pitcher == "Jaun, William")

# Create a detailed table for each pitch
detailed_pitch_table <- Jaun_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 Jaun")
Detailed Pitch Table for Jaun
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 87.87 2191.70 141.47 -15.27 20.60 FoulBall 5.65 -1.96 5.94 4.7
Four-Seam 89.42 2285.79 143.03 -15.13 21.59 BallCalled 5.60 -1.93 6.08 4.8
Changeup 76.47 1293.55 132.81 -15.12 15.76 BallCalled 5.35 -2.07 6.19 4.4
Sinker 86.15 2203.14 140.88 -17.21 22.68 FoulBall 5.52 -1.94 5.97 4.7
Changeup 78.22 1462.16 135.25 -10.28 11.88 BallCalled 5.32 -2.00 6.38 4.5
Changeup 76.76 1382.35 135.25 -11.69 13.34 StrikeSwinging 5.39 -1.92 6.28 4.5
Sinker 87.13 2286.59 134.48 -18.99 19.94 StrikeCalled 5.53 -1.95 5.98 4.5
Changeup 76.92 1308.17 133.41 -11.42 12.33 StrikeSwinging 5.52 -1.94 6.13 4.4
Four-Seam 88.92 2356.70 139.63 -17.14 21.38 StrikeSwinging 5.57 -1.89 5.98 4.7
Sinker 86.40 2294.34 133.50 -18.29 18.70 FoulBall 5.52 -2.01 5.93 4.4
Curveball 72.74 2435.30 283.05 13.79 -1.21 FoulBall 5.73 -1.84 5.41 9.4
Four-Seam 88.42 2394.67 138.00 -17.89 21.19 BallCalled 5.59 -2.02 5.93 4.6
Curveball 74.04 2398.97 303.39 14.91 -7.98 StrikeSwinging 5.63 -1.72 5.29 10.1
Curveball 73.99 2446.46 310.75 14.69 -10.81 InPlay 5.62 -1.75 5.01 10.4
Four-Seam 88.74 2371.48 134.62 -18.82 19.80 BallCalled 5.52 -2.02 6.35 4.5
Four-Seam 87.59 2333.76 141.73 -16.31 21.93 StrikeSwinging 5.65 -1.83 6.07 4.7
Four-Seam 88.54 2363.71 140.49 -17.31 22.25 BallCalled 5.54 -1.87 6.20 4.7
Curveball 74.67 2480.33 300.90 14.87 -7.16 StrikeCalled 5.66 -1.68 5.74 10.0
Four-Seam 89.02 2389.54 145.89 -15.06 23.52 FoulBall 5.75 -1.75 6.21 4.9
Four-Seam 89.00 2236.01 135.50 -17.24 18.93 BallCalled 5.62 -1.92 6.22 4.5
Curveball 76.00 2428.20 300.01 17.50 -8.33 BallinDirt 5.58 -1.62 5.63 10.0
Sinker 86.66 2167.92 139.91 -17.37 22.13 BallCalled 5.60 -1.95 6.18 4.7
Sinker 87.45 2290.59 132.68 -17.58 17.59 BallCalled 5.50 -1.97 6.24 4.4
Four-Seam 87.21 2275.17 140.86 -16.70 21.92 BallCalled 5.37 -1.99 6.41 4.7
Sinker 85.91 2155.57 135.33 -16.59 18.24 StrikeCalled 5.46 -2.02 6.15 4.5
Sinker 86.06 2146.12 133.35 -17.98 18.37 InPlay 5.45 -1.95 6.07 4.4
Sinker 87.59 2193.81 136.26 -17.92 20.11 StrikeCalled 5.45 -2.08 6.15 4.5
Four-Seam 89.53 2308.67 137.70 -17.51 20.57 InPlay 5.53 -1.97 6.23 4.6
# 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:  28 \n\n
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Jaun")
Summary Pitch Table for Jaun
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 14.29% 4 2 2 50.00% 50.00% 77.09 1361.56 13.33 -12.13 134.18 4.4 5.39 -1.98 6.24
Curveball 17.86% 5 0 5 0.00% 100.00% 74.29 2437.85 -7.10 15.15 299.62 10.0 5.64 -1.72 5.42
Four-Seam 39.29% 11 6 5 54.55% 45.45% 88.57 2318.84 21.24 -16.76 139.90 4.7 5.58 -1.92 6.15
Sinker 28.57% 8 2 6 25.00% 75.00% 86.67 2217.26 19.72 -17.74 135.80 4.5 5.50 -1.98 6.08
# 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('Jaun maximum FB velocity: ', max_fb_velocity, '\\n')
## Jaun maximum FB velocity:  89.53 \n
# Prepare data for plotting pitch locations
pitch_location_data <- Jaun_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 Jaun',
       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 Jaun',
       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')
  )