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
Bristol621 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Bristol 621/Bristol 621.xlsx")
# Filter the data for the pitcher Interdonato
Interdonato_data <- Bristol621 %>%
  filter(Pitcher == "Interdonato, Reed")

# Create a detailed table for each pitch
detailed_pitch_table <- 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 Interdonato")
Detailed Pitch Table for Interdonato
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 90.64 2433.83 237.65 20.63 14.16 BallCalled 5.24 2.37 6.17 7.9
Sinker 90.55 2317.96 246.99 20.72 9.83 InPlay 5.19 2.46 6.24 8.2
Sinker 90.97 2478.78 240.45 21.41 13.22 StrikeCalled 5.42 2.30 6.30 8.0
Slider 85.01 2365.85 206.95 1.12 3.34 BallCalled 4.91 2.71 5.82 6.9
Slider 85.86 2372.53 180.89 0.06 5.34 BallCalled 5.04 2.80 5.85 6.0
Sinker 91.29 2426.52 239.37 20.69 13.46 StrikeCalled 5.25 2.45 6.47 8.0
Sinker 92.61 2532.64 245.70 21.56 10.96 FoulBallNotFieldable 5.27 2.48 6.21 8.2
Slider 85.17 2306.72 252.36 1.18 1.58 BallCalled 5.16 2.65 5.62 8.4
Sinker 91.55 2287.16 248.83 20.28 8.84 InPlay 5.35 2.24 6.29 8.3
Sinker 92.59 2420.76 242.36 19.72 11.36 BallCalled 5.40 2.33 6.14 8.1
Four-Seam 91.59 2288.73 227.13 16.64 16.44 BallCalled 5.37 2.24 6.55 7.6
Sinker 91.86 2407.69 232.06 18.13 15.13 BallCalled 5.30 2.39 6.32 7.7
Sinker 92.28 2491.34 230.48 17.82 15.70 BallCalled 5.38 2.38 6.16 7.7
Sinker 91.32 2269.54 242.54 18.31 10.56 StrikeCalled 5.56 1.99 6.55 8.1
Slider 85.07 2249.02 267.26 2.38 1.37 BallCalled 5.25 2.33 5.93 8.9
Sinker 90.83 2490.61 237.00 22.42 15.70 StrikeSwinging 5.52 2.09 6.40 7.9
Sinker 91.95 2347.36 234.36 19.59 15.12 FoulBallNotFieldable 5.50 2.00 6.32 7.8
Sinker 92.92 2435.55 241.89 21.44 12.55 InPlay 5.66 1.97 6.25 8.1
Slider 85.06 2355.24 242.45 4.09 3.43 BallCalled 5.28 2.35 6.01 8.1
Sinker 91.91 2358.09 231.79 17.12 14.59 FoulBallNotFieldable 5.55 2.04 6.60 7.7
Slider 86.64 2339.06 216.76 2.00 3.92 StrikeSwinging 5.33 2.37 5.80 7.2
Slider 87.15 2286.35 196.86 0.61 3.26 StrikeCalled 5.37 2.18 6.07 6.6
Slider 86.10 2337.33 216.69 1.49 3.33 InPlay 5.34 2.35 5.89 7.2
Slider 84.87 2266.01 154.41 -1.42 4.04 BallCalled 5.27 2.43 5.66 5.1
Sinker 91.48 2421.16 232.73 19.40 15.90 FoulBallNotFieldable 5.54 1.99 6.19 7.8
Sinker 92.67 2448.84 224.62 17.78 19.23 BallCalled 5.67 1.86 6.17 7.5
Slider 85.13 2240.55 315.45 3.58 -2.35 StrikeSwinging 5.27 2.29 5.81 10.5
Slider 86.10 2343.73 274.54 4.30 0.81 StrikeSwinging 5.25 2.11 5.83 9.2
Sinker 90.97 2325.91 227.71 17.27 16.75 BallCalled 5.57 2.09 6.81 7.6
Sinker 90.82 2432.86 242.31 21.17 12.23 InPlay 5.44 2.03 6.22 8.1
Slider 86.73 2370.87 268.77 5.89 1.22 FoulBallNotFieldable 5.28 2.41 5.83 9.0
Sinker 92.64 2422.58 230.50 17.60 15.57 BallCalled 5.54 1.86 6.43 7.7
Slider 85.14 2333.59 231.96 3.33 3.53 InPlay 5.20 2.42 5.70 7.7
Sinker 89.83 2264.30 230.88 18.40 16.09 BallCalled 5.39 2.29 6.03 7.7
Sinker 89.93 2401.85 237.38 20.75 14.42 BallCalled 5.38 2.32 5.80 7.9
Sinker 90.11 2382.33 239.50 19.46 12.62 BallCalled 5.35 2.17 5.87 8.0
Sinker 91.24 2456.25 236.47 19.73 14.15 StrikeCalled 5.18 2.06 6.24 7.9
Sinker 91.46 2407.35 239.58 19.62 12.61 StrikeSwinging 5.34 2.11 6.43 8.0
Sinker 91.25 2272.23 237.53 18.66 12.90 FoulBallFieldable 5.32 2.17 6.56 7.9
Changeup 86.80 2273.61 304.14 6.08 -2.91 InPlay 5.22 2.39 6.06 10.1
Sinker 91.02 2361.26 232.35 17.13 14.31 StrikeCalled 5.36 2.21 6.44 7.7
Changeup 84.77 2025.70 238.34 16.73 11.51 BallCalled 5.08 2.42 6.18 7.9
Sinker 91.39 2372.36 240.16 22.32 13.93 StrikeSwinging 5.30 2.30 6.34 8.0
Sinker 92.47 2297.49 237.33 19.80 13.75 StrikeSwinging 5.47 2.02 6.28 7.9
Four-Seam 91.81 2344.60 234.32 20.65 15.93 BallCalled 5.46 2.27 6.28 7.8
Sinker 91.27 2358.03 240.35 21.38 13.27 InPlay 5.38 2.19 6.44 8.0
# 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:  46
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Interdonato")
Summary Pitch Table for Interdonato
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 4.35% 2 1 1 50.00% 50.00% 85.78 2149.66 4.30 11.41 271.24 9.0 5.15 2.41 6.12
Four-Seam 4.35% 2 2 0 100.00% 0.00% 91.70 2316.66 16.19 18.64 230.72 7.7 5.42 2.26 6.42
Sinker 63.04% 29 10 19 34.48% 65.52% 91.44 2390.44 13.76 19.67 237.27 7.9 5.41 2.18 6.30
Slider 28.26% 13 6 7 46.15% 53.85% 85.69 2320.53 2.52 2.20 232.72 7.8 5.23 2.42 5.83
# 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("Interdonato maximum FB velocity: ", max_fb_velocity, "mph\n")
## Interdonato maximum FB velocity:  92.92 mph
# Prepare data for plotting pitch locations
pitch_location_data <- 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) + # Increase point size
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + # Red box
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + # Black box
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) + # Gray box
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) + # Adjust ratio to shrink vertical distance
  labs(title = "Pitch Locations for Interdonato",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each 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")
  )

# 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 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")
  )