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

# Create a detailed table for each pitch
detailed_pitch_table <- Andrew_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 Andrew Luczak")
Detailed Pitch Table for Andrew Luczak
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 86.53 2244.33 223.69 19.09 21.62 BallCalled 5.80 2.17 6.09 7.5
Sinker 89.22 2346.42 226.11 19.04 19.85 StrikeSwinging 5.45 2.27 6.06 7.5
Changeup 87.45 2314.83 237.75 15.92 11.46 BallCalled 5.23 2.53 6.12 7.9
Sinker 88.58 2253.51 227.83 18.66 18.32 StrikeSwinging 5.70 2.24 6.19 7.6
Sinker 89.10 2404.31 225.48 18.15 19.30 InPlay 5.57 2.29 6.15 7.5
Sinker 89.68 2393.32 235.87 16.98 12.74 FoulBallNotFieldable 5.11 2.42 6.07 7.9
Sinker 90.86 2050.57 255.89 18.97 6.04 BallCalled 5.12 2.45 6.08 8.5
Sinker 91.14 2412.98 244.65 15.71 8.62 StrikeCalled 5.17 2.44 6.18 8.2
Changeup 84.43 1852.44 218.80 9.92 13.60 InPlay 5.20 2.41 6.22 7.3
Sinker 89.20 2376.99 225.07 15.86 17.18 BallCalled 5.56 2.32 6.20 7.5
Sinker 89.45 2404.49 226.06 17.91 18.60 StrikeCalled 5.50 2.20 6.25 7.5
Curveball 78.04 2298.77 60.74 -4.95 -0.91 FoulBallNotFieldable 5.37 2.40 6.06 2.0
Four-Seam 89.27 2283.22 216.88 12.12 17.35 HitByPitch 5.52 2.24 5.97 7.2
Sinker 89.80 2188.18 245.34 15.97 8.55 StrikeCalled 5.07 2.54 6.06 8.2
Sinker 89.13 2319.42 234.18 13.59 11.09 StrikeSwinging 5.17 2.43 6.00 7.8
Four-Seam 89.57 2217.10 222.48 12.95 15.41 BallCalled 5.35 2.42 6.27 7.4
Changeup 82.57 1743.98 222.35 8.00 10.25 InPlay 5.38 2.38 6.46 7.4
Four-Seam 89.43 2287.36 215.14 12.11 18.50 StrikeCalled 5.59 2.40 6.12 7.2
Sinker 88.74 2400.50 236.20 15.49 11.71 BallCalled 5.47 2.38 6.07 7.9
Sinker 90.70 2447.82 237.64 16.68 12.02 StrikeSwinging 5.40 2.40 6.01 7.9
Curveball 78.25 2595.74 63.75 -8.24 -2.45 BallinDirt 5.50 2.45 5.99 2.1
Slider 78.48 2523.97 83.56 -13.43 0.33 StrikeCalled 5.48 2.63 5.61 2.8
Sinker 90.85 2386.77 225.08 16.79 18.13 BallCalled 5.60 2.48 6.16 7.5
Four-Seam 88.73 2207.37 218.19 13.34 18.41 BallCalled 5.52 2.38 6.35 7.3
Four-Seam 89.36 2484.73 219.88 13.64 17.66 StrikeCalled 5.53 2.40 6.30 7.3
Sinker 91.11 2332.10 229.00 18.12 17.09 BallCalled 5.44 2.37 6.37 7.6
Sinker 90.12 2325.47 226.30 15.49 16.18 InPlay 5.64 2.45 5.96 7.5
# 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:  27
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Andrew Luczak")
Summary Pitch Table for Andrew Luczak
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 11.11% 3 1 2 33.33% 66.67% 84.82 1970.42 11.77 11.28 226.30 7.5 5.27 2.44 6.27
Curveball 7.41% 2 0 2 0.00% 100.00% 78.15 2447.26 -1.68 -6.60 62.25 2.0 5.44 2.42 6.03
Four-Seam 22.22% 6 3 3 50.00% 50.00% 88.82 2287.35 18.16 13.88 219.38 7.3 5.55 2.34 6.18
Sinker 55.56% 15 5 10 33.33% 66.67% 89.85 2336.19 14.36 16.89 233.38 7.8 5.40 2.38 6.12
Slider 3.70% 1 0 1 0.00% 100.00% 78.48 2523.97 0.33 -13.43 83.56 2.8 5.48 2.63 5.61
# 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("Andrew Luczak maximum FB velocity: ", max_fb_velocity, "mph\n")
## Andrew Luczak maximum FB velocity:  91.14 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Andrew_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) +
  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 Andrew Luczak",
       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 for Andrew Luczak
ggplot(detailed_pitch_table, aes(x = HorizontalBreak, y = InducedVerticalBreak, color = AutoPitchType)) +
  geom_point(size = 3) + # Increase point size
  labs(title = "Pitch Movement for Andrew 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")
  )