# Filter the data for the pitcher Max Moss
Brian_Foley_data <- data %>%
  filter(Pitcher == "Moss, 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 Moss")
Detailed Pitch Table for Max Moss
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 76.95 1657.61 250.71 19.28 8.33 StrikeCalled 4.96 3.48 5.25 8.4
Changeup 86.46 1877.05 231.25 14.89 13.28 FoulBallNotFieldable 4.91 3.36 5.29 7.7
Changeup 76.29 1346.41 254.89 15.68 5.69 FoulBallNotFieldable 4.73 3.46 5.27 8.5
Changeup 77.06 1613.12 260.59 17.56 4.42 FoulBallNotFieldable 4.83 3.51 5.37 8.7
Changeup 86.49 1801.13 242.86 18.14 10.56 BallCalled 4.86 3.29 5.21 8.1
Changeup 78.10 1839.85 251.95 19.54 7.86 FoulBallNotFieldable 4.75 3.44 5.33 8.4
Changeup 86.40 1916.99 249.54 19.30 8.43 StrikeCalled 4.99 3.12 5.46 8.3
Changeup 78.33 1735.02 258.93 15.82 4.55 StrikeCalled 4.80 3.38 5.24 8.6
Changeup 78.25 1883.87 258.63 19.01 5.32 BallCalled 4.64 3.38 5.17 8.6
Changeup 87.26 1872.80 231.65 15.54 13.50 StrikeCalled 4.97 3.29 5.40 7.7
Changeup 78.89 1679.63 261.60 14.51 3.56 InPlay 4.62 3.39 5.29 8.7
Changeup 86.57 2008.58 233.66 15.00 12.29 BallCalled 4.87 3.48 5.30 7.8
Changeup 85.84 1706.32 234.62 16.34 12.84 BallCalled 4.90 3.48 5.22 7.8
Changeup 86.31 1619.05 256.72 17.82 5.36 FoulBallNotFieldable 4.94 3.31 5.34 8.6
Changeup 79.18 1843.60 264.78 18.16 3.11 StrikeSwinging 4.73 3.37 5.37 8.8
Changeup 78.12 1787.16 255.33 18.33 6.35 BallCalled 4.72 3.46 5.39 8.5
Changeup 77.58 1791.62 258.17 19.20 5.62 StrikeSwinging 4.69 3.43 5.22 8.6
Changeup 87.91 1958.69 243.40 18.95 10.76 StrikeCalled 4.86 3.25 5.30 8.1
Curveball 76.84 2158.14 69.03 -12.18 -3.12 BallCalled 4.71 3.43 4.92 2.3
Slider 76.56 2280.31 90.60 -11.93 1.60 FoulBallNotFieldable 4.75 3.43 5.07 3.0
Changeup 79.03 1751.55 264.94 13.83 2.74 BallCalled 4.59 3.32 5.43 8.8
Changeup 87.30 1886.34 231.49 13.22 11.73 BallCalled 4.86 3.13 5.39 7.7
Curveball 77.34 2228.27 49.40 -8.89 -6.11 InPlay 4.87 3.03 5.11 1.6
# 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:  23
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Max Moss")
Summary Pitch Table for Max Moss
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 86.96% 20 7 13 35.00% 65.00% 81.92 1778.82 7.82 17.01 249.79 8.3 4.81 3.37 5.31
Curveball 8.70% 2 1 1 50.00% 50.00% 77.09 2193.20 -4.62 -10.54 59.22 2.0 4.79 3.23 5.02
Slider 4.35% 1 0 1 0.00% 100.00% 76.56 2280.31 1.60 -11.93 90.60 3.0 4.75 3.43 5.07
# 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 Moss maximum FB velocity: ", max_fb_velocity, "mph\n")
## Max Moss maximum FB velocity:  -Inf 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 Moss",
       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")
  )