# 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 77.37 1509.08 237.35 14.40 11.06 BallCalled 6.33 1.41 4.36 7.9
Four-Seam 88.88 1941.20 204.49 8.40 19.84 FoulBallNotFieldable 6.50 0.93 4.57 6.8
Changeup 77.72 1514.62 234.14 12.12 10.64 StrikeCalled 6.31 1.58 4.23 7.8
Changeup 78.43 1551.09 233.43 12.05 10.77 FoulBallNotFieldable 6.28 1.48 4.25 7.8
Changeup 78.18 1766.33 253.95 17.35 6.71 BallCalled 6.26 1.79 4.38 8.5
Four-Seam 89.53 2023.64 200.54 7.11 20.27 BallCalled 6.52 0.90 4.25 6.7
Changeup 79.05 1807.70 248.54 14.56 7.34 StrikeSwinging 6.29 1.43 4.13 8.3
Changeup 79.06 1640.83 247.03 15.95 8.38 InPlay 6.36 1.38 4.08 8.2
Curveball 78.42 2377.37 12.39 -1.94 -7.16 InPlay 6.42 1.35 3.97 0.4
Curveball 76.42 2265.89 48.64 -5.62 -3.24 StrikeCalled 6.44 1.24 3.90 1.6
Changeup 77.54 1639.94 249.79 17.41 8.10 InPlay 6.25 1.60 4.12 8.3
Changeup 87.05 1884.18 213.08 11.51 19.07 BallCalled 6.51 0.82 4.40 7.1
Changeup 77.44 1627.84 247.88 17.67 8.88 StrikeCalled 6.32 1.64 4.27 8.3
Changeup 78.66 1690.56 264.87 16.95 3.14 BallCalled 6.23 1.64 4.18 8.8
Curveball 77.11 2380.88 40.96 -5.60 -4.79 BallCalled 6.42 1.45 3.82 1.4
Changeup 78.37 1549.03 239.53 15.46 10.76 StrikeCalled 6.27 1.56 4.07 8.0
Changeup 79.24 1751.87 254.56 18.45 6.61 BallCalled 6.27 1.34 4.35 8.5
Four-Seam 87.09 1973.55 203.99 10.07 24.05 StrikeCalled 6.49 1.07 4.40 6.8
Curveball 75.79 2320.15 17.86 -2.20 -5.08 BallCalled 6.39 1.33 3.93 0.6
Curveball 74.51 2435.65 32.15 -6.69 -8.88 StrikeCalled 6.43 1.25 3.80 1.1
Four-Seam 87.60 2077.50 203.22 10.01 24.71 InPlay 6.57 0.79 4.35 6.8
Four-Seam 88.65 1927.77 214.36 12.77 19.98 BallCalled 6.53 0.99 4.34 7.1
Changeup 78.81 1631.48 248.09 17.81 8.75 BallCalled 6.35 1.22 4.27 8.3
Changeup 79.45 1648.01 241.20 16.27 10.54 StrikeSwinging 6.30 1.27 4.22 8.0
Four-Seam 88.44 2020.41 216.44 12.46 18.17 InPlay 6.52 0.61 4.47 7.2
Changeup 76.52 1643.19 251.62 15.09 6.64 BallCalled 6.35 1.66 4.15 8.4
Changeup 75.53 1597.47 238.46 16.04 11.64 StrikeCalled 6.32 1.64 4.07 7.9
Changeup 87.07 1927.81 211.98 11.78 20.25 FoulBallNotFieldable 6.46 1.06 4.39 7.1
Changeup 78.10 1594.59 257.38 15.45 5.19 FoulBallNotFieldable 6.29 1.62 4.12 8.6
Changeup 78.30 1649.08 252.61 18.02 7.33 StrikeSwinging 6.17 1.80 3.99 8.4
Changeup 77.52 1576.75 251.76 17.48 7.47 InPlay 6.28 1.85 4.22 8.4
Curveball 76.75 2332.96 55.92 -11.50 -6.18 BallCalled 6.33 1.77 3.97 1.9
Changeup 77.73 1534.31 255.50 16.80 6.01 StrikeSwinging 6.27 1.59 4.18 8.5
Changeup 79.22 1772.21 250.22 18.73 8.35 BallCalled 6.18 1.79 4.28 8.3
Four-Seam 87.80 2010.34 200.52 6.75 19.31 InPlay 6.49 1.12 4.44 6.7
Changeup 76.66 1745.99 246.60 17.42 9.24 BallCalled 6.34 1.45 4.05 8.2
Four-Seam 87.06 2008.72 204.25 9.78 23.06 StrikeCalled 6.47 0.88 4.36 6.8
Changeup 76.68 1588.66 248.54 16.89 8.32 StrikeCalled 6.24 1.39 4.28 8.3
Curveball 76.87 2413.57 27.76 -4.64 -7.13 BallCalled 6.31 1.47 4.01 0.9
Changeup 77.02 1638.63 248.48 15.24 7.64 InPlay 6.28 1.41 4.18 8.3
Four-Seam 88.90 1980.35 205.88 9.83 21.52 StrikeCalled 6.42 0.87 4.27 6.9
Changeup 79.58 1671.50 253.03 16.26 6.45 StrikeSwinging 6.27 1.25 4.23 8.4
Curveball 78.83 2531.51 44.24 -8.29 -7.03 BallCalled 6.42 1.49 3.88 1.5
Changeup 79.08 1835.94 253.35 20.99 7.86 FoulBallNotFieldable 6.24 1.59 4.18 8.4
Curveball 76.51 2353.44 21.45 -4.82 -10.48 BallCalled 6.27 1.74 3.90 0.7
Four-Seam 88.07 2016.08 201.29 6.76 18.66 BallCalled 6.46 0.77 4.52 6.7
Changeup 77.41 1539.39 252.21 14.73 6.43 InPlay 6.30 1.37 4.11 8.4
Four-Seam 89.12 1950.78 211.28 10.94 19.26 BallCalled 6.51 0.84 4.45 7.0
Curveball 76.76 2419.51 18.85 -3.32 -8.04 FoulBallNotFieldable 6.28 1.38 4.15 0.6
Four-Seam 88.18 1873.17 192.02 4.47 22.42 BallCalled 6.55 0.74 4.51 6.4
Changeup 79.21 1632.06 259.95 15.30 4.28 BallCalled 6.26 1.19 4.53 8.7
Changeup 78.94 1674.65 252.92 15.14 6.23 BallCalled 6.36 1.25 4.26 8.4
Changeup 77.15 1549.49 256.23 17.31 5.92 StrikeSwinging 6.18 1.47 4.32 8.5
Four-Seam 88.28 2006.34 214.65 12.47 19.38 InPlay 6.44 0.99 4.39 7.2
Changeup 86.06 1845.66 218.83 12.28 16.60 StrikeCalled 6.45 0.94 4.41 7.3
Changeup 77.01 1688.32 250.78 18.02 7.88 InPlay 6.24 1.56 4.19 8.4
Curveball 78.16 2458.55 28.02 -2.95 -4.08 BallCalled 6.41 1.11 3.99 0.9
Changeup 86.65 1858.34 209.98 10.07 18.67 InPlay 6.47 0.77 4.43 7.0
Four-Seam 88.16 1968.94 204.30 6.55 15.84 FoulBallNotFieldable 6.44 0.92 4.44 6.8
Changeup 79.20 1657.89 214.85 7.92 13.11 BallCalled 6.29 1.33 4.27 7.2
Changeup 76.93 1383.88 240.11 12.72 9.09 StrikeCalled 6.21 1.41 4.31 8.0
Changeup 78.37 1599.42 223.55 9.14 11.42 StrikeSwinging 6.22 1.52 4.05 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 %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # 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:  62
# 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 59.68% 37 12 25 32.43% 67.57% 78.98 1659.94 9.26 15.32 243.58 8.1 6.30 1.43 4.23
Curveball 17.74% 11 7 4 63.64% 36.36% 76.92 2389.95 -6.55 -5.23 31.66 1.1 6.37 1.42 3.94
Four-Seam 22.58% 14 5 9 35.71% 64.29% 88.27 1984.20 20.46 9.17 205.52 6.8 6.49 0.89 4.41
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-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:  89.53 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", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | 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.85, xmax = 0.85, 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")
  )