# Filter the data for the pitcher Ellison
Ellison_data <- Sanford64 %>%
  filter(Pitcher == "Ellison, Chris")

# Create a detailed table for each pitch
detailed_pitch_table <- Ellison_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 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 Ellison")
Detailed Pitch Table for Ellison
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ClockTilt
Four-Seam 90.94 2142.25 201.41 7.00 19.00 BallCalled 6.7
Curveball 76.24 1989.59 110.67 -7.33 4.37 FoulBallNotFieldable 3.7
Curveball 76.64 1904.26 88.69 -9.88 1.36 BallCalled 3.0
Four-Seam 89.21 1847.04 217.29 10.08 14.20 FoulBallNotFieldable 7.2
Four-Seam 90.73 2071.60 213.43 8.62 14.29 InPlay 7.1
Four-Seam 90.14 2055.47 218.98 11.19 15.08 BallCalled 7.3
Sinker 90.48 2111.47 232.08 14.51 12.48 FoulBallNotFieldable 7.7
Sinker 91.15 2221.41 231.48 16.54 14.26 BallCalled 7.7
Slider 78.39 2107.14 104.79 -6.70 3.44 BallCalled 3.5
Slider 78.20 2179.59 109.75 -5.53 3.20 BallCalled 3.7
Four-Seam 90.49 2155.80 216.39 12.48 18.02 InPlay 7.2
Four-Seam 90.08 2173.61 200.49 7.78 22.08 BallCalled 6.7
Four-Seam 89.25 2165.35 204.70 9.05 20.94 InPlay 6.8
Four-Seam 89.86 2239.85 204.09 9.34 22.13 FoulBallNotFieldable 6.8
Sinker 89.14 2261.75 227.19 15.91 15.94 BallCalled 7.6
Four-Seam 89.43 2222.76 210.35 8.68 15.93 FoulBallNotFieldable 7.0
Slider 78.19 2302.14 120.13 -6.31 5.18 InPlay 4.0
Four-Seam 89.45 2313.95 217.74 13.82 18.98 StrikeCalled 7.3
Four-Seam 90.38 2291.52 220.85 14.63 18.04 BallCalled 7.4
Slider 77.31 2311.73 96.69 -9.08 2.45 FoulBallNotFieldable 3.2
Slider 79.42 2400.23 92.87 -4.16 1.85 BallCalled 3.1
Four-Seam 89.54 2218.15 213.88 11.78 18.65 StrikeSwinging 7.1
Four-Seam 88.24 2179.99 212.28 11.25 18.92 BallCalled 7.1
Slider 77.47 1062.79 138.00 -5.79 7.91 BallCalled 4.6
Four-Seam 89.14 2143.05 217.21 12.48 17.55 InPlay 7.2
Sinker 88.73 2080.05 231.82 16.45 14.04 BallCalled 7.7
Slider 79.67 1773.87 170.82 -0.47 4.13 BallCalled 5.7
Changeup 88.62 1841.64 231.96 14.11 12.06 BallCalled 7.7
Changeup 87.11 1851.26 208.52 7.86 15.73 StrikeCalled 7.0
Four-Seam 87.90 1965.22 210.78 9.88 17.81 BallCalled 7.0
Four-Seam 88.61 2019.89 214.16 9.75 15.53 BallCalled 7.1
Curveball 75.68 1951.35 74.63 -10.27 -0.91 BallCalled 2.5
Four-Seam 89.27 2057.33 214.05 10.36 16.50 BallCalled 7.1
Four-Seam 87.41 2059.50 209.44 9.60 18.27 StrikeCalled 7.0
Four-Seam 88.46 2017.02 212.80 10.41 17.36 InPlay 7.1
# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    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
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Ellison")
Summary Pitch Table for Ellison
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 2 1 1 50.00% 50.00% 87.87 1846.45 13.89 10.98 220.24 7.3
Curveball 3 2 1 66.67% 33.33% 76.19 1948.40 1.61 -9.16 91.33 3.1
Four-Seam 19 8 11 42.11% 57.89% 89.40 2123.12 17.86 10.43 212.12 7.1
Sinker 4 3 1 75.00% 25.00% 89.88 2168.67 14.18 15.85 230.64 7.7
Slider 7 5 2 71.43% 28.57% 78.38 2019.64 4.02 -5.43 119.01 4.0
# Prepare data for plotting pitch locations
pitch_location_data <- Ellison_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)) +
  labs(title = "Pitch Locations for Ellison",
       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 Ellison",
       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")
  )