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

# 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 Max Simpson")
Detailed Pitch Table for Max Simpson
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 90.00 2137.92 245.05 18.57 9.82 FoulBallNotFieldable 5.66 2.25 5.74 8.2
Changeup 85.28 1457.02 274.84 12.83 0.11 BallCalled 5.57 2.48 5.68 9.2
Sinker 88.39 2013.75 254.52 18.37 6.20 InPlay 5.64 2.41 5.95 8.5
Sinker 87.67 1987.43 233.29 14.46 11.92 BallCalled 5.57 2.32 5.97 7.8
Sinker 88.97 2156.12 258.04 21.74 5.76 BallCalled 5.66 2.27 5.82 8.6
Sinker 87.14 2032.64 232.26 12.93 11.14 StrikeCalled 5.63 2.44 5.80 7.7
Curveball 78.10 1993.68 60.68 -9.50 -3.94 BallCalled 5.85 1.99 5.66 2.0
Changeup 87.83 1995.17 251.94 16.88 6.58 FoulBallNotFieldable 5.52 2.31 5.93 8.4
Changeup 83.35 1240.73 268.94 10.09 1.49 InPlay 5.47 2.48 5.49 9.0
Slider 80.06 1898.13 178.48 -0.12 6.10 InPlay 5.77 2.19 5.70 5.9
Sinker 89.23 2070.00 247.22 15.78 7.69 BallCalled 5.69 2.12 5.89 8.2
Sinker 88.76 2091.42 252.97 18.17 6.63 FoulBallNotFieldable 5.69 2.16 5.92 8.4
Slider 80.77 2154.61 93.95 -16.09 2.68 BallCalled 5.71 2.01 5.51 3.1
Four-Seam 88.99 2214.59 219.11 13.11 17.24 InPlay 5.93 1.85 6.11 7.3
Changeup 88.03 2080.74 259.77 18.35 4.42 BallCalled 5.57 2.42 5.87 8.7
Sinker 88.02 2051.24 229.60 12.21 11.55 BallCalled 5.61 2.42 5.72 7.7
Sinker 88.02 2098.94 260.71 16.88 3.83 BallCalled 5.49 2.44 6.03 8.7
Sinker 87.85 2287.95 225.89 13.86 14.51 StrikeCalled 5.72 2.34 6.08 7.5
Four-Seam 88.45 2262.14 218.89 12.95 17.16 BallCalled 5.83 2.22 5.89 7.3
Sinker 88.55 2062.77 262.77 19.24 3.54 StrikeCalled 5.49 2.55 5.96 8.8
Changeup 84.70 1564.23 281.58 13.49 -1.62 StrikeSwinging 5.52 2.32 5.56 9.4
Changeup 85.13 1602.18 267.46 12.57 1.75 StrikeSwinging 5.34 2.57 5.71 8.9
Sinker 88.34 1970.53 254.37 19.41 6.53 FoulBallNotFieldable 5.61 2.34 5.96 8.5
Splitter 82.55 1165.85 266.25 6.18 1.63 StrikeCalled 5.48 2.52 5.55 8.9
Changeup 83.64 1284.61 273.64 8.24 0.67 InPlay 5.36 2.55 5.62 9.1
Changeup 87.66 1824.60 225.63 14.41 15.22 BallCalled 5.62 2.32 5.99 7.5
Changeup 88.00 1856.28 248.51 18.56 8.44 FoulBallNotFieldable 5.54 2.32 6.02 8.3
Curveball 76.96 1945.35 38.29 -5.88 -6.00 BallCalled 5.90 1.88 5.48 1.3
Splitter 81.71 1096.34 156.75 -2.38 6.92 StrikeCalled 5.48 2.30 5.75 5.2
Curveball 77.60 2025.15 45.05 -7.55 -6.14 BallCalled 5.74 2.01 5.65 1.5
Four-Seam 89.81 2234.15 222.76 14.23 16.50 InPlay 5.87 1.90 5.84 7.4
Sinker 88.36 1986.08 244.87 17.27 9.16 InPlay 5.65 2.37 6.03 8.2
Changeup 87.46 1980.27 247.59 17.57 8.38 BallCalled 5.57 2.55 5.91 8.3
Changeup 86.63 2005.88 251.38 17.68 7.13 StrikeCalled 5.47 2.51 6.03 8.4
Changeup 83.29 1522.30 239.78 12.89 8.83 InPlay 5.40 2.70 5.75 8.0
Sinker 85.67 1980.54 239.87 14.12 9.55 BallCalled 5.59 2.55 5.96 8.0
Changeup 82.22 1435.31 258.45 11.08 3.73 InPlay 5.30 2.76 5.97 8.6
Sinker 87.94 1949.52 240.79 16.06 10.23 HitByPitch 5.67 2.38 5.99 8.0
Sinker 86.70 2002.04 239.10 14.81 10.06 BallCalled 5.65 2.37 6.03 8.0
Changeup 86.37 1988.64 234.18 15.72 12.58 StrikeSwinging 5.59 2.31 6.01 7.8
Curveball 76.57 1966.64 43.49 -11.48 -10.60 InPlay 5.84 1.81 5.61 1.4
Sinker 86.92 2127.77 236.01 16.94 12.63 StrikeSwinging 5.75 2.44 5.98 7.9
Changeup 83.91 1647.32 265.80 13.58 2.23 BallCalled 5.45 2.69 5.82 8.9
Changeup 87.58 2075.75 251.80 18.36 7.21 FoulBallNotFieldable 5.52 2.47 5.83 8.4
Curveball 78.12 1987.62 51.43 -11.71 -7.92 BallCalled 5.78 2.15 5.72 1.7
Changeup 87.61 1866.49 251.53 18.78 7.39 FoulBallNotFieldable 5.69 2.43 5.94 8.4
Changeup 83.60 1244.43 268.29 12.68 1.64 BallCalled 5.45 2.51 5.71 8.9
Four-Seam 87.85 2156.31 216.91 11.19 16.08 InPlay 5.68 2.16 6.05 7.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 == "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:  48
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Max Simpson")
Summary Pitch Table for Max Simpson
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 37.50% 18 6 12 33.33% 66.67% 85.68 1704.00 5.34 14.65 256.73 8.6 5.50 2.48 5.82
Curveball 10.42% 5 4 1 80.00% 20.00% 77.47 1983.69 -6.92 -9.22 47.79 1.6 5.82 1.97 5.62
Four-Seam 8.33% 4 1 3 25.00% 75.00% 88.78 2216.80 16.74 12.87 219.42 7.3 5.83 2.03 5.97
Sinker 35.42% 17 7 10 41.18% 58.82% 88.03 2059.22 8.87 16.52 244.55 8.2 5.63 2.36 5.93
Slider 4.17% 2 1 1 50.00% 50.00% 80.41 2026.37 4.39 -8.11 136.22 4.5 5.74 2.10 5.61
Splitter 4.17% 2 0 2 0.00% 100.00% 82.13 1131.09 4.28 1.90 211.50 7.1 5.48 2.41 5.65
# 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("Max Simpson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Max Simpson maximum FB velocity:  90 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 Max Simpson",
       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")
  )