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 Moss
Moss_data <- Bristol621 %>%
  filter(Pitcher == "Moss, Max")

# Create a detailed table for each pitch
detailed_pitch_table <- Moss_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 Moss")
Detailed Pitch Table for Moss
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 85.88 1899.71 256.45 21.62 6.41 BallCalled 4.82 3.12 5.70 8.5
Changeup 86.28 1968.15 248.54 21.35 9.62 StrikeCalled 4.74 3.11 5.74 8.3
Curveball 76.38 2189.59 100.04 -10.23 3.20 InPlay 4.52 3.37 5.49 3.3
Changeup 86.24 2059.88 239.16 17.15 11.44 StrikeCalled 4.72 3.22 5.78 8.0
Sinker 86.18 2048.96 240.33 19.58 12.36 BallCalled 4.83 3.41 5.60 8.0
Changeup 87.78 2036.79 243.06 20.09 11.40 BallCalled 4.89 3.29 5.73 8.1
Changeup 87.47 1888.43 238.50 16.82 11.47 StrikeCalled 4.66 3.25 5.77 8.0
Changeup 78.02 1799.67 264.05 19.41 3.40 FoulBallNotFieldable 4.44 3.47 5.77 8.8
Changeup 86.72 1926.53 232.27 14.87 12.81 FoulBallNotFieldable 4.78 3.29 5.77 7.7
Changeup 78.14 1852.26 259.38 17.79 4.68 StrikeSwinging 4.41 3.36 5.78 8.6
Slider 78.13 2210.81 92.38 -7.63 1.76 StrikeSwinging 4.56 3.20 5.65 3.1
Slider 78.66 2283.73 63.21 -6.54 -2.05 StrikeCalled 4.52 3.28 5.53 2.1
Slider 77.52 2221.41 118.14 -6.71 4.89 InPlay 4.52 3.19 5.62 3.9
Curveball 75.21 2118.88 62.44 -7.99 -2.58 StrikeCalled 4.55 3.24 5.64 2.1
Curveball 75.37 2178.28 58.57 -8.57 -3.68 InPlay 4.61 3.30 5.62 2.0
Curveball 75.91 2181.47 73.31 -8.45 -1.04 BallCalled 4.69 3.02 5.60 2.4
Changeup 86.02 1923.12 245.91 19.15 9.87 BallCalled 4.77 3.12 5.60 8.2
Curveball 76.67 2182.53 70.23 -8.41 -1.52 StrikeCalled 4.56 3.24 5.36 2.3
Changeup 86.62 1969.26 233.44 16.82 13.74 FoulBallNotFieldable 4.70 3.03 5.77 7.8
Changeup 86.18 1877.16 223.18 15.33 17.72 HitByPitch 4.70 3.20 5.82 7.4
Changeup 86.63 1942.38 248.59 17.28 8.01 BallCalled 4.79 3.28 5.75 8.3
Changeup 86.70 1961.67 233.63 15.60 12.80 BallCalled 4.74 3.21 5.70 7.8
Changeup 86.97 1928.66 235.79 14.85 11.40 StrikeCalled 4.83 2.85 5.69 7.9
Changeup 78.28 1814.69 256.01 16.18 5.49 FoulBallNotFieldable 4.54 2.81 5.91 8.5
Changeup 86.69 1996.14 236.16 18.03 13.37 BallCalled 4.79 2.74 5.58 7.9
Changeup 87.33 1972.06 252.85 19.31 7.13 BallCalled 4.77 2.77 5.76 8.4
Changeup 87.03 1937.87 233.97 17.42 13.95 InPlay 4.83 2.93 5.62 7.8
Changeup 86.56 1940.33 245.11 18.16 9.62 BallCalled 4.84 2.96 5.78 8.2
Changeup 87.19 1878.95 241.33 19.34 11.82 StrikeCalled 4.73 2.74 5.74 8.0
Curveball 78.36 2065.32 59.70 -8.04 -3.26 InPlay 4.62 3.17 5.68 2.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:  30
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Moss")
Summary Pitch Table for Moss
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 66.67% 20 8 12 40.00% 60.00% 85.44 1928.69 10.31 17.83 243.37 8.1 4.72 3.09 5.74
Curveball 20.00% 6 1 5 16.67% 83.33% 76.32 2152.68 -1.48 -8.62 70.72 2.4 4.59 3.22 5.56
Sinker 3.33% 1 1 0 100.00% 0.00% 86.18 2048.96 12.36 19.58 240.33 8.0 4.83 3.41 5.60
Slider 10.00% 3 0 3 0.00% 100.00% 78.10 2238.65 1.53 -6.96 91.24 3.0 4.53 3.22 5.60
# 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("Moss maximum FB velocity: ", max_fb_velocity, "mph\n")
## Moss maximum FB velocity:  86.18 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Moss_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 Moss",
       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 Moss",
       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")
  )