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

# 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 Tyler Cox")
Detailed Pitch Table for Tyler Cox
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 73.86 2080.58 68.49 -16.94 -4.87 StrikeCalled 4.92 2.46 5.55 2.3
Curveball 73.52 2024.57 113.82 -10.86 6.72 FoulBallNotFieldable 5.15 2.46 5.48 3.8
Curveball 71.91 1914.85 43.65 -16.07 -15.00 FoulBallNotFieldable 5.50 1.95 5.42 1.5
Curveball 74.01 2011.68 61.20 -12.70 -4.88 FoulBallNotFieldable 5.21 2.44 5.53 2.0
Four-Seam 88.13 2024.04 220.50 11.53 14.69 StrikeCalled 5.31 2.36 6.52 7.3
Sinker 87.61 2004.56 232.31 15.72 13.36 BallCalled 5.01 2.14 6.40 7.7
Changeup 85.54 1886.80 257.70 19.26 5.46 StrikeCalled 4.90 2.31 6.08 8.6
Curveball 73.62 1820.50 35.88 -10.24 -12.35 InPlay 5.15 2.14 5.32 1.2
Changeup 84.65 1903.70 253.29 16.06 6.11 BallCalled 5.19 2.28 6.01 8.4
Changeup 84.82 1889.15 249.08 17.44 7.94 InPlay 5.00 2.21 6.17 8.3
Sinker 86.60 1889.16 235.70 14.06 10.75 StrikeCalled 4.90 2.39 6.12 7.9
Sinker 86.80 1877.52 238.16 13.55 9.58 BallCalled 4.95 2.20 6.01 7.9
Changeup 78.37 1931.05 258.99 21.24 5.61 BallCalled 4.76 2.43 5.70 8.6
Changeup 86.22 1862.04 264.59 13.57 2.43 InPlay 4.90 2.32 6.11 8.8
Curveball 72.10 1932.86 75.08 -11.99 -1.33 InPlay 5.06 2.47 5.64 2.5
Changeup 87.41 1929.48 250.07 17.39 7.44 BallCalled 4.96 2.22 6.14 8.3
Changeup 85.22 1828.30 248.77 17.64 8.03 StrikeCalled 5.16 2.36 6.05 8.3
Changeup 76.69 1936.10 275.12 19.71 -0.18 InPlay 4.86 2.25 5.73 9.2
# 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 %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # 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 Tyler Cox")
Summary Pitch Table for Tyler Cox
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 44.44% 8 3 5 37.50% 62.50% 83.61 1895.83 5.36 17.79 257.20 8.6 4.97 2.30 6.00
Curveball 33.33% 6 0 6 0.00% 100.00% 73.17 1964.17 -5.28 -13.13 66.35 2.2 5.16 2.32 5.49
Four-Seam 5.56% 1 0 1 0.00% 100.00% 88.13 2024.04 14.69 11.53 220.50 7.3 5.31 2.36 6.52
Sinker 16.67% 3 2 1 66.67% 33.33% 87.00 1923.75 11.23 14.44 235.39 7.8 4.95 2.24 6.18
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Tyler Cox maximum FB velocity: ", max_fb_velocity, "mph\n")
## Tyler Cox maximum FB velocity:  88.13 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", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | 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.85, xmax = 0.85, 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 Tyler Cox",
       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")
  )