# Filter the data for the pitcher Kutz
Kutz_data <- Newport65 %>%
  filter(Pitcher == "Kutz, Charlie")

# Create a detailed table for each pitch
detailed_pitch_table <- Kutz_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 Kutz")
Detailed Pitch Table for Kutz
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ClockTilt
Four-Seam 88.01 2450.75 135.16 -10.85 12.12 StrikeCalled 4.5
Slider 79.87 2701.91 289.95 9.60 -2.01 BallCalled 9.7
Slider 80.11 2659.13 272.02 8.26 1.10 BallCalled 9.1
Four-Seam 88.21 2368.18 139.63 -7.91 10.49 FoulBallNotFieldable 4.7
Slider 80.80 2712.31 261.23 10.31 2.95 BallCalled 8.7
NA NA NA NA NA NA StrikeSwinging NA
Sinker 88.45 2462.09 122.98 -13.81 10.06 InPlay 4.1
Sinker 86.99 2405.91 115.76 -12.39 7.11 BallCalled 3.9
Sinker 88.03 2416.43 123.03 -12.33 9.11 BallCalled 4.1
Sinker 88.58 2471.05 131.27 -14.60 14.03 BallCalled 4.4
Sinker 87.96 2426.52 123.73 -13.89 10.39 BallCalled 4.1
Changeup 86.66 2409.06 127.39 -11.39 9.78 BallCalled 4.2
Slider 80.42 2664.08 285.05 5.80 -0.11 BallCalled 9.5
Changeup 85.88 2344.54 137.10 -9.13 11.00 BallCalled 4.6
Four-Seam 87.54 2487.47 144.10 -10.17 15.27 BallCalled 4.8
Four-Seam 87.71 2446.22 141.43 -9.65 13.19 BallCalled 4.7
Curveball 79.43 2767.27 286.47 11.58 -1.97 InPlay 9.5
Curveball 79.09 2724.80 305.82 8.89 -4.92 BallCalled 10.2
Slider 81.83 2737.73 321.75 2.91 -2.33 BallCalled 10.7
Four-Seam 88.24 2527.20 137.73 -10.83 13.08 BallCalled 4.6
Changeup 86.25 2529.08 146.94 -8.11 13.65 StrikeCalled 4.9
Slider 80.42 2828.52 279.07 12.77 -0.55 StrikeCalled 9.3
Slider 81.15 2891.53 278.31 11.85 -0.28 InPlay 9.3
# 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 Kutz")
Summary Pitch Table for Kutz
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 3 2 1 66.67% 33.33% 86.26 2427.56 11.48 -9.54 137.14 4.6
Curveball 2 1 1 50.00% 50.00% 79.26 2746.03 -3.44 10.23 296.14 9.8
Four-Seam 5 3 2 60.00% 40.00% 87.94 2455.96 12.83 -9.88 139.61 4.7
Sinker 5 4 1 80.00% 20.00% 88.00 2436.40 10.14 -13.40 123.35 4.1
Slider 7 5 2 71.43% 28.57% 80.66 2742.17 -0.18 8.79 283.91 9.5
NA 1 0 1 0.00% 100.00% NaN NaN NaN NaN NaN NaN
# Prepare data for plotting pitch locations
pitch_location_data <- Kutz_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 Kutz",
       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 Kutz",
       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")
  )