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 Reed Interdonato
Reed_Interdonato_data <- Valley628 %>%
  filter(Pitcher == "Interdonato, Reed")

# Create a detailed table for each pitch
detailed_pitch_table <- Reed_Interdonato_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 Reed Interdonato")
Detailed Pitch Table for Reed Interdonato
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 90.63 2379.93 236.30 21.37 15.54 StrikeCalled 5.65 2.18 6.07 7.9
Slider 86.15 2318.67 206.77 1.10 3.67 StrikeSwinging 5.33 2.39 5.62 6.9
Slider 86.48 2355.38 231.51 2.42 3.28 BallinDirt 5.38 2.21 5.99 7.7
Slider 85.19 2354.38 202.20 0.18 2.04 FoulBallNotFieldable 5.40 2.42 5.84 6.7
Sinker 90.20 2265.05 236.60 20.81 14.90 BallCalled 5.84 1.71 6.52 7.9
Slider 86.09 2429.16 222.10 2.73 4.03 InPlay 5.38 2.33 5.79 7.4
Slider 83.10 2324.71 36.91 -1.81 -1.32 StrikeSwinging 5.38 2.29 5.57 1.2
Sinker 92.33 2409.46 232.88 19.61 15.97 BallinDirt 5.66 1.92 6.27 7.8
Slider 84.49 2393.06 100.59 -1.54 1.59 BallCalled 5.39 2.50 5.61 3.4
Sinker 92.74 2432.40 242.54 20.48 11.87 BallCalled 5.54 2.10 6.20 8.1
Four-Seam 92.82 2439.84 226.55 15.42 15.79 BallCalled 5.53 2.10 6.27 7.6
Sinker 91.24 2282.49 239.89 19.78 12.73 BallCalled 5.69 1.88 6.18 8.0
Sinker 91.68 2385.04 227.56 17.32 17.12 BallCalled 5.73 1.88 5.93 7.6
Sinker 92.76 2389.05 228.97 18.08 16.92 BallCalled 5.66 1.95 6.09 7.6
Sinker 90.92 2347.30 227.49 17.04 16.82 StrikeCalled 5.71 1.85 6.06 7.6
Sinker 92.65 2403.62 234.11 19.63 15.44 BallCalled 5.77 1.85 6.11 7.8
Sinker 93.08 2490.63 234.93 19.13 14.60 StrikeCalled 5.71 1.89 6.40 7.8
Sinker 93.55 2431.94 241.24 20.76 12.59 BallCalled 5.77 1.80 6.53 8.0
Slider 85.87 2309.12 151.33 -2.32 5.57 BallinDirt 5.44 2.10 6.00 5.0
Sinker 92.03 2430.22 245.65 21.05 10.68 StrikeCalled 5.70 1.93 6.35 8.2
Sinker 92.56 2441.95 234.44 19.97 15.43 InPlay 5.88 1.85 6.43 7.8
Slider 86.57 2348.66 242.79 6.03 4.38 StrikeCalled 5.39 2.18 6.06 8.1
Four-Seam 92.89 2484.05 234.19 20.59 16.27 InPlay 5.78 1.88 6.57 7.8
Slider 84.09 2344.26 297.27 4.92 -1.20 StrikeSwinging 5.32 2.37 5.76 9.9
Sinker 91.54 2474.89 231.11 18.65 16.23 BallCalled 5.58 2.14 6.43 7.7
Slider 86.17 2387.52 151.39 -0.48 2.41 BallCalled 5.38 2.20 5.74 5.0
Four-Seam 92.30 2472.70 232.10 19.77 16.62 StrikeSwinging 5.64 2.14 6.15 7.7
Sinker 93.05 2412.71 234.91 19.22 14.64 BallCalled 5.74 1.84 6.22 7.8
Slider 86.96 2284.05 293.73 3.92 -0.50 InPlay 5.48 2.33 5.96 9.8
Sinker 92.19 2498.99 239.67 21.05 13.49 BallCalled 5.70 2.11 6.34 8.0
Slider 85.75 2354.49 284.15 1.39 0.94 BallCalled 5.45 2.24 5.66 9.5
Sinker 92.48 2434.93 231.59 18.44 15.77 BallCalled 5.69 1.92 6.37 7.7
Sinker 91.96 2502.44 234.03 18.03 14.22 BallCalled 5.78 2.05 6.29 7.8
Slider 84.97 2276.71 214.16 1.89 3.91 BallCalled 5.52 1.99 5.74 7.1
Four-Seam 92.57 2496.41 228.00 19.21 18.38 StrikeCalled 5.74 1.76 6.17 7.6
Sinker 92.18 2514.32 228.57 19.20 18.09 BallCalled 5.74 1.76 6.33 7.6
Sinker 92.10 2415.45 233.68 19.18 15.24 BallCalled 5.63 1.84 6.45 7.8
Sinker 92.43 2412.88 235.36 19.73 14.78 StrikeSwinging 5.77 1.86 6.33 7.8
Sinker 92.36 2431.19 232.54 19.60 16.18 BallCalled 5.75 1.67 6.48 7.8
Four-Seam 92.41 2457.50 231.56 19.57 16.72 BallCalled 5.60 2.23 6.32 7.7
Sinker 90.96 2437.14 234.40 19.53 15.17 FoulBallNotFieldable 5.50 2.10 6.08 7.8
Sinker 92.93 2384.25 239.56 19.98 13.07 StrikeCalled 5.54 2.04 6.16 8.0
Sinker 93.88 2465.85 231.09 18.79 16.37 BallCalled 5.63 2.10 6.06 7.7
Sinker 93.15 2389.73 243.68 21.87 12.14 FoulBallNotFieldable 5.59 2.06 6.08 8.1
Changeup 86.84 1747.88 259.99 18.01 4.40 BallCalled 5.23 2.21 6.37 8.7
Sinker 94.53 2479.51 234.93 21.69 16.35 StrikeSwinging 5.63 2.04 6.25 7.8
Sinker 92.08 2271.93 233.92 19.12 15.05 BallCalled 5.87 1.60 6.27 7.8
Sinker 92.73 2413.57 235.95 20.21 14.77 InPlay 5.68 2.02 6.41 7.9
# 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:  48
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Reed Interdonato")
Summary Pitch Table for Reed Interdonato
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 2.08% 1 1 0 100.00% 0.00% 86.84 1747.88 4.40 18.01 259.99 8.7 5.23 2.21 6.37
Four-Seam 10.42% 5 2 3 40.00% 60.00% 92.60 2470.10 16.76 18.91 230.48 7.7 5.66 2.02 6.30
Sinker 60.42% 29 17 12 58.62% 41.38% 92.31 2414.79 14.90 19.63 235.09 7.8 5.69 1.93 6.27
Slider 27.08% 13 4 9 30.77% 69.23% 85.53 2344.63 2.22 1.42 202.68 6.7 5.40 2.27 5.80
# 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("Reed Interdonato maximum FB velocity: ", max_fb_velocity, "mph\n")
## Reed Interdonato maximum FB velocity:  94.53 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Reed_Interdonato_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 Reed Interdonato",
       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 Reed Interdonato",
       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")
  )