# Filter the data for the pitcher Moss
Moss_data <- Mystic612 %>%
  filter(Pitcher == "Moss, Max")

# Create a detailed table for each pitch
detailed_pitch_table <- Moss_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 Moss")
Detailed Pitch Table for Moss
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 77.44 2015.66 60.86 -4.32 -0.67 FoulBallNotFieldable 4.62 3.19 5.65 2.0
Changeup 85.48 1533.18 246.22 15.31 8.05 StrikeSwinging 4.72 3.05 6.04 8.2
Changeup 78.04 1637.38 270.98 19.58 1.25 FoulBallNotFieldable 4.60 3.06 6.02 9.0
Curveball 77.38 1864.07 81.71 -6.82 0.71 InPlay 4.59 3.16 5.73 2.7
Changeup 85.74 1453.14 237.76 12.32 9.07 BallCalled 4.71 3.33 5.90 7.9
Changeup 86.23 1687.78 232.21 14.06 12.16 StrikeSwinging 4.73 3.27 6.03 7.7
Changeup 77.06 1485.14 251.86 15.84 6.75 StrikeSwinging 4.54 3.52 5.93 8.4
Changeup 77.44 1605.51 262.98 17.65 3.79 BallCalled 4.56 3.32 5.88 8.8
Changeup 86.59 1691.98 238.80 15.38 10.54 FoulBallNotFieldable 4.72 3.33 5.93 8.0
Changeup 77.88 1809.60 244.26 15.58 9.16 FoulBallNotFieldable 4.52 3.34 6.19 8.1
Changeup 87.25 1854.40 231.46 15.89 14.09 BallCalled 4.76 3.12 6.09 7.7
Changeup 78.50 1739.60 248.89 15.04 7.32 BallCalled 4.44 3.35 6.35 8.3
Changeup 77.88 1832.71 252.43 15.82 6.59 BallCalled 4.63 3.23 6.18 8.4
Changeup 79.14 2038.97 254.85 21.30 7.45 BallinDirt 4.60 3.31 6.23 8.5
Changeup 87.54 1962.01 242.46 17.71 10.62 BallCalled 4.68 3.36 5.83 8.1
Changeup 87.21 1907.17 223.29 13.63 15.81 BallCalled 4.80 3.25 6.03 7.4
Sinker 88.41 1991.82 227.20 15.48 15.62 StrikeCalled 4.75 3.15 6.20 7.6
Changeup 79.15 2073.14 257.32 18.20 5.68 FoulBallNotFieldable 4.56 3.26 6.07 8.6
Changeup 77.13 1812.83 262.19 17.94 4.12 StrikeSwinging 4.52 3.49 6.26 8.7
Changeup 86.15 1767.15 235.37 15.62 12.16 BallCalled 4.73 3.19 5.85 7.8
Changeup 78.54 1820.21 250.52 15.80 7.17 BallCalled 4.59 3.23 6.18 8.4
Changeup 78.71 1840.93 249.49 14.33 6.87 BallCalled 4.49 3.34 6.05 8.3
Sinker 88.21 1955.98 238.92 16.87 11.42 BallCalled 4.69 3.21 6.03 8.0
# 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:  23
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Moss")
Summary Pitch Table for Moss
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 82.61% 19 10 9 52.63% 47.37% 81.67 1765.94 8.35 16.16 247.02 8.2 4.63 3.28 6.05
Curveball 8.70% 2 0 2 0.00% 100.00% 77.41 1939.87 0.02 -5.57 71.28 2.4 4.61 3.17 5.69
Sinker 8.70% 2 1 1 50.00% 50.00% 88.31 1973.90 13.52 16.18 233.06 7.8 4.72 3.18 6.12
# 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("Moss maximum FB velocity: ", max_fb_velocity, "mph\n")
## Moss maximum FB velocity:  88.41 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Moss_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 Moss",
       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 Moss",
       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")
  )