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

# 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 Colin Burke")
Detailed Pitch Table for Colin Burke
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 87.89 1747.25 207.86 8.88 17.87 FoulBallNotFieldable 6.72 0.38 4.93 6.9
Curveball 78.71 2354.04 24.15 -8.45 -17.32 BallCalled 6.46 0.63 4.28 0.8
Four-Seam 88.56 1963.27 211.04 10.32 18.36 BallCalled 6.70 0.59 4.81 7.0
Changeup 87.70 1893.04 219.28 13.59 17.87 BallCalled 6.72 0.54 4.80 7.3
Changeup 87.99 1701.73 215.67 11.39 17.01 BallCalled 6.74 0.61 4.73 7.2
Changeup 87.87 1829.42 213.66 10.08 16.31 StrikeCalled 6.55 0.78 4.76 7.1
Curveball 77.85 2264.73 24.57 -9.18 -18.49 StrikeSwinging 6.47 0.69 4.62 0.8
Curveball 79.20 2467.22 24.06 -9.75 -20.32 FoulBallNotFieldable 6.40 0.86 4.49 0.8
Four-Seam 87.44 1973.72 209.57 10.72 20.22 BallCalled 6.72 0.70 4.70 7.0
Curveball 79.27 2417.87 7.17 -2.54 -18.65 BallCalled 6.50 0.91 4.40 0.2
Curveball 79.58 2466.51 42.45 -11.70 -11.29 FoulBallNotFieldable 6.40 0.70 4.31 1.4
Four-Seam 87.04 1941.15 200.43 7.44 21.24 StrikeSwinging 6.64 0.37 4.67 6.7
Four-Seam 88.28 1943.66 204.68 9.02 20.84 StrikeCalled 6.61 0.58 4.70 6.8
Four-Seam 86.62 1945.21 204.03 8.06 19.41 BallCalled 6.63 0.42 4.67 6.8
Curveball 78.28 2455.12 14.95 -4.86 -16.67 StrikeCalled 6.48 0.68 4.30 0.5
Curveball 79.40 2305.92 16.59 -6.41 -20.10 FoulBallNotFieldable 6.44 0.67 4.26 0.6
Curveball 80.76 2457.95 21.25 -7.74 -18.36 FoulBallNotFieldable 6.48 0.58 4.40 0.7
Four-Seam 87.82 1989.46 199.07 6.03 18.71 FoulBallNotFieldable 6.79 0.64 4.91 6.6
Curveball 78.91 2489.99 20.83 -5.82 -13.91 StrikeCalled 6.48 0.74 4.45 0.7
Changeup 86.44 2012.50 216.26 11.26 16.46 BallCalled 6.74 0.46 4.65 7.2
Changeup 86.36 1802.37 221.55 12.26 14.98 StrikeCalled 6.62 0.65 4.72 7.4
Curveball 78.08 2547.96 24.49 -8.19 -16.47 BallCalled 6.50 0.69 4.41 0.8
Changeup 85.80 1883.92 218.42 14.63 19.65 BallCalled 6.67 0.47 4.64 7.3
Changeup 85.67 1811.72 210.58 10.63 19.16 BallCalled 6.60 0.63 4.61 7.0
Changeup 86.80 1855.15 221.24 12.45 15.31 BallCalled 6.62 0.60 4.64 7.4
Changeup 87.49 1854.01 211.48 10.29 17.90 BallCalled 6.64 0.68 4.69 7.0
Changeup 87.40 1970.83 213.73 12.96 20.57 StrikeCalled 6.59 0.69 4.68 7.1
Changeup 86.67 1883.82 216.67 11.10 15.99 StrikeCalled 6.61 0.55 4.79 7.2
Curveball 79.32 2504.33 6.52 -1.82 -14.53 BallCalled 6.41 0.64 4.44 0.2
Curveball 78.92 2410.06 21.42 -7.08 -16.61 StrikeCalled 6.47 0.63 4.55 0.7
Four-Seam 87.01 2031.01 215.80 13.17 19.39 StrikeCalled 6.64 0.55 4.64 7.2
Curveball 79.29 2524.00 19.69 -6.69 -17.28 BallCalled 6.40 0.60 4.52 0.7
Changeup 86.42 1948.26 211.96 10.76 18.37 InPlay 6.68 0.63 4.50 7.1
Changeup 86.76 1915.37 220.14 13.17 16.88 BallCalled 6.70 0.54 4.73 7.3
Changeup 86.51 1881.98 202.43 8.55 21.98 StrikeSwinging 6.60 0.55 4.65 6.7
Curveball 79.07 2488.54 36.21 -9.79 -11.87 BallCalled 6.39 0.59 4.31 1.2
Curveball 78.13 2417.29 14.15 -4.78 -17.50 StrikeCalled 6.38 0.69 4.37 0.5
Curveball 78.60 2536.15 32.69 -7.80 -10.67 BallCalled 6.44 0.66 4.41 1.1
Curveball 78.40 2375.73 6.06 -1.69 -14.49 InPlay 6.43 0.65 4.37 0.2
Changeup 86.49 1950.85 211.11 10.18 18.12 FoulBallNotFieldable 6.67 0.65 4.33 7.0
Curveball 77.56 2387.84 17.27 -5.19 -15.11 InPlay 6.37 0.75 4.34 0.6
# 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:  41
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Colin Burke")
Summary Pitch Table for Colin Burke
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 39.02% 16 8 8 50.00% 50.00% 86.89 1871.39 17.78 11.39 214.50 7.1 6.65 0.59 4.68
Curveball 43.90% 18 7 11 38.89% 61.11% 78.85 2437.29 -16.09 -6.64 20.81 0.7 6.44 0.69 4.40
Four-Seam 17.07% 7 3 4 42.86% 57.14% 87.54 1969.64 19.74 9.25 206.37 6.9 6.68 0.55 4.73
# 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("Colin Burke maximum FB velocity: ", max_fb_velocity, "mph\n")
## Colin Burke maximum FB velocity:  88.56 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 Colin Burke",
       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")
  )