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

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_Foley_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
Slider 77.32 2247.13 259.15 12.72 4.02 BallCalled 5.88 -1.73 4.89 8.6
Sinker 88.67 2195.75 124.59 -19.29 14.64 BallCalled 5.76 -2.06 5.30 4.2
Four-Seam 88.64 2239.53 146.02 -14.90 23.43 BallCalled 6.02 -1.78 5.35 4.9
Sinker 88.37 2184.36 131.22 -15.28 14.69 BallCalled 5.79 -1.78 5.24 4.4
Sinker 87.97 2218.11 134.85 -18.42 19.66 BallCalled 6.01 -1.84 5.16 4.5
Sinker 88.40 2246.08 128.71 -17.58 15.41 FoulBallNotFieldable 5.82 -1.93 5.19 4.3
Changeup 78.13 1502.29 128.18 -13.64 12.29 StrikeSwinging 5.73 -2.03 5.40 4.3
Changeup 81.46 1487.23 140.99 -12.40 16.72 BallCalled 6.00 -1.79 5.48 4.7
Four-Seam 86.82 2152.03 190.09 3.88 23.10 InPlay 6.17 -0.74 5.05 6.3
Curveball 72.98 2404.34 301.76 18.21 -9.37 BallCalled 6.20 -1.54 4.70 10.1
Changeup 87.04 2152.65 117.97 -22.14 12.99 StrikeSwinging 5.75 -1.99 5.16 3.9
Sinker 88.18 2237.10 135.65 -17.51 19.12 BallCalled 5.76 -1.95 5.29 4.5
Four-Seam 87.92 2271.25 138.28 -16.42 19.66 FoulBallNotFieldable 5.91 -1.86 5.17 4.6
Changeup 79.46 1382.00 131.48 -8.12 8.73 BallCalled 5.78 -1.77 5.60 4.4
Sinker 87.41 2255.27 134.16 -17.47 18.26 FoulBallNotFieldable 5.96 -2.07 5.35 4.5
NA 88.75 2158.53 NA NA NA BallCalled 6.05 -1.94 5.15 NA
Four-Seam 87.41 2172.71 149.55 -10.32 18.79 BallCalled 5.79 -2.05 5.38 5.0
# 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:  17
# 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 23.53% 4 2 2 50.00% 50.00% 81.52 1631.04 12.68 -14.07 129.66 4.3 5.82 -1.90 5.41
Curveball 5.88% 1 1 0 100.00% 0.00% 72.98 2404.34 -9.37 18.21 301.76 10.1 6.20 -1.54 4.70
Four-Seam 23.53% 4 2 2 50.00% 50.00% 87.70 2208.88 21.25 -9.44 155.99 5.2 5.97 -1.61 5.24
Sinker 35.29% 6 4 2 66.67% 33.33% 88.17 2222.78 16.96 -17.59 131.53 4.4 5.85 -1.94 5.26
Slider 5.88% 1 1 0 100.00% 0.00% 77.32 2247.13 4.02 12.72 259.15 8.6 5.88 -1.73 4.89
NA 5.88% 1 1 0 100.00% 0.00% 88.75 2158.53 NaN NaN NaN NaN 6.05 -1.94 5.15
# 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:  88.67 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_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")
  )

# 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
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement"),
       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")
  )