knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(knitr)

# Load the data
Valley628 <- read_excel("C:\\Users\\Franco Castagliuolo\\OneDrive - Bentley University\\Neers 24\\Pitchers\\Valley 628\\Valley 628.xlsx")
# Filter the data for the pitcher Matthew Martinez
Matthew_Martinez_data <- Valley628 %>%
  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 77.00 1688.10 252.63 16.92 6.92 BallCalled 6.24 1.46 4.78 8.4
Changeup 78.38 1751.17 237.46 14.85 11.10 BallCalled 6.16 1.34 4.66 7.9
Four-Seam 87.82 2041.73 206.09 10.29 22.44 BallCalled 6.47 0.78 4.69 6.9
Sinker 87.69 2035.37 222.04 16.79 20.06 StrikeCalled 6.44 0.81 4.94 7.4
Changeup 78.32 1727.69 236.43 16.08 12.31 StrikeCalled 6.26 1.35 4.64 7.9
Changeup 78.12 1587.19 251.64 20.07 8.39 InPlay 6.24 1.28 4.80 8.4
Changeup 78.36 NA 248.41 18.78 9.08 BallCalled 6.25 1.37 4.74 8.3
Curveball 76.15 2466.77 42.60 -10.51 -9.73 StrikeCalled 6.29 1.29 4.35 1.4
Curveball 76.80 2481.32 41.28 -6.56 -5.62 BallCalled 6.36 1.42 4.30 1.4
Changeup 78.63 1798.75 247.64 19.07 9.63 StrikeSwinging 6.24 1.51 4.63 8.3
Curveball 75.86 2370.84 27.67 -6.01 -9.62 InPlay 6.21 1.46 4.39 0.9
Changeup 77.32 1640.62 246.64 20.74 10.69 StrikeSwinging 6.19 1.41 4.68 8.2
Curveball 74.48 2406.15 27.33 -7.62 -12.82 BallCalled 6.11 1.43 4.46 0.9
Four-Seam 88.17 2142.68 211.06 12.72 22.85 BallCalled 6.42 0.98 4.79 7.0
Changeup 78.25 1642.32 248.39 17.99 8.87 FoulBallNotFieldable 6.22 1.32 4.94 8.3
Changeup 78.95 1589.29 238.89 15.72 11.25 StrikeSwinging 6.18 1.51 4.57 8.0
Four-Seam 88.45 1973.60 215.16 13.46 20.58 StrikeCalled 6.29 0.93 4.87 7.2
Changeup 78.10 1519.09 250.12 13.83 6.53 StrikeSwinging 6.10 1.49 5.00 8.3
Curveball 76.76 2357.51 38.44 -7.41 -7.64 BallCalled 6.26 1.24 4.44 1.3
Four-Seam 87.58 2006.15 216.88 13.00 18.67 FoulBallNotFieldable 6.42 1.07 4.71 7.2
Changeup 78.09 1540.87 267.80 18.76 2.30 StrikeCalled 6.13 1.38 4.77 8.9
Four-Seam 88.28 2040.27 214.27 11.57 18.23 BallCalled 6.37 1.18 4.76 7.1
Changeup 78.26 1545.53 267.33 14.40 2.22 BallCalled 6.11 1.44 4.82 8.9
Changeup 79.56 1550.92 258.96 13.68 4.16 StrikeSwinging 6.26 1.47 4.65 8.6
Changeup 79.29 1655.33 236.71 12.70 9.90 FoulBallNotFieldable 6.10 1.52 4.58 7.9
Four-Seam 88.60 2029.58 196.19 5.56 20.56 StrikeSwinging 6.40 0.88 4.73 6.5
Curveball 74.27 2372.32 47.73 -6.96 -4.34 BallCalled 6.26 1.72 4.22 1.6
Curveball 75.09 2558.23 52.13 -12.54 -7.91 BallCalled 6.22 1.42 4.43 1.7
Changeup 77.52 1490.11 254.00 16.29 6.41 StrikeSwinging 6.11 1.57 4.83 8.5
Curveball 76.25 2463.07 48.43 -6.51 -3.86 FoulBallNotFieldable 6.31 1.37 4.44 1.6
Four-Seam 87.78 2070.77 205.66 9.16 20.44 StrikeSwinging 6.47 0.94 4.76 6.9
Changeup 80.53 1567.11 249.02 16.06 7.82 StrikeSwinging 6.21 1.61 4.87 8.3
Changeup 80.28 1725.46 255.20 17.40 6.30 InPlay 6.13 1.62 4.70 8.5
Changeup 79.17 1586.22 254.73 16.81 6.26 BallCalled 6.14 1.45 5.11 8.5
Changeup 79.42 1463.22 263.02 17.98 3.84 StrikeSwinging 6.25 1.44 5.13 8.8
Curveball 77.62 2392.06 52.83 -14.14 -9.04 FoulBallNotFieldable 6.27 1.31 4.49 1.8
Four-Seam 88.59 2086.76 185.17 2.13 24.95 BallCalled 6.45 0.81 4.94 6.2
Changeup 80.57 1596.12 223.31 12.07 14.28 BallCalled 6.15 1.44 5.25 7.4
Changeup 79.35 1467.37 210.92 9.26 17.14 InPlay 6.18 1.39 5.10 7.0
Changeup 77.82 1554.87 261.86 18.04 4.16 BallCalled 6.15 1.45 4.50 8.7
Sinker 88.00 2008.66 234.42 17.12 13.59 InPlay 6.29 1.13 4.79 7.8
# 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:  41
# 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.66% 22 7 15 31.82% 68.18% 78.69 1604.16 8.16 16.25 248.23 8.3 6.18 1.45 4.81
Curveball 21.95% 9 5 4 55.56% 44.44% 75.92 2429.81 -7.84 -8.70 42.05 1.4 6.25 1.41 4.39
Four-Seam 19.51% 8 4 4 50.00% 50.00% 88.16 2048.94 21.09 9.74 206.31 6.9 6.41 0.95 4.78
Sinker 4.88% 2 0 2 0.00% 100.00% 87.84 2022.01 16.82 16.95 228.23 7.6 6.36 0.97 4.86
# 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.6 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
  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")
  )