# 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.47 2265.59 201.03 7.45 20.56 FoulBallNotFieldable 6.11 1.12 6.37 6.7
Four-Seam 87.83 2214.84 210.12 10.46 19.39 FoulBallNotFieldable 5.97 1.15 6.30 7.0
Four-Seam 90.22 2307.88 219.81 13.58 17.52 BallCalled 6.09 1.09 6.29 7.3
Curveball 77.39 2476.88 20.11 -3.59 -8.26 FoulBallNotFieldable 5.86 1.23 6.01 0.7
Curveball 76.93 2463.31 23.33 -3.06 -5.54 InPlay 5.93 1.27 5.88 0.8
Four-Seam 87.51 2218.36 205.39 7.79 17.65 BallCalled 6.12 1.18 6.32 6.8
Four-Seam 87.06 2218.80 202.93 7.33 18.51 FoulBallNotFieldable 5.93 1.22 6.47 6.8
Curveball 77.26 2429.21 5.11 -0.54 -4.55 BallCalled 5.85 1.20 5.98 0.2
Four-Seam 88.14 2235.19 211.48 11.09 19.32 InPlay 6.00 0.98 6.24 7.0
Four-Seam 87.74 2211.27 209.44 10.54 19.80 FoulBallNotFieldable 5.95 0.99 6.31 7.0
Curveball 75.80 2352.28 3.41 -0.43 -5.57 BallCalled 5.93 1.23 5.75 0.1
Four-Seam 87.71 2181.76 208.46 9.78 19.26 StrikeCalled 6.01 0.92 6.29 6.9
Four-Seam 89.55 2285.99 219.16 13.53 17.83 InPlay 6.00 0.92 6.10 7.3
Four-Seam 85.65 2156.78 205.50 8.46 19.05 BallCalled 5.99 1.27 6.24 6.8
Four-Seam 86.66 2275.14 201.91 8.76 23.11 BallCalled 6.01 1.33 6.11 6.7
Four-Seam 87.11 2284.17 207.44 10.14 20.82 BallCalled 5.90 1.25 6.24 6.9
Four-Seam 87.89 2258.75 211.05 12.38 21.87 StrikeCalled 5.93 1.22 6.20 7.0
Four-Seam 87.76 2231.73 206.13 8.42 18.42 FoulBallNotFieldable 5.96 1.29 6.22 6.9
Four-Seam 89.28 2289.84 211.51 10.17 17.83 StrikeCalled 5.90 1.27 6.24 7.1
Curveball 76.36 2331.88 39.57 -3.53 -2.73 BallCalled 5.83 1.21 5.87 1.3
Four-Seam 88.31 2236.79 198.06 6.01 19.74 InPlay 5.81 1.18 6.35 6.6
Four-Seam 88.15 2201.71 221.23 14.06 17.37 InPlay 6.00 1.09 6.09 7.4
# 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:  22
# 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
Curveball 22.73% 5 3 2 60.00% 40.00% 76.75 2410.71 -5.33 -2.23 18.31 0.6 5.88 1.23 5.90
Four-Seam 77.27% 17 5 12 29.41% 70.59% 87.94 2239.68 19.30 10.00 208.86 7.0 5.98 1.15 6.26
# 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:  90.22 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")
  )