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

# Create a detailed table for each pitch
detailed_pitch_table <- 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 Simpson")
Detailed Pitch Table for Simpson
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 87.76 2042.35 253.03 18.29 6.85 FoulBall 5.34 2.17 6.76 8.4
Changeup 85.74 1752.33 254.41 15.27 5.72 BallCalled 5.14 2.46 6.63 8.5
Sinker 89.97 2055.08 227.26 14.24 14.40 InPlay 5.39 2.16 6.46 7.6
Slider 80.06 1920.54 143.56 -6.21 9.71 BallCalled 5.53 1.63 6.76 4.8
Sinker 88.73 2049.00 227.93 15.37 15.20 InPlay 5.49 1.90 6.93 7.6
Slider 79.21 1917.74 150.33 -7.20 14.26 StrikeSwinging 5.39 1.77 5.93 5.0
Slider 79.85 2015.00 143.03 -8.09 12.08 BallCalled 5.39 1.77 6.39 4.8
Sinker 88.46 2106.42 224.98 14.14 15.45 FoulBall 5.50 1.80 6.90 7.5
Curveball 80.21 1885.09 52.92 -6.57 -3.45 BallCalled 5.76 1.31 6.39 1.8
Four-Seam 88.74 2235.84 209.76 10.78 20.20 InPlay 5.69 1.50 6.94 7.0
Slider 80.21 1972.45 171.51 -1.24 9.85 BallCalled 5.32 1.96 6.59 5.7
Curveball 77.65 1904.92 97.72 -12.68 3.40 StrikeCalled 5.24 2.21 6.65 3.3
Changeup 86.72 2053.04 216.86 11.71 16.87 BallCalled 5.30 1.97 6.72 7.2
Slider 79.47 2010.09 104.87 -13.73 5.41 StrikeSwinging 5.40 2.00 6.48 3.5
Curveball 79.25 1963.93 51.62 -7.70 -4.50 BallinDirt 5.55 1.65 6.62 1.7
Changeup 87.73 1996.89 245.03 19.86 10.60 StrikeCalled 5.33 1.98 6.81 8.2
Sinker 88.51 1967.34 250.00 18.23 7.87 BallCalled 5.45 1.85 6.84 8.3
Changeup 86.95 2014.02 256.25 18.37 5.82 BallCalled 5.36 1.87 6.67 8.5
Sinker 88.25 2072.76 223.46 14.14 16.13 InPlay 5.28 2.03 6.74 7.4
Changeup 87.10 2035.53 249.40 15.41 7.00 InPlay 5.12 2.22 6.70 8.3
Sinker 87.88 2003.68 235.93 17.14 12.83 BallCalled 5.33 2.00 6.62 7.9
Changeup 86.45 2087.41 250.12 15.14 6.74 BallCalled 5.12 2.21 6.67 8.3
Curveball 79.74 1907.51 57.67 -12.13 -6.17 StrikeCalled 5.54 1.60 6.33 1.9
Sinker 88.74 2099.74 246.70 15.47 7.87 FoulBall 5.24 2.11 6.84 8.2
Curveball 78.86 1843.40 56.05 -8.49 -4.20 InPlay 5.48 1.92 6.28 1.9
Changeup 87.63 1925.37 238.47 17.49 12.03 BallCalled 5.20 1.88 6.87 7.9
Changeup 87.08 2000.70 252.02 18.69 7.37 InPlay 5.13 2.14 6.91 8.4
Changeup 86.67 1894.68 242.72 16.29 9.73 StrikeCalled 5.16 2.14 6.42 8.1
Curveball 78.05 1897.07 79.59 -12.83 -0.80 BallCalled 5.36 1.90 6.16 2.7
Sinker 88.14 1960.50 243.21 16.19 9.42 StrikeCalled 5.22 2.17 6.89 8.1
Four-Seam 88.53 2137.74 213.82 10.96 17.68 FoulBall 5.47 1.87 7.01 7.1
Curveball 79.18 1910.69 57.96 -10.65 -5.30 InPlay 5.52 1.81 6.54 1.9
# 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\n")
## Total number of pitches thrown:  32
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Simpson")
Summary Pitch Table for Simpson
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 31.25% 10 5 5 50.00% 50.00% 86.98 1980.23 8.87 16.65 245.83 8.2 5.22 2.10 6.72
Curveball 21.88% 7 2 5 28.57% 71.43% 78.99 1901.80 -3.00 -10.15 64.79 2.2 5.49 1.77 6.42
Four-Seam 6.25% 2 0 2 0.00% 100.00% 88.63 2186.79 18.94 10.87 211.79 7.0 5.58 1.69 6.97
Sinker 25.00% 8 2 6 25.00% 75.00% 88.58 2039.32 12.40 15.62 234.93 7.8 5.36 2.00 6.78
Slider 15.62% 5 3 2 60.00% 40.00% 79.76 1967.16 10.26 -7.29 142.66 4.8 5.41 1.83 6.43
# 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("Simpson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Simpson maximum FB velocity:  89.97 mph
# Prepare data for plotting pitch locations
pitch_location_data <- 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) + # Increase point size
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + # Red box
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + # Black box
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) + # Gray box
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) + # Adjust ratio to shrink vertical distance
  labs(title = "Pitch Locations for Simpson",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each pitch type
  geom_text(aes(x = -1.75, y = 5, label = "RHH"), color = "black", size = 3, hjust = 0) + # Label for RHH
  geom_text(aes(x = 1.75, y = 5, label = "LHH"), color = "black", size = 3, hjust = 1) + # Label for LHH
  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 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")
  )