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

# Create a detailed table for each pitch
detailed_pitch_table <- Kyle_Carozza_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.84 2183.54 192.16 3.94 19.52 InPlay 6.03 1.27 6.46 6.4
Curveball 75.80 2360.65 359.89 0.02 -10.79 BallCalled 5.80 1.55 5.98 12.0
Four-Seam 88.20 2278.54 194.29 5.79 24.00 FoulBallNotFieldable 6.05 1.21 6.46 6.5
Curveball 76.25 2494.47 15.23 -3.02 -9.65 BallCalled 5.95 1.47 5.77 0.5
Four-Seam 88.38 2264.30 209.50 9.98 18.84 BallCalled 5.96 1.15 6.35 7.0
Four-Seam 88.38 2239.16 208.02 10.10 20.21 FoulBallNotFieldable 6.03 1.16 6.15 6.9
Curveball 75.43 2321.46 358.53 0.31 -10.70 BallCalled 5.89 1.40 5.96 12.0
Curveball 75.84 2472.06 19.88 -5.00 -12.31 StrikeCalled 5.93 1.38 5.87 0.7
Four-Seam 89.74 2328.81 209.35 10.75 20.31 BallCalled 6.17 1.20 6.06 7.0
Curveball 76.10 2337.07 13.67 -2.03 -6.80 BallCalled 5.90 1.40 5.93 0.5
Four-Seam 88.49 2227.92 215.53 11.40 17.16 FoulBallNotFieldable 6.06 1.38 6.35 7.2
Four-Seam 90.36 2314.49 205.95 10.23 22.34 BallCalled 6.14 1.20 6.13 6.9
Four-Seam 89.57 2260.99 213.91 11.53 18.41 StrikeCalled 6.06 0.99 6.57 7.1
Curveball 76.82 2444.88 23.47 -4.19 -8.16 BallCalled 6.02 1.43 5.84 0.8
Four-Seam 89.28 2258.86 209.71 10.56 19.71 BallCalled 6.01 1.25 6.16 7.0
Four-Seam 89.34 2303.92 201.00 8.82 24.25 StrikeSwinging 6.13 1.20 6.47 6.7
Four-Seam 88.37 2252.89 205.01 9.28 21.18 FoulBallNotFieldable 6.11 1.29 6.30 6.8
Four-Seam 89.54 2231.03 213.04 10.89 17.97 HitByPitch 6.05 1.21 6.22 7.1
# 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:  18
# 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 33.33% 6 5 1 83.33% 16.67% 76.04 2405.10 -9.73 -2.32 131.78 4.4 5.92 1.44 5.89
Four-Seam 66.67% 12 4 8 33.33% 66.67% 89.04 2262.04 20.32 9.44 206.46 6.9 6.07 1.21 6.31
# 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.36 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Kyle_Carozza_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 for", unique(detailed_pitch_table$AutoPitchType)),
       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")
)