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
Sanford625 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Sanford 625/Sanford 625.xlsx")
# Filter the data for the pitcher Brandon Adams
Brandon_Adams_data <- Sanford625 %>%
  filter(Pitcher == "Adams, Brandon")

# Create a detailed table for each pitch
detailed_pitch_table <- Brandon_Adams_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 Brandon Adams")
Detailed Pitch Table for Brandon Adams
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Curveball 75.02 2194.05 74.09 -11.51 -1.85 BallCalled 5.58 2.40 5.98 2.5
Four-Seam 89.45 2143.08 212.75 11.65 19.14 StrikeSwinging 5.77 2.19 6.64 7.1
Curveball 77.75 2429.73 50.60 -6.90 -4.32 StrikeCalled 5.67 2.26 5.94 1.7
Curveball 78.20 2319.95 59.80 -9.86 -4.44 BallCalled 5.70 2.19 5.98 2.0
Four-Seam 90.34 2174.27 210.19 10.30 18.78 StrikeSwinging 5.82 2.07 6.35 7.0
Four-Seam 89.49 2155.46 220.35 14.12 17.67 StrikeSwinging 5.80 2.28 6.54 7.3
Curveball 77.50 2359.60 46.67 -7.33 -5.56 StrikeCalled 5.63 2.35 5.73 1.6
Curveball 78.43 2495.54 67.30 -13.48 -4.30 BallCalled 5.63 2.15 5.96 2.2
Four-Seam 90.07 2141.27 205.97 8.79 19.10 InPlay 5.80 2.08 6.32 6.9
Four-Seam 88.57 2186.01 211.87 11.33 19.51 StrikeCalled 5.74 2.26 6.77 7.1
Changeup 80.25 2005.03 255.93 20.25 6.52 StrikeSwinging 5.45 2.74 6.16 8.5
Changeup 81.66 2087.87 235.99 14.30 11.04 InPlay 5.47 2.76 6.15 7.9
Four-Seam 89.99 2196.45 211.10 11.68 20.59 StrikeCalled 5.77 2.13 6.50 7.0
Curveball 78.38 2367.91 59.18 -8.35 -3.49 BallCalled 5.55 2.11 6.03 2.0
Curveball 77.50 2323.17 46.52 -8.61 -6.59 FoulBallNotFieldable 5.54 2.26 5.91 1.6
Curveball 77.64 2363.32 45.22 -9.39 -7.87 InPlay 5.58 2.28 5.76 1.5
Slider 77.72 2286.10 96.85 -10.92 2.63 StrikeCalled 5.48 2.31 5.92 3.2
Curveball 77.72 2345.80 49.51 -12.43 -9.10 StrikeCalled 5.54 2.29 5.97 1.7
Sinker 89.46 2305.01 230.80 18.01 15.91 InPlay 5.61 2.29 6.34 7.7
Curveball 75.74 2347.33 27.55 -6.24 -10.32 BallCalled 5.63 2.35 5.66 0.9
Four-Seam 88.84 2269.98 202.05 7.89 20.66 FoulBallNotFieldable 5.77 1.99 6.70 6.7
Curveball 77.12 2288.83 51.66 -8.72 -5.68 BallCalled 5.59 2.29 5.80 1.7
Slider 75.68 2347.11 86.44 -14.58 0.39 StrikeCalled 5.56 2.26 5.94 2.9
Changeup 80.64 1962.47 251.84 19.83 7.82 FoulBallNotFieldable 5.35 2.63 6.35 8.4
Four-Seam 90.26 2310.03 212.20 12.04 20.37 StrikeSwinging 5.78 2.12 6.27 7.1
Curveball 77.64 2420.77 67.54 -12.48 -3.57 BallCalled 5.59 2.27 5.98 2.3
Curveball 75.57 2249.28 66.02 -10.45 -3.16 BallCalled 5.50 2.47 5.90 2.2
Four-Seam 89.36 2210.37 219.98 13.45 17.26 StrikeCalled 5.53 2.10 6.99 7.3
Four-Seam 89.64 2284.99 212.14 11.75 19.88 BallCalled 5.70 1.86 6.88 7.1
Sinker 88.61 2298.43 235.19 16.44 12.66 BallCalled 5.48 2.13 6.81 7.8
Sinker 88.66 2153.56 229.51 16.88 15.61 StrikeCalled 5.57 2.28 6.82 7.7
Changeup 81.44 2074.79 245.46 18.52 9.86 BallinDirt 5.39 2.70 6.35 8.2
Sinker 88.03 2139.90 225.23 14.73 15.85 StrikeCalled 5.53 2.12 6.72 7.5
Changeup 82.09 2006.01 262.89 20.52 3.87 FoulBallNotFieldable 5.45 2.60 6.53 8.8
Changeup 81.28 2081.00 247.79 17.97 8.88 BallinDirt 5.33 2.62 6.25 8.3
Sinker 88.22 2063.64 220.96 12.52 15.53 InPlay 5.54 2.20 6.85 7.4
Four-Seam 87.93 2124.62 206.12 9.34 20.18 BallCalled 5.70 2.04 7.00 6.9
Curveball 77.30 2350.61 56.70 -6.40 -2.71 BallCalled 5.41 2.27 6.28 1.9
Sinker 88.18 2179.18 244.21 19.50 10.68 FoulBallNotFieldable 5.53 2.22 6.80 8.1
Four-Seam 88.44 2115.09 201.46 6.63 18.12 InPlay 5.56 2.06 6.79 6.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:  40
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Brandon Adams")
Summary Pitch Table for Brandon Adams
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 15.00% 6 0 6 0.00% 100.00% 81.23 2036.19 8.00 18.56 249.98 8.3 5.41 2.67 6.30
Curveball 35.00% 14 9 5 64.29% 35.71% 77.25 2346.85 -5.21 -9.44 54.88 1.8 5.58 2.28 5.92
Four-Seam 30.00% 12 2 10 16.67% 83.33% 89.36 2192.64 19.27 10.75 210.51 7.0 5.73 2.10 6.65
Sinker 15.00% 6 1 5 16.67% 83.33% 88.53 2189.95 14.37 16.35 230.98 7.7 5.54 2.21 6.72
Slider 5.00% 2 0 2 0.00% 100.00% 76.70 2316.60 1.51 -12.75 91.64 3.0 5.52 2.29 5.93
# 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("Brandon Adams maximum FB velocity: ", max_fb_velocity, "mph\n")
## Brandon Adams maximum FB velocity:  90.34 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brandon_Adams_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 Brandon Adams",
       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 Brandon Adams",
       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")
  )