# Filter the data for the pitcher Kyle Carozza
Brian_Foley_data <- data %>%
  filter(Pitcher == "Carozza, Kyle")

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_Foley_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 Kyle Carozza")
Detailed Pitch Table for Kyle Carozza
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 88.34 2285.14 206.20 9.28 20.12 StrikeCalled 6.01 1.03 6.32 6.9
Four-Seam 89.39 2338.02 209.58 9.48 17.94 BallCalled 6.06 1.16 6.38 7.0
Changeup 78.75 1305.17 173.74 -1.29 13.25 BallCalled 5.85 1.12 6.98 5.8
Four-Seam 88.45 2232.30 202.31 8.04 20.94 FoulBallNotFieldable 6.08 0.99 6.45 6.7
Four-Seam 90.43 2420.88 205.35 10.07 22.57 BallCalled 5.94 1.10 6.22 6.8
Four-Seam 90.07 2336.05 210.26 10.17 18.70 FoulBallNotFieldable 6.00 1.17 6.28 7.0
Four-Seam 90.61 2373.28 198.22 6.51 21.07 BallCalled 6.04 0.95 6.08 6.6
Curveball 78.51 2523.31 60.02 -6.27 -2.02 FoulBallNotFieldable 5.86 1.28 5.91 2.0
Four-Seam 90.16 2329.78 203.32 9.00 22.01 BallCalled 6.02 0.91 6.28 6.8
Curveball 79.60 2465.41 9.34 -1.36 -6.90 StrikeCalled 5.83 1.25 5.79 0.3
Curveball 78.84 2430.58 19.39 -2.02 -4.55 FoulBallNotFieldable 5.90 1.17 6.02 0.6
Four-Seam 90.35 2340.71 222.75 13.87 16.24 InPlay 5.99 1.13 6.07 7.4
Curveball 78.17 2455.21 20.27 -3.12 -6.85 StrikeCalled 5.85 1.23 5.91 0.7
Four-Seam 89.22 2289.61 218.10 12.08 16.74 BallCalled 6.01 1.24 6.28 7.3
Curveball 79.42 2493.62 14.90 -2.61 -8.34 StrikeCalled 5.85 1.18 5.82 0.5
Curveball 79.21 2490.49 19.00 -3.67 -9.07 StrikeCalled 5.86 1.29 5.78 0.6
Curveball 78.28 2449.15 28.36 -3.50 -4.95 StrikeCalled 5.84 1.32 5.97 0.9
Slider 80.26 2527.84 80.31 -2.19 1.14 StrikeCalled 5.85 1.25 5.86 2.7
Four-Seam 91.26 2372.12 214.37 11.22 17.68 BallCalled 5.89 1.07 6.07 7.1
Curveball 78.58 2485.59 29.52 -3.63 -4.74 InPlay 5.82 1.20 5.95 1.0
Curveball 77.33 2450.56 31.72 -4.28 -5.39 StrikeSwinging 5.87 1.22 5.87 1.1
Four-Seam 88.73 2273.49 203.55 8.12 19.92 FoulBallNotFieldable 5.92 1.32 6.40 6.8
Curveball 78.58 2495.93 349.99 7.57 -41.40 BallCalled 4.87 1.21 6.04 11.7
Curveball 78.15 2374.98 41.54 -4.21 -3.48 BallCalled 5.87 1.11 5.91 1.4
Four-Seam 88.51 2305.93 211.21 10.23 18.06 InPlay 5.92 1.25 6.35 7.0
Curveball 76.31 2352.73 49.85 -7.09 -4.52 BallCalled 5.89 1.26 6.10 1.7
Curveball 76.71 2416.39 8.31 -1.36 -7.83 BallCalled 5.83 1.36 6.04 0.3
Four-Seam 89.28 2302.10 201.12 7.18 19.85 BallCalled 5.90 1.18 6.26 6.7
Four-Seam 87.96 2271.95 214.41 12.05 18.87 BallCalled 5.99 1.15 6.40 7.1
Four-Seam 88.74 2313.73 215.54 12.48 18.80 FoulBallNotFieldable 5.92 1.15 6.50 7.2
Four-Seam 90.02 2354.19 205.11 8.45 19.26 FoulBallNotFieldable 5.92 1.13 6.14 6.8
Curveball 77.78 2298.23 359.86 0.02 -8.54 InPlay 5.70 1.22 5.97 12.0
# 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:  32
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Kyle Carozza")
Summary Pitch Table for Kyle Carozza
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 3.12% 1 1 0 100.00% 0.00% 78.75 1305.17 13.25 -1.29 173.74 5.8 5.85 1.12 6.98
Curveball 43.75% 14 4 10 28.57% 71.43% 78.25 2441.58 -8.47 -2.54 74.43 2.5 5.77 1.24 5.93
Four-Seam 50.00% 16 8 8 50.00% 50.00% 89.47 2321.20 19.30 9.89 208.84 7.0 5.98 1.12 6.28
Slider 3.12% 1 0 1 0.00% 100.00% 80.26 2527.84 1.14 -2.19 80.31 2.7 5.85 1.25 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("Kyle Carozza maximum FB velocity: ", max_fb_velocity, "mph\n")
## Kyle Carozza maximum FB velocity:  91.26 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_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 Kyle Carozza",
       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
ggplot(detailed_pitch_table, aes(x = HorizontalBreak, y = InducedVerticalBreak, color = AutoPitchType)) +
  geom_point(size = 3) + # Increase point size
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement"),
       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")
  )