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

# Create a detailed table for each pitch
detailed_pitch_table <- William_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 William Jaun")
Detailed Pitch Table for William Jaun
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 87.84 2247.29 131.49 -17.58 16.78 BallCalled 5.82 -2.15 6.18 4.4
Sinker 88.79 2262.19 131.01 -17.81 16.65 BallCalled 5.74 -2.25 6.21 4.4
Four-Seam 87.53 2216.64 143.91 -14.08 20.56 StrikeCalled 5.96 -2.10 6.00 4.8
Curveball 73.19 2393.50 282.34 12.53 -0.87 BallCalled 6.07 -1.83 5.59 9.4
Four-Seam 86.77 2228.94 149.15 -12.81 22.72 StrikeCalled 5.98 -2.09 6.23 5.0
Four-Seam 88.43 2226.06 139.61 -15.65 19.62 InPlay 6.02 -1.92 6.17 4.7
Sinker 87.55 2204.91 132.81 -20.06 19.91 StrikeSwinging 5.99 -1.99 6.12 4.4
Changeup 76.36 1434.10 130.24 -11.69 11.33 BallCalled 6.03 -2.00 6.14 4.3
Sinker 88.49 2251.03 135.66 -17.39 19.06 BallCalled 5.93 -2.10 5.99 4.5
Curveball 73.47 2417.02 307.42 9.19 -5.26 BallCalled 6.26 -1.78 5.51 10.2
Sinker 86.83 2201.71 134.63 -16.60 17.65 InPlay 5.92 -2.07 6.07 4.5
Sinker 87.75 2119.89 134.60 -17.75 18.89 BallCalled 6.05 -2.24 5.92 4.5
Sinker 87.07 2150.52 133.72 -17.42 18.11 FoulBallNotFieldable 5.87 -2.29 6.04 4.5
Changeup 77.63 1273.81 130.00 -12.88 12.63 BallCalled 5.82 -2.23 6.18 4.3
Sinker 88.00 2242.48 137.50 -17.85 20.98 FoulBallNotFieldable 5.97 -2.08 5.88 4.6
Curveball 74.90 2418.08 295.60 11.43 -3.42 BallinDirt 6.22 -1.80 5.70 9.9
Four-Seam 89.33 2246.36 148.23 -12.57 21.71 FoulBallNotFieldable 5.84 -2.26 5.97 4.9
Sinker 86.93 2214.41 137.59 -17.15 20.13 InPlay 6.06 -2.14 5.93 4.6
Four-Seam 89.07 2221.92 135.94 -15.73 17.53 BallCalled 5.91 -2.09 6.15 4.5
Four-Seam 88.48 2196.07 136.36 -15.92 17.95 StrikeCalled 5.98 -2.16 6.02 4.5
Four-Seam 88.35 2148.40 137.61 -15.17 17.87 BallCalled 5.94 -2.17 5.93 4.6
Sinker 87.99 2132.89 139.57 -15.59 19.56 StrikeSwinging 5.94 -2.16 5.99 4.7
Sinker 89.21 2197.58 131.72 -16.80 16.16 FoulBallNotFieldable 5.90 -2.09 5.92 4.4
Changeup 79.17 1208.63 103.12 -6.64 3.15 FoulBallNotFieldable 5.84 -2.09 6.32 3.4
Changeup 78.57 1450.91 121.61 -14.88 10.87 InPlay 5.74 -2.04 5.98 4.1
Four-Seam 87.22 2187.67 150.20 -12.05 22.65 StrikeCalled 5.78 -2.37 5.97 5.0
Four-Seam 87.94 2328.48 145.53 -13.41 21.08 StrikeCalled 6.12 -2.07 5.75 4.9
Curveball 74.70 2402.09 286.51 18.87 -3.58 BallCalled 6.14 -1.97 5.74 9.6
Sinker 89.23 2217.27 142.76 -14.42 20.28 StrikeCalled 5.76 -2.35 5.98 4.8
Four-Seam 89.52 2318.34 144.79 -14.07 21.46 BallinDirt 6.04 -2.10 5.91 4.8
Sinker 86.81 1996.02 137.67 -16.12 18.99 InPlay 5.96 -2.07 6.02 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")
## Total number of pitches thrown:  31
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for William Jaun")
Summary Pitch Table for William Jaun
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 12.90% 4 2 2 50.00% 50.00% 77.93 1341.86 9.49 -11.52 121.24 4.0 5.86 -2.09 6.16
Curveball 12.90% 4 3 1 75.00% 25.00% 74.06 2407.67 -3.28 13.00 292.97 9.8 6.17 -1.84 5.64
Four-Seam 32.26% 10 2 8 20.00% 80.00% 88.26 2231.89 20.32 -14.15 143.13 4.8 5.96 -2.13 6.01
Sinker 41.94% 13 4 9 30.77% 69.23% 87.88 2187.55 18.70 -17.12 135.44 4.5 5.92 -2.15 6.02
# 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("William Jaun maximum FB velocity: ", max_fb_velocity, "mph\n")
## William Jaun maximum FB velocity:  89.52 mph
# Prepare data for plotting pitch locations
pitch_location_data <- William_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) +
  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 William Jaun",
       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")
  )

ggplot(detailed_pitch_table, aes(x = HorizontalBreak, y = InducedVerticalBreak, color = AutoPitchType)) +
  geom_point(size = 3) + # Increase point size
  labs(title = "Pitch Movement for William 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")
  )