# 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
Changeup 86.79 1978.99 246.14 17.75 9.28 BallCalled 5.09 2.00 6.55 8.2
Changeup 86.38 1980.05 245.34 16.63 8.94 StrikeSwinging 5.12 2.08 6.37 8.2
Curveball 73.85 2072.22 48.72 -12.88 -9.53 StrikeSwinging 5.34 2.06 5.67 1.6
Sinker 87.34 2016.95 240.94 19.56 12.31 StrikeSwinging 5.09 1.99 6.43 8.0
Changeup 87.35 2017.87 254.87 15.58 5.61 BallCalled 5.14 2.07 6.29 8.5
Changeup 77.02 1958.11 261.62 21.34 5.01 InPlay 4.98 2.12 6.22 8.7
Curveball 73.39 1954.87 74.31 -13.93 -1.95 BallCalled 5.28 2.15 6.09 2.5
Changeup 86.70 1939.89 244.77 16.08 8.76 BallCalled 5.10 2.09 6.59 8.2
Changeup 84.07 1896.11 239.38 15.84 10.71 FoulBallNotFieldable 5.21 2.23 6.40 8.0
Curveball 72.43 1980.29 44.82 -15.12 -13.17 InPlay 5.56 2.09 5.76 1.5
Sinker 86.60 1925.47 239.96 13.36 8.91 BallCalled 5.19 2.13 6.41 8.0
Changeup 85.76 1901.76 252.41 13.98 5.64 StrikeCalled 5.22 2.10 6.33 8.4
Curveball 72.04 1997.55 42.29 -13.60 -13.13 StrikeSwinging 5.32 2.06 5.72 1.4
Sinker 87.43 1942.12 235.67 13.22 10.10 BallCalled 5.12 2.12 6.20 7.9
Curveball 73.59 2003.08 61.53 -18.28 -8.21 BallCalled 5.35 2.07 5.69 2.1
Curveball 71.27 1947.69 51.18 -17.62 -12.38 StrikeCalled 5.42 1.98 5.64 1.7
Curveball 73.03 1832.78 60.50 -17.54 -8.33 BallCalled 5.43 1.77 5.95 2.0
Changeup 84.52 1849.11 242.04 13.06 8.20 StrikeCalled 5.21 2.05 6.45 8.1
Curveball 73.65 1864.54 63.31 -17.58 -7.12 InPlay 5.34 2.03 5.75 2.1
NA NA NA NA NA NA StrikeCalled NA NA NA NA
Changeup 86.06 1794.52 247.04 14.53 7.26 StrikeCalled 5.09 1.98 6.49 8.2
Curveball 74.94 1712.07 50.88 -13.73 -9.62 BallCalled 5.33 1.98 5.69 1.7
Changeup 86.61 1948.84 252.99 16.52 6.19 InPlay 5.12 2.17 6.43 8.4
Curveball 70.50 1759.40 57.44 -17.59 -9.41 InPlay 5.59 1.93 5.78 1.9
Changeup 84.87 1824.83 251.88 13.93 5.74 StrikeCalled 5.19 2.12 6.29 8.4
Curveball 73.02 1956.68 50.13 -19.03 -14.20 StrikeSwinging 5.27 2.03 5.64 1.7
Curveball 73.41 1938.61 61.63 -18.06 -8.27 InPlay 5.31 2.05 5.75 2.1
Curveball 72.34 1859.18 56.71 -17.41 -9.57 FoulBallNotFieldable 5.48 1.68 5.91 1.9
Curveball 72.45 1970.03 42.90 -14.67 -13.89 InPlay 5.51 1.79 5.79 1.4
Changeup 84.13 1868.08 242.21 18.80 11.33 BallCalled 5.29 2.32 6.45 8.1
Curveball 69.67 2003.60 48.05 -20.85 -16.51 BallCalled 5.44 1.93 5.60 1.6
Changeup 86.02 1952.37 221.68 14.08 17.19 BallCalled 5.27 2.05 6.54 7.4
Changeup 83.16 1796.31 254.53 21.78 7.55 BallCalled 5.26 2.24 6.37 8.5
Curveball 72.08 1936.57 44.94 -12.59 -10.61 FoulBallNotFieldable 5.42 2.00 5.75 1.5
Changeup 85.48 1841.90 243.71 19.08 10.95 StrikeCalled 5.22 2.07 6.35 8.1
Curveball 73.49 1991.65 55.68 -18.73 -10.67 InPlay 5.44 1.96 5.68 1.9
Curveball 71.01 1981.82 48.13 -18.58 -14.39 InPlay 5.53 1.80 6.03 1.6
Changeup 85.21 1902.21 243.22 13.71 8.18 BallCalled 5.19 2.16 6.49 8.1
Changeup 85.44 1913.11 245.40 13.08 7.15 InPlay 5.26 2.12 6.35 8.2
Changeup 84.44 1766.81 257.38 21.68 6.16 StrikeCalled 5.23 2.01 6.48 8.6
Curveball 73.32 2040.74 91.59 -21.65 2.40 BallCalled 5.19 1.93 6.24 3.1
Changeup 84.40 1907.01 211.55 8.62 15.40 StrikeSwinging 5.21 1.99 6.47 7.1
Changeup 84.88 1929.34 220.65 11.05 14.13 InPlay 5.21 1.92 6.40 7.4
Changeup 83.67 1790.65 243.09 18.26 10.69 FoulBallNotFieldable 5.18 2.34 6.34 8.1
Four-Seam 87.78 1963.94 202.59 7.72 19.76 FoulBallNotFieldable 5.28 1.91 6.57 6.8
Changeup 85.07 1906.80 244.64 16.28 9.19 BallCalled 5.23 2.33 6.30 8.2
Changeup 86.72 2090.58 223.18 11.80 13.76 BallCalled 5.47 1.91 6.53 7.4
Changeup 84.38 1993.19 252.44 18.71 7.30 InPlay 5.17 2.17 6.34 8.4
Changeup 85.06 1830.01 254.80 19.45 6.64 StrikeCalled 5.23 2.09 6.58 8.5
Curveball 70.65 1994.07 52.90 -16.83 -10.58 BallCalled 5.65 1.71 5.84 1.8
Changeup 83.77 1869.69 246.00 17.60 9.12 InPlay 5.35 2.07 6.36 8.2
Changeup 84.63 1936.29 240.11 15.36 10.16 BallCalled 5.26 2.25 6.44 8.0
Changeup 83.96 1901.77 262.41 17.72 3.69 StrikeSwinging 5.21 1.96 6.36 8.7
Changeup 85.35 1960.58 253.88 17.08 6.22 FoulBallNotFieldable 5.38 2.06 6.46 8.5
Curveball 69.89 1987.38 47.19 -13.87 -10.76 FoulBallNotFieldable 5.61 1.94 5.72 1.6
Changeup 84.64 1860.33 252.64 15.73 6.07 FoulBallNotFieldable 5.35 2.12 6.28 8.4
Curveball 72.49 2051.47 38.83 -13.03 -14.22 InPlay 5.48 1.73 5.58 1.3
# 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:  57
# 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 52.63% 30 10 20 33.33% 66.67% 84.88 1903.57 8.74 16.17 245.07 8.2 5.21 2.11 6.41
Curveball 38.60% 22 7 15 31.82% 68.18% 72.39 1947.10 -10.19 -16.51 54.26 1.8 5.42 1.94 5.78
Four-Seam 1.75% 1 0 1 0.00% 100.00% 87.78 1963.94 19.76 7.72 202.59 6.8 5.28 1.91 6.57
Sinker 5.26% 3 2 1 66.67% 33.33% 87.12 1961.51 10.44 15.38 238.86 8.0 5.13 2.08 6.35
NA 1.75% 1 0 1 0.00% 100.00% NaN NaN NaN NaN NaN NaN NaN NaN NaN
# 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:  87.78 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")
  )