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

# Create a detailed table for each pitch
detailed_pitch_table <- Luczak_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 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 ClockTilt
Sinker 89.74 2474.79 229.48 22.19 20.27 BallCalled 7.6
Sinker 87.75 2163.56 241.68 20.16 12.11 StrikeCalled 8.1
Slider 78.56 2510.58 95.06 -16.70 3.11 BallCalled 3.2
Sinker 89.09 2244.60 238.45 21.21 14.26 FoulBallNotFieldable 7.9
Slider 77.84 2378.71 71.67 -12.65 -2.40 FoulBallNotFieldable 2.4
Sinker 88.78 1968.43 244.68 19.44 10.43 InPlay 8.2
Sinker 88.48 2245.69 241.64 21.25 12.72 FoulBallNotFieldable 8.1
Changeup 82.87 1816.01 247.93 19.01 9.13 BallCalled 8.3
Changeup 83.42 1782.55 247.98 18.12 8.72 BallCalled 8.3
Sinker 90.45 2105.00 230.48 18.39 16.39 BallCalled 7.7
Sinker 89.40 2071.83 241.82 20.26 12.06 InPlay 8.1
Sinker 88.93 2240.54 234.35 22.32 17.27 FoulBallNotFieldable 7.8
Changeup 83.23 1940.43 242.80 18.49 10.84 BallCalled 8.1
Changeup 84.02 1750.13 232.54 15.60 13.21 BallCalled 7.8
Sinker 89.53 2013.76 245.66 19.73 10.07 FoulBallNotFieldable 8.2
Sinker 89.98 2229.51 245.54 20.44 10.54 FoulBallNotFieldable 8.2
Changeup 84.19 1952.70 230.46 16.62 15.01 InPlay 7.7
Sinker 90.35 2333.75 229.88 19.77 17.80 BallCalled 7.7
Sinker 90.23 2316.55 234.23 18.98 14.80 InPlay 7.8
Slider 77.81 2445.10 89.95 -15.08 1.44 BallCalled 3.0
Sinker 89.04 2194.80 253.79 22.42 7.69 BallCalled 8.5
Sinker 90.31 2152.38 243.75 20.28 11.09 StrikeSwinging 8.1
Sinker 89.75 2226.01 235.65 18.36 13.66 FoulBallNotFieldable 7.9
Slider 78.10 2549.55 85.08 -15.94 0.18 FoulBallNotFieldable 2.8
Four-Seam 90.75 2256.04 218.87 14.40 19.11 StrikeSwinging 7.3
Sinker 89.70 2123.82 245.58 21.74 11.11 StrikeCalled 8.2
Slider 78.25 2473.17 82.11 -10.58 0.06 BallCalled 2.7
Slider 77.58 2409.56 91.39 -9.96 1.79 InPlay 3.0
# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    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
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Luczak")
Summary Pitch Table for Luczak
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 5 4 1 80.00% 20.00% 83.55 1848.36 11.38 17.57 240.34 8.0
Four-Seam 1 0 1 0.00% 100.00% 90.75 2256.04 19.11 14.40 218.87 7.3
Sinker 16 4 12 25.00% 75.00% 89.47 2194.06 13.27 20.43 239.79 8.0
Slider 6 3 3 50.00% 50.00% 78.02 2461.11 0.70 -13.48 85.88 2.9
# 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")
  )