# Filter the data for the pitcher Matthew Martinez
Matthew_Martinez_data <- data %>%
  filter(Pitcher == "Martinez, Matthew")

# Create a detailed table for each pitch
detailed_pitch_table <- Matthew_Martinez_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 Matthew Martinez")
Detailed Pitch Table for Matthew Martinez
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 88.71 2079.55 209.42 13.04 24.46 BallCalled 6.35 0.72 4.80 7.0
Changeup 78.25 1779.39 244.39 18.94 10.74 StrikeCalled 6.01 1.44 5.01 8.1
Changeup 78.46 1769.59 243.87 17.96 10.36 BallCalled 6.00 1.41 4.96 8.1
Four-Seam 88.68 2138.61 210.68 12.85 23.05 FoulBallNotFieldable 6.30 0.98 4.81 7.0
Changeup 79.42 1719.05 258.14 16.63 5.08 StrikeSwinging 5.99 1.44 4.89 8.6
Curveball 76.84 2365.26 59.91 -8.06 -2.97 StrikeCalled 6.21 1.27 4.56 2.0
Changeup 78.52 1595.74 245.26 14.96 8.47 StrikeSwinging 6.00 1.34 5.05 8.2
Changeup 78.19 1633.64 253.71 14.14 5.62 StrikeSwinging 6.00 1.14 4.91 8.5
Changeup 79.77 1879.12 248.73 15.76 7.61 StrikeCalled 6.12 1.45 4.64 8.3
Four-Seam 87.29 1974.77 205.60 8.58 19.16 FoulBallNotFieldable 6.26 0.83 4.94 6.9
Changeup 80.60 1725.41 240.12 13.49 9.25 StrikeSwinging 6.09 1.18 4.93 8.0
NA NA NA NA NA NA StrikeCalled NA NA NA NA
Sinker 87.27 2008.96 225.52 15.61 16.60 StrikeSwinging 6.24 0.92 4.85 7.5
Changeup 80.25 1732.23 246.53 15.58 8.14 BallinDirt 5.95 1.45 5.45 8.2
Changeup 81.24 1808.35 252.90 17.79 6.86 BallCalled 6.05 1.41 5.13 8.4
Curveball 77.04 2493.84 62.23 -14.30 -5.93 StrikeCalled 6.09 1.34 4.47 2.1
Changeup 79.85 1617.42 254.81 17.30 6.22 StrikeSwinging 6.08 1.21 4.96 8.5
Sinker 88.71 2040.92 224.28 14.33 16.00 BallCalled 6.34 0.96 4.74 7.5
Changeup 80.17 1787.74 255.47 17.50 5.97 StrikeSwinging 6.06 1.45 5.28 8.5
Changeup 80.15 1701.14 250.92 17.47 7.53 InPlay 6.00 1.48 5.14 8.4
Sinker 88.62 1911.08 222.92 13.43 15.63 FoulBallNotFieldable 6.13 0.88 5.04 7.4
Changeup 79.46 1713.77 246.82 15.50 8.06 StrikeSwinging 6.07 1.24 4.52 8.2
Changeup 80.14 1752.30 242.57 16.37 9.84 InPlay 5.97 1.14 5.11 8.1
Curveball 77.37 2424.08 61.80 -9.25 -3.33 BallCalled 6.25 1.30 4.58 2.1
Curveball 77.58 2431.66 59.72 -6.97 -2.57 StrikeCalled 6.19 1.45 4.68 2.0
Curveball 77.16 2360.48 55.85 -9.07 -4.66 BallCalled 6.11 1.24 4.66 1.9
Changeup 79.82 1675.61 246.19 14.03 7.75 StrikeSwinging 6.16 1.30 4.87 8.2
Curveball 77.80 2585.15 68.85 -14.75 -4.09 StrikeSwinging 6.15 1.24 4.68 2.3
# 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:  28
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Matthew Martinez")
Summary Pitch Table for Matthew Martinez
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 53.57% 15 2 13 13.33% 86.67% 79.62 1726.03 7.83 16.23 248.70 8.3 6.04 1.34 4.99
Curveball 21.43% 6 2 4 33.33% 66.67% 77.30 2443.41 -3.92 -10.40 61.39 2.1 6.17 1.31 4.60
Four-Seam 10.71% 3 1 2 33.33% 66.67% 88.23 2064.31 22.22 11.49 208.57 7.0 6.30 0.84 4.85
Sinker 10.71% 3 1 2 33.33% 66.67% 88.20 1986.99 16.08 14.46 224.24 7.5 6.24 0.92 4.88
NA 3.57% 1 0 1 0.00% 100.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("Matthew Martinez maximum FB velocity: ", max_fb_velocity, "mph\n")
## Matthew Martinez maximum FB velocity:  88.71 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Matthew_Martinez_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 Matthew Martinez",
       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)) +
  scale_y_continuous(limits = c(-25, 25)) +
  labs(title = "Pitch Movement for Matthew Martinez",
       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")
  )