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

# Create a detailed table for each pitch
detailed_pitch_table <- Kutz_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 Kutz")
Detailed Pitch Table for Kutz
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 85.92 2364.92 110.75 -17.28 8.07 StrikeCalled 5.32 -2.57 6.35 3.7
Curveball 79.40 2730.37 264.73 4.76 2.08 BallCalled 5.10 -2.61 5.82 8.8
Changeup 87.45 2527.03 116.24 -16.46 9.54 BallCalled 5.37 -2.57 6.42 3.9
Changeup 88.14 2392.75 114.51 -18.59 9.93 BallCalled 5.34 -2.51 6.63 3.8
Changeup 88.09 2472.64 114.60 -20.80 11.03 StrikeCalled 5.37 -2.67 6.64 3.8
Slider 80.53 2760.84 254.26 9.34 4.30 StrikeSwinging 5.19 -2.69 5.76 8.5
Slider 80.27 2571.89 243.26 2.09 2.79 BallCalled 5.17 -2.77 5.82 8.1
Slider 81.22 2603.14 261.71 2.07 1.86 BallCalled 5.10 -2.77 5.89 8.7
Changeup 88.25 2410.69 108.25 -18.74 7.58 StrikeSwinging 5.17 -2.71 6.43 3.6
Slider 81.32 2713.04 299.57 6.31 -1.88 BallCalled 5.14 -2.69 5.81 10.0
Changeup 85.55 2443.19 119.31 -13.66 9.11 StrikeCalled 5.21 -2.66 6.33 4.0
Slider 82.15 2779.82 306.69 5.93 -2.95 FoulBall 5.17 -2.82 5.76 10.2
Slider 81.31 2716.94 293.92 6.00 -1.23 StrikeCalled 5.13 -2.70 5.88 9.8
Slider 79.52 2624.67 250.00 10.61 5.45 BallCalled 5.18 -2.77 6.05 8.3
Sinker 89.13 2387.88 112.19 -18.70 8.93 StrikeCalled 5.31 -2.52 7.04 3.7
Sinker 88.38 2487.36 102.98 -15.84 5.00 FoulBall 5.27 -2.64 6.48 3.4
Slider 81.92 2680.94 257.94 9.66 3.62 BallCalled 5.08 -2.74 5.81 8.6
Slider 80.65 2678.47 269.64 10.04 1.54 FoulBall 5.04 -2.76 5.82 9.0
Sinker 89.31 2453.99 96.55 -15.70 3.08 BallCalled 5.21 -2.67 6.30 3.2
Slider 80.66 2624.01 290.52 6.93 -0.96 StrikeSwinging 5.08 -2.77 5.86 9.7
Curveball 78.92 2787.81 259.83 7.29 2.99 StrikeCalled 5.20 -2.84 5.93 8.7
Sinker 87.92 NA 126.47 -14.01 11.77 BallCalled 5.27 -2.70 6.50 4.2
Curveball 79.81 2646.75 300.05 8.73 -3.48 StrikeSwinging 4.95 -2.85 5.84 10.0
Curveball 78.91 2786.55 289.11 10.37 -2.05 BallCalled 4.99 -2.86 5.73 9.6
Slider 80.07 2642.66 268.50 10.85 1.90 StrikeSwinging 5.15 -2.76 5.99 8.9
# 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:  25
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Kutz")
Summary Pitch Table for Kutz
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 24.00% 6 2 4 33.33% 66.67% 87.23 2435.20 9.21 -17.59 113.94 3.8 5.30 -2.61 6.47
Curveball 16.00% 4 2 2 50.00% 50.00% 79.26 2737.87 -0.11 7.79 278.43 9.3 5.06 -2.79 5.83
Sinker 16.00% 4 2 2 50.00% 50.00% 88.68 2443.08 7.20 -16.06 109.55 3.6 5.26 -2.63 6.58
Slider 44.00% 11 5 6 45.45% 54.55% 80.87 2672.40 1.31 7.26 272.36 9.1 5.13 -2.75 5.86
# 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("Kutz maximum FB velocity: ", max_fb_velocity, "mph\n")
## Kutz maximum FB velocity:  89.31 mph
# 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")
  )