# 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
Sinker 88.28 2145.53 135.00 -14.36 15.50 InPlay 5.67 -2.04 5.34 4.5
Sinker 88.71 2132.93 135.08 -15.73 16.89 FoulBallNotFieldable 5.81 -2.12 5.17 4.5
Curveball 77.12 1525.46 6.92 -3.03 -23.95 InPlay 3.43 -1.65 5.86 0.2
Sinker 88.60 2340.19 113.17 -23.10 11.24 BallCalled 5.70 -2.17 5.17 3.8
Curveball 74.56 2283.19 280.79 23.71 -2.81 StrikeCalled 5.82 -1.26 4.98 9.4
Slider 76.00 2414.89 277.63 14.39 -0.24 FoulBallNotFieldable 6.01 -1.73 5.08 9.3
Four-Seam 90.34 2379.17 140.12 -16.03 20.38 FoulBallNotFieldable 5.87 -2.03 5.21 4.7
Curveball 74.32 2351.82 293.23 16.01 -5.21 InPlay 5.93 -1.58 4.54 9.8
Four-Seam 85.89 2261.31 148.05 -11.15 19.17 FoulBallNotFieldable 5.76 -1.87 5.06 4.9
Slider 76.50 1491.06 327.20 1.31 -0.64 FoulBallNotFieldable 4.91 -1.20 5.26 10.9
Four-Seam 87.06 2149.00 154.18 -7.70 17.14 FoulBallNotFieldable 5.73 -1.86 5.28 5.1
Curveball 73.51 2383.30 316.35 12.56 -11.28 BallCalled 6.12 -1.51 4.35 10.5
Sinker 88.89 2197.15 132.41 -17.05 16.82 InPlay 5.81 -1.86 5.36 4.4
Sinker 87.05 2185.81 139.78 -15.01 19.10 BallCalled 5.83 -2.02 5.16 4.7
Changeup 87.05 2264.85 124.29 -18.83 14.15 FoulBallNotFieldable 5.69 -2.02 5.12 4.1
Changeup 79.69 1506.97 107.51 -19.42 7.69 BallCalled 5.57 -2.15 5.26 3.6
NA 86.91 2267.60 NA NA NA BallCalled 6.13 -1.94 5.16 NA
Sinker 86.12 2174.31 138.45 -16.15 19.60 InPlay 5.90 -1.99 5.26 4.6
Curveball 71.96 2346.99 292.47 26.92 -9.21 StrikeCalled 5.52 -1.32 4.66 9.7
NA 87.07 2222.22 NA NA NA BallCalled 5.95 -2.04 5.25 NA
Curveball 73.89 2436.74 304.19 14.84 -8.29 BallCalled 5.85 -1.65 4.69 10.1
Changeup 87.19 2279.48 124.94 -19.77 15.11 BallCalled 5.92 -2.12 5.25 4.2
Sinker 87.17 2287.20 135.35 -13.90 15.39 InPlay 5.77 -2.03 5.29 4.5
NA 83.97 2127.11 NA NA NA BallCalled 6.19 -1.81 5.07 NA
NA 85.99 2192.45 NA NA NA BallCalled 6.16 -1.80 5.15 NA
Sinker 85.09 2238.08 136.01 -14.82 16.54 StrikeSwinging 5.93 -1.90 5.21 4.5
Changeup 77.38 1485.58 129.26 -10.56 10.12 BallCalled 5.68 -1.87 5.21 4.3
Changeup 85.13 2199.23 131.81 -17.49 16.87 StrikeSwinging 5.79 -1.91 5.27 4.4
Four-Seam 88.59 2288.62 132.54 -18.18 17.88 FoulBallNotFieldable 5.78 -2.01 5.23 4.4
Sinker 87.52 2295.03 129.79 -17.33 15.83 FoulBallNotFieldable 5.87 -1.68 4.96 4.3
Curveball 72.92 2271.91 304.07 24.59 -14.86 StrikeCalled 5.48 -0.89 4.81 10.1
Sinker 86.78 2094.56 132.64 -15.56 15.51 StrikeCalled 5.79 -2.04 5.29 4.4
Slider 75.56 2321.14 287.66 9.86 -1.62 StrikeCalled 5.84 -1.75 5.12 9.6
Four-Seam 88.62 2327.38 139.76 -13.75 17.45 BallCalled 5.86 -1.83 5.38 4.7
Curveball 74.47 2297.65 314.20 11.26 -9.26 BallCalled 6.04 -1.43 4.55 10.5
Four-Seam 87.33 2166.35 147.82 -10.21 17.39 FoulBallNotFieldable 5.92 -1.64 5.44 4.9
Four-Seam 88.28 2239.83 148.05 -9.74 16.80 BallCalled 5.71 -1.80 5.44 4.9
Sinker 87.34 2318.93 136.39 -16.62 18.75 FoulBallNotFieldable 5.76 -1.86 5.53 4.5
NA 72.84 2281.94 NA NA NA BallCalled 6.13 -1.64 4.80 NA
Sinker 86.40 2237.04 134.51 -16.35 17.40 StrikeCalled 5.84 -1.99 5.19 4.5
Sinker 87.47 2314.26 134.69 -9.88 11.02 StrikeSwinging 5.49 -1.54 5.22 4.5
Four-Seam 88.87 2357.76 141.16 -15.84 20.98 BallCalled 6.00 -1.87 5.36 4.7
Sinker 88.64 2322.97 126.38 -18.28 14.73 FoulBallNotFieldable 5.64 -2.11 5.36 4.2
Changeup 77.23 1507.67 114.69 -13.83 8.05 BallCalled 5.59 -2.16 5.30 3.8
Sinker 86.63 2072.41 137.01 -13.26 15.51 StrikeSwinging 5.73 -1.88 5.05 4.6
Changeup 84.76 2037.45 131.60 -16.27 15.69 BallCalled 5.71 -1.97 5.19 4.4
Sinker 86.39 2142.19 126.17 -17.37 13.90 BallCalled 5.72 -2.03 5.31 4.2
Four-Seam 86.63 2254.20 147.90 -11.05 18.87 BallCalled 5.73 -2.06 5.43 4.9
Four-Seam 85.64 2111.36 146.06 -12.69 20.15 StrikeCalled 5.82 -1.94 5.22 4.9
Changeup 85.48 2095.73 130.06 -16.11 14.83 InPlay 5.77 -2.03 5.25 4.3
# 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:  50
# 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 16.00% 8 5 3 62.50% 37.50% 82.99 1922.12 12.81 -16.54 124.27 4.1 5.72 -2.03 5.23
Curveball 16.00% 8 3 5 37.50% 62.50% 74.09 2237.13 -10.61 15.86 264.03 8.8 5.52 -1.41 4.80
Four-Seam 20.00% 10 4 6 40.00% 60.00% 87.72 2253.50 18.62 -12.63 144.56 4.8 5.82 -1.89 5.30
Sinker 32.00% 16 3 13 18.75% 81.25% 87.32 2218.66 15.86 -15.92 132.68 4.4 5.77 -1.95 5.24
Slider 6.00% 3 0 3 0.00% 100.00% 76.02 2075.70 -0.83 8.52 297.50 9.9 5.59 -1.56 5.15
NA 10.00% 5 5 0 100.00% 0.00% 83.36 2218.26 NaN NaN NaN NaN 6.11 -1.85 5.09
# 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:  90.34 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")
  )