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

# 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 Matthew Martinez")
Detailed Pitch Table for Matthew Martinez
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 75.35 1645.97 249.08 16.29 7.97 StrikeCalled 6.41 1.76 3.84 8.3
Curveball 76.85 2566.65 33.24 -6.85 -8.75 BallCalled 6.41 1.51 4.01 1.1
Changeup 75.79 1492.39 251.68 16.89 7.40 StrikeCalled 6.35 1.53 4.13 8.4
Curveball 77.09 2452.49 36.13 -4.88 -4.84 BallCalled 6.44 1.59 3.99 1.2
Changeup 76.82 1663.73 262.88 19.16 4.11 StrikeCalled 6.35 1.72 4.02 8.8
Curveball 76.82 2515.40 36.65 -5.65 -6.03 StrikeCalled 6.54 1.67 4.00 1.2
Changeup 76.71 1640.40 244.62 16.72 9.49 InPlay 6.38 1.57 4.10 8.2
Curveball 76.96 2421.69 35.06 -4.84 -5.42 StrikeCalled 6.48 1.55 4.06 1.2
Changeup 76.68 1633.58 255.17 17.27 6.27 BallCalled 6.34 1.72 4.48 8.5
Changeup 76.68 1656.94 260.34 19.31 4.98 StrikeCalled 6.38 1.63 4.17 8.7
Curveball 77.79 2613.95 35.20 -5.81 -6.74 BallCalled 6.44 1.32 4.20 1.2
Curveball 77.92 2598.86 30.83 -4.41 -5.91 BallCalled 6.45 1.58 4.24 1.0
Changeup 77.73 1651.39 223.34 9.67 11.84 StrikeSwinging 6.43 1.55 4.02 7.4
Four-Seam 88.52 1945.45 221.55 12.41 15.22 StrikeSwinging 6.61 1.11 4.46 7.4
Changeup 76.56 1780.97 259.37 14.86 4.24 BallCalled 6.27 1.52 4.34 8.6
Curveball 77.44 2484.41 36.92 -6.35 -6.93 BallCalled 6.41 1.40 4.12 1.2
Curveball 77.58 2498.03 24.90 -3.48 -5.89 InPlay 6.45 1.44 4.00 0.8
Four-Seam 88.99 2024.05 200.17 7.83 22.61 FoulBallNotFieldable 6.59 0.99 4.45 6.7
Curveball 75.73 2363.66 45.07 -10.40 -8.54 BallCalled 6.40 1.57 3.99 1.5
Changeup 78.11 1617.43 223.04 8.40 10.65 FoulBallNotFieldable 6.30 1.61 4.35 7.4
Four-Seam 90.11 1909.56 212.85 10.53 17.40 BallCalled 6.54 1.04 4.29 7.1
Curveball 76.58 2399.76 34.92 -4.71 -5.16 InPlay 6.35 1.42 4.03 1.2
Changeup 78.94 1800.01 238.71 14.82 10.47 BallCalled 6.30 1.45 4.25 8.0
Four-Seam 89.11 1974.40 215.85 11.93 17.62 BallCalled 6.48 0.97 4.46 7.2
Changeup 78.31 1782.75 230.60 12.72 11.96 BallCalled 6.23 1.41 4.76 7.7
Changeup 88.11 1812.87 209.39 7.81 15.01 BallCalled 6.62 1.01 4.52 7.0
Changeup 76.80 1702.64 234.80 11.82 9.95 InPlay 6.37 1.33 4.05 7.8
Changeup 77.36 1772.50 247.38 13.22 7.08 StrikeCalled 6.28 1.61 4.27 8.2
Curveball 74.73 2480.07 49.77 -8.75 -5.87 BallCalled 6.40 1.63 3.88 1.7
Four-Seam 88.51 2058.06 211.53 10.93 19.05 StrikeSwinging 6.49 1.25 4.53 7.1
Four-Seam 88.72 1978.72 205.36 7.76 17.48 StrikeSwinging 6.55 0.87 4.38 6.8
Curveball 76.96 2463.98 21.45 -3.85 -8.39 StrikeSwinging 6.33 1.66 4.05 0.7
Curveball 78.98 2502.25 48.86 -7.72 -5.48 StrikeSwinging 6.40 1.32 3.78 1.6
Four-Seam 88.47 1942.02 211.39 9.34 16.47 FoulBallNotFieldable 6.46 0.85 4.39 7.0
Curveball 76.77 2428.27 51.29 -9.85 -6.18 BallCalled 6.40 1.29 4.00 1.7
Changeup 79.91 1726.73 250.22 15.32 6.97 InPlay 6.18 1.51 4.77 8.3
Curveball 78.10 2514.51 50.35 -11.09 -7.64 BallCalled 6.45 1.34 4.25 1.7
Changeup 78.91 1767.12 238.67 15.61 11.12 BallCalled 6.27 1.67 4.70 8.0
Changeup 78.66 1729.71 254.87 16.51 6.14 FoulBallNotFieldable 6.28 1.65 4.57 8.5
Curveball 76.38 2389.23 39.14 -6.12 -5.93 BallCalled 6.33 1.46 4.02 1.3
Changeup 79.01 1649.14 268.19 13.95 1.90 StrikeSwinging 6.37 1.52 4.39 8.9
Four-Seam 89.39 1903.01 197.73 5.60 18.69 InPlay 6.47 0.76 4.50 6.6
Changeup 78.71 1756.60 243.25 16.30 9.75 BallCalled 6.25 1.47 4.89 8.1
Changeup 77.59 1731.70 226.76 8.74 9.71 BallCalled 6.31 1.41 4.70 7.6
Four-Seam 88.31 1999.15 198.47 6.47 20.62 StrikeCalled 6.54 0.90 4.37 6.6
Changeup 79.03 1745.08 245.56 13.02 7.32 BallCalled 6.24 1.42 4.76 8.2
Changeup 78.88 1735.77 244.19 13.93 8.25 StrikeCalled 6.37 1.39 4.39 8.1
Four-Seam 90.03 2014.72 199.30 5.96 18.15 FoulBallNotFieldable 6.47 1.17 4.46 6.6
Changeup 79.31 1821.75 244.82 17.06 9.60 BallCalled 6.24 1.57 4.57 8.2
Slider 78.33 2302.17 58.63 -7.23 -2.86 BallCalled 6.33 1.35 4.07 2.0
Changeup 78.75 1615.85 255.69 20.03 6.72 InPlay 6.29 1.60 4.45 8.5
Four-Seam 88.10 2043.50 222.19 12.62 15.06 BallCalled 6.45 1.24 4.58 7.4
Four-Seam 88.22 1942.05 215.05 12.28 18.68 BallCalled 6.56 1.17 4.58 7.2
Changeup 78.40 1598.92 241.17 17.16 10.97 InPlay 6.27 1.36 4.61 8.0
Changeup 77.52 1616.36 245.40 16.53 9.04 FoulBallNotFieldable 6.29 1.53 4.45 8.2
Four-Seam 88.39 1964.72 216.95 11.50 16.37 FoulBallNotFieldable 6.44 1.09 4.63 7.2
Changeup 79.31 1651.92 231.71 9.56 8.98 InPlay 6.25 1.37 4.64 7.7
# 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:  57
# 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 47.37% 27 10 17 37.04% 62.96% 78.29 1696.30 8.44 14.54 243.74 8.1 6.32 1.51 4.41
Curveball 28.07% 16 10 6 62.50% 37.50% 77.04 2480.83 -6.48 -6.55 38.11 1.3 6.42 1.48 4.04
Four-Seam 22.81% 13 4 9 30.77% 69.23% 88.84 1976.88 17.96 9.63 209.88 7.0 6.51 1.03 4.47
Slider 1.75% 1 1 0 100.00% 0.00% 78.33 2302.17 -2.86 -7.23 58.63 2.0 6.33 1.35 4.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("Matthew Martinez maximum FB velocity: ", max_fb_velocity, "mph\n")
## Matthew Martinez maximum FB velocity:  90.11 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 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"),
       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")
  )