# 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
Changeup 74.63 1647.45 248.03 16.01 8.13 BallCalled 6.02 1.47 4.39 8.3
Changeup 74.62 1561.19 254.60 14.94 5.78 StrikeCalled 5.98 1.68 4.50 8.5
Changeup 86.50 1891.10 212.36 11.99 20.11 BallCalled 6.35 1.29 4.46 7.1
Changeup 75.49 1485.66 229.57 12.56 12.28 InPlay 6.10 1.52 4.75 7.7
Changeup 76.62 1690.30 248.61 14.59 7.22 StrikeCalled 6.15 1.55 4.53 8.3
Curveball 77.06 2428.99 22.47 -3.52 -7.24 BallCalled 6.14 1.55 4.41 0.7
Changeup 75.86 1647.62 251.58 14.43 6.48 InPlay 6.07 1.43 4.50 8.4
Curveball 76.30 2393.27 20.51 -3.09 -6.71 StrikeCalled 6.13 1.43 4.37 0.7
Changeup 76.14 1828.52 250.80 19.62 8.46 FoulBallNotFieldable 5.99 1.41 4.52 8.4
Changeup 76.25 1642.20 255.77 16.41 5.77 InPlay 5.87 1.57 4.58 8.5
Changeup 76.70 1783.08 236.14 13.41 10.65 FoulBallNotFieldable 6.09 1.48 4.34 7.9
Changeup 87.99 1550.35 232.37 15.02 12.89 InPlay 6.24 1.00 4.90 7.7
Changeup 78.09 1486.82 249.21 16.75 7.99 BallCalled 6.13 1.55 4.57 8.3
Changeup 77.55 1659.97 248.29 16.60 8.29 InPlay 6.04 1.65 4.48 8.3
Curveball 77.62 2345.58 25.63 -4.51 -7.73 BallCalled 6.16 1.23 4.37 0.9
Changeup 76.57 1804.36 243.36 18.70 11.12 BallCalled 6.04 1.59 4.19 8.1
Changeup 77.94 1598.21 241.89 16.02 10.21 FoulBallNotFieldable 6.00 1.29 4.32 8.1
Curveball 77.23 2335.17 27.25 -2.97 -4.15 StrikeCalled 6.15 1.40 4.45 0.9
Changeup 87.76 1612.09 224.29 12.43 13.97 InPlay 6.33 0.94 4.59 7.5
# 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:  19
# 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 78.95% 15 4 11 26.67% 73.33% 78.58 1659.26 9.96 15.30 241.79 8.1 6.09 1.43 4.51
Curveball 21.05% 4 2 2 50.00% 50.00% 77.05 2375.75 -6.46 -3.52 23.96 0.8 6.14 1.40 4.40
# 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:  -Inf 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)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement for", unique(detailed_pitch_table$AutoPitchType)),
       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")
  )