# Filter the data for the pitcher Luczak
Luczak_data <- Bristol610 %>%
  filter(Pitcher == "Luczak, Andrew")

# Create a detailed table for each pitch
detailed_pitch_table <- Luczak_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 Luczak")
Detailed Pitch Table for Luczak
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 89.71 2378.48 239.02 19.87 13.06 BallCalled 5.08 2.77 5.89 8.0
Sinker 89.46 2141.33 229.09 17.51 16.26 StrikeCalled 5.43 2.54 6.22 7.6
Slider 77.46 2479.49 78.87 -15.91 -1.69 InPlay 5.36 2.64 5.72 2.6
Sinker 89.43 2232.42 252.95 19.23 7.01 StrikeCalled 4.99 2.75 5.99 8.4
Sinker 89.12 2311.04 244.73 21.32 11.22 InPlay 4.96 2.70 6.08 8.2
Sinker 88.12 2324.58 253.59 19.35 7.00 BallCalled 5.09 2.80 6.11 8.5
Sinker 87.04 2180.07 249.80 17.16 7.57 InPlay 5.00 2.69 5.98 8.3
Sinker 89.70 2419.37 245.59 16.91 8.85 FoulBallNotFieldable 5.13 2.67 6.00 8.2
Sinker 88.00 2148.74 245.26 19.17 9.96 FoulBallNotFieldable 5.10 2.65 6.13 8.2
Sinker 89.77 2235.24 238.43 20.18 13.57 FoulBallNotFieldable 5.45 2.52 6.10 7.9
Sinker 88.97 2230.76 241.39 18.47 11.38 InPlay 5.51 2.48 6.22 8.0
Slider 76.33 2476.13 74.79 -17.77 -3.10 StrikeCalled 5.25 2.72 5.78 2.5
Curveball 76.40 2347.38 60.81 -17.22 -7.94 BallCalled 5.29 2.63 5.88 2.0
Changeup 83.01 2007.78 237.69 17.87 12.71 BallCalled 5.26 2.61 6.07 7.9
Sinker 89.71 2236.86 250.92 19.93 8.10 BallCalled 4.97 2.68 6.23 8.4
Sinker 90.15 2195.90 252.40 19.04 7.20 FoulBallNotFieldable 5.02 2.74 5.88 8.4
Sinker 90.90 1981.36 252.99 17.92 6.52 StrikeSwinging 5.03 2.73 6.32 8.4
Curveball 77.29 2352.31 50.40 -12.33 -8.65 StrikeCalled 5.17 2.68 5.89 1.7
Sinker 90.97 2061.43 255.50 19.37 6.06 InPlay 5.00 2.76 6.12 8.5
Curveball 75.90 2446.05 40.20 -8.39 -8.28 BallCalled 5.34 2.60 5.60 1.3
Sinker 89.05 2368.77 232.20 19.05 16.01 InPlay 5.44 2.50 6.34 7.7
Sinker 90.47 2422.79 243.18 20.86 11.72 StrikeCalled 5.17 2.71 5.87 8.1
Sinker 89.43 2036.22 264.41 20.44 3.19 FoulBallNotFieldable 5.03 2.58 6.02 8.8
Sinker 89.77 2390.05 259.87 21.20 5.00 InPlay 5.05 2.50 5.67 8.7
Sinker 89.98 2209.51 258.27 20.98 5.48 StrikeCalled 4.94 2.74 6.07 8.6
Curveball 77.34 2387.67 50.15 -16.25 -12.03 BallinDirt 5.15 2.44 5.65 1.7
Curveball 76.65 2537.94 55.12 -10.84 -6.13 StrikeCalled 5.20 2.60 5.92 1.8
Four-Seam 90.47 2326.77 220.09 15.71 19.78 FoulBallNotFieldable 5.46 2.43 6.14 7.3
Curveball 76.21 2501.50 68.34 -7.79 -1.72 BallCalled 5.37 2.48 5.96 2.3
Sinker 89.27 2007.16 243.55 16.57 9.29 BallCalled 5.06 2.64 6.20 8.1
Four-Seam 88.91 2364.81 222.78 16.98 19.47 BallCalled 5.38 2.35 6.33 7.4
Sinker 88.58 2406.03 231.26 19.42 16.73 BallCalled 5.27 2.50 6.23 7.7
Sinker 87.67 2257.40 241.42 18.47 11.19 StrikeSwinging 4.98 2.80 6.08 8.0
Sinker 89.45 2410.07 249.21 20.64 8.97 BallCalled 4.94 2.72 6.34 8.3
Curveball 75.40 2442.52 72.16 -9.02 -1.42 InPlay 5.30 2.68 5.81 2.4
Sinker 89.24 2297.08 256.19 20.95 6.28 BallCalled 5.05 2.83 5.86 8.5
Sinker 88.29 2245.65 246.13 19.29 9.68 FoulBallNotFieldable 4.90 2.64 6.18 8.2
Changeup 82.27 1818.41 250.60 16.37 7.09 StrikeSwinging 5.29 2.70 6.09 8.4
Sinker 89.72 2375.65 231.10 17.60 15.39 BallCalled 5.42 2.53 6.27 7.7
Sinker 88.80 2235.23 250.54 19.41 8.02 InPlay 4.92 2.71 6.21 8.4
Four-Seam 87.06 2268.96 221.19 15.82 19.32 BallCalled 5.56 2.45 6.21 7.4
Sinker 89.36 2055.69 238.62 18.58 12.47 BallCalled 5.11 2.59 6.04 8.0
Changeup 85.80 2038.02 247.23 19.70 9.51 BallCalled 5.10 2.58 5.92 8.2
Four-Seam 86.48 2380.94 226.55 18.19 18.54 BallCalled 5.39 2.58 6.06 7.6
Changeup 87.24 2269.60 243.44 19.32 10.93 BallCalled 4.96 2.87 5.97 8.1
Curveball 73.65 2370.14 50.27 -15.26 -10.92 BallCalled 5.28 2.71 5.54 1.7
Sinker 89.25 2094.29 256.61 19.97 5.89 StrikeCalled 4.97 2.69 6.34 8.6
Sinker 88.07 2274.25 241.60 18.46 11.23 FoulBallNotFieldable 5.03 2.67 6.03 8.1
Changeup 81.26 1722.26 233.85 13.98 11.57 StrikeSwinging 5.34 2.52 6.38 7.8
Sinker 88.78 2098.19 249.82 18.49 7.92 BallCalled 5.13 2.68 6.21 8.3
Curveball 73.46 2401.53 69.19 -15.48 -4.15 StrikeSwinging 5.15 2.74 6.02 2.3
Sinker 88.39 2249.80 248.00 18.56 8.63 StrikeCalled 5.06 2.72 6.11 8.3
Sinker 89.46 2305.58 224.74 16.49 17.78 StrikeSwinging 5.33 2.42 6.22 7.5
Sinker 89.21 2159.64 250.88 17.75 7.25 InPlay 5.13 2.59 6.17 8.4
Curveball 77.44 2557.62 57.71 -10.87 -5.41 InPlay 5.23 2.59 5.97 1.9
Sinker 89.84 2280.65 249.43 18.94 8.21 StrikeCalled 5.03 2.64 6.04 8.3
Sinker 90.16 2394.20 246.91 19.12 9.28 FoulBallNotFieldable 5.02 2.54 5.92 8.2
Four-Seam 90.54 2479.25 214.17 13.87 21.60 InPlay 5.38 2.51 6.21 7.1
Four-Seam 90.47 2238.50 212.65 13.13 21.65 BallCalled 5.36 2.54 6.13 7.1
Sinker 88.38 2251.25 248.77 19.85 8.84 InPlay 5.07 2.75 6.25 8.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\n")
## Total number of pitches thrown:  60
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Luczak")
Summary Pitch Table for Luczak
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 8.33% 5 3 2 60.00% 40.00% 83.92 1971.21 10.36 17.45 242.56 8.1 5.19 2.66 6.09
Curveball 16.67% 10 4 6 40.00% 60.00% 75.97 2434.47 -6.66 -12.35 57.44 1.9 5.25 2.62 5.82
Four-Seam 10.00% 6 4 2 66.67% 33.33% 88.99 2343.20 20.06 15.62 219.57 7.3 5.42 2.48 6.18
Sinker 61.67% 37 10 27 27.03% 72.97% 89.23 2240.61 9.68 19.10 246.33 8.2 5.10 2.65 6.11
Slider 3.33% 2 0 2 0.00% 100.00% 76.89 2477.81 -2.40 -16.84 76.83 2.5 5.30 2.68 5.75
# 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("Luczak maximum FB velocity: ", max_fb_velocity, "mph\n")
## Luczak maximum FB velocity:  90.97 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Luczak_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 Luczak",
       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 Luczak",
       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")
  )