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

# Create a detailed table for each pitch
detailed_pitch_table <- Max_Simpson_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
Four-Seam 85.91 2057.99 201.19 6.28 17.58 BallCalled 5.99 1.86 6.85 6.7
Sinker 85.55 2010.23 226.93 16.73 17.10 FoulBallNotFieldable 5.69 2.15 6.86 7.6
Changeup 86.11 1986.61 242.89 18.07 10.69 BallCalled 5.78 2.00 6.71 8.1
Sinker 87.38 1993.02 244.54 17.84 9.77 InPlay 5.64 2.16 6.68 8.2
Changeup 85.73 1973.71 238.13 17.26 12.21 BallCalled 5.62 2.11 6.68 7.9
Curveball 76.54 1812.72 64.21 -9.72 -3.01 BallCalled 5.82 1.86 6.03 2.1
Sinker 87.91 2039.69 237.49 16.04 11.49 StrikeCalled 5.54 2.16 6.68 7.9
Changeup 86.64 1959.17 245.81 18.38 9.61 InPlay 5.51 2.12 6.72 8.2
Curveball 76.72 1782.84 33.15 -5.81 -6.94 StrikeCalled 5.99 1.57 6.06 1.1
Curveball 76.39 1874.89 104.54 -13.93 5.36 InPlay 5.71 2.03 6.12 3.5
Curveball 76.48 1862.09 60.00 -9.22 -3.61 StrikeCalled 5.81 1.96 6.34 2.0
Sinker 87.90 2078.50 237.54 16.56 11.86 BallCalled 5.54 2.11 6.75 7.9
Curveball 76.04 1785.82 68.04 -9.62 -2.13 InPlay 5.77 1.97 6.33 2.3
Four-Seam 85.35 1976.17 215.94 13.53 19.95 FoulBallNotFieldable 5.59 2.08 6.90 7.2
Splitter 81.30 1174.02 247.34 7.15 4.35 StrikeSwinging 5.43 2.38 6.30 8.2
Curveball 77.75 1917.97 57.93 -5.71 -2.06 StrikeSwinging 5.82 1.98 6.35 1.9
Curveball 77.48 1835.64 89.03 -5.90 1.48 StrikeCalled 6.04 1.72 6.43 3.0
Four-Seam 87.57 2108.35 210.62 9.47 17.17 BallCalled 5.94 1.94 6.70 7.0
Curveball 76.66 1991.95 66.92 -5.84 -0.82 InPlay 5.97 1.85 6.05 2.2
Sinker 86.89 2045.85 232.73 14.90 12.55 StrikeCalled 5.67 2.01 6.76 7.8
Curveball 76.22 1853.08 67.17 -6.51 -1.12 FoulBallNotFieldable 5.82 1.78 6.25 2.2
Four-Seam 88.34 2151.50 210.55 11.09 20.21 InPlay 5.97 1.71 6.78 7.0
Changeup 87.64 2106.71 249.33 16.69 7.48 InPlay 5.43 2.25 6.70 8.3
Curveball 75.01 1846.16 46.05 -6.74 -4.75 StrikeCalled 5.88 1.89 6.25 1.5
Four-Seam 85.84 2058.32 200.14 5.89 17.32 BallCalled 5.66 2.11 6.82 6.7
Curveball 74.99 1835.93 47.04 -5.29 -3.18 InPlay 5.96 1.63 6.41 1.6
Slider 78.89 2003.12 143.08 -7.25 11.54 StrikeCalled 5.78 1.87 6.24 4.8
Curveball 76.35 1883.10 44.52 -7.90 -6.25 BallCalled 5.90 1.71 6.27 1.5
Changeup 86.58 1977.71 245.52 19.75 10.39 FoulBallNotFieldable 5.61 2.17 6.71 8.2
Curveball 78.08 2006.40 60.08 -5.65 -1.64 BallCalled 5.92 1.85 6.25 2.0
Sinker 87.36 1952.74 237.67 15.93 11.35 StrikeSwinging 5.53 2.21 6.86 7.9
Sinker 87.41 2115.35 248.55 17.41 8.06 InPlay 5.63 2.11 6.77 8.3
NA NA NA NA NA NA BallCalled NA NA NA NA
Sinker 87.25 2028.79 224.15 13.29 14.90 InPlay 5.62 2.04 6.74 7.5
Changeup 87.87 1950.10 245.76 17.19 8.97 FoulBallNotFieldable 5.55 2.11 6.95 8.2
Splitter 80.66 1094.65 245.42 3.39 2.97 InPlay 5.34 2.41 6.53 8.2
Curveball 75.89 1932.61 70.89 -6.35 -0.66 BallCalled 5.81 2.00 6.24 2.4
Sinker 87.35 1988.10 234.71 13.43 10.70 BallCalled 5.68 2.25 6.53 7.8
Sinker 87.30 1964.00 235.92 14.38 10.92 InPlay 5.63 2.25 6.62 7.9
Curveball 76.47 1800.47 66.20 -3.04 0.26 StrikeCalled 5.82 1.75 6.51 2.2
Curveball 76.22 1850.93 66.76 -4.53 -0.36 StrikeCalled 5.95 1.77 6.48 2.2
Curveball 76.28 1935.34 53.13 -3.74 -1.18 StrikeSwinging 5.88 1.69 6.42 1.8
Curveball 75.55 1792.75 63.43 -7.46 -2.17 StrikeCalled 5.90 1.68 6.41 2.1
Curveball 75.53 1893.15 61.55 -6.67 -2.15 StrikeCalled 5.95 1.63 6.42 2.1
Curveball 75.08 1841.88 65.44 -4.25 -0.38 InPlay 5.92 1.69 6.26 2.2
Curveball 75.09 1849.20 64.15 -8.89 -2.76 StrikeCalled 5.72 1.84 6.31 2.1
Changeup 82.08 1230.36 259.69 7.57 2.60 StrikeCalled 5.39 2.25 6.44 8.7
Changeup 84.27 1289.92 269.72 12.24 1.34 StrikeSwinging 5.45 2.32 6.38 9.0
Sinker 88.89 2136.16 226.06 14.19 14.85 StrikeCalled 5.79 1.75 6.96 7.5
Sinker 88.74 2183.78 238.60 16.81 11.39 FoulBallNotFieldable 5.63 1.96 6.75 8.0
Sinker 88.20 2022.00 229.67 12.72 12.03 BallCalled 5.69 1.85 6.72 7.7
Slider 79.09 1928.38 110.76 -7.46 4.59 BallCalled 5.51 2.00 6.03 3.7
Sinker 87.73 1985.50 233.71 15.99 13.00 InPlay 5.60 1.90 6.89 7.8
Sinker 88.73 2213.05 224.68 12.53 13.79 InPlay 5.72 1.94 6.75 7.5
# 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:  54
# 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 14.81% 8 2 6 25.00% 75.00% 85.86 1809.29 7.91 15.89 249.61 8.3 5.54 2.17 6.66
Curveball 38.89% 21 4 17 19.05% 80.95% 76.23 1865.95 -1.81 -6.80 62.87 2.1 5.87 1.80 6.29
Four-Seam 9.26% 5 3 2 60.00% 40.00% 86.60 2070.47 18.45 9.25 207.69 6.9 5.83 1.94 6.81
Sinker 27.78% 15 3 12 20.00% 80.00% 87.64 2050.45 12.25 15.25 234.20 7.8 5.64 2.06 6.75
Slider 3.70% 2 1 1 50.00% 50.00% 78.99 1965.75 8.06 -7.36 126.92 4.2 5.64 1.94 6.14
Splitter 3.70% 2 0 2 0.00% 100.00% 80.98 1134.34 3.66 5.27 246.38 8.2 5.38 2.40 6.42
NA 1.85% 1 1 0 100.00% 0.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", "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:  88.89 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Max_Simpson_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
  labs(title = "Pitch Movement for Max Simpson",
       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")
  )