# Load necessary libraries
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
file_path <- "C:\\Users\\Franco Castagliuolo\\OneDrive - Bentley University\\Shore 77.xlsx"
data <- readxl::read_excel(file_path)

# Filter the data for the pitcher Dennis Helwig
Dennis_Helwig_data <- data %>%
  filter(Pitcher == "Helwig, Dennis")

# Create a detailed table for each pitch
detailed_pitch_table <- Dennis_Helwig_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 Dennis Helwig")
Detailed Pitch Table for Dennis Helwig
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 93.79 2369.39 225.94 17.24 17.80 InPlay 6.05 1.17 6.46 7.5
Four-Seam 91.77 2304.56 220.73 14.20 17.73 InPlay 6.16 2.08 6.53 7.4
Sinker 91.38 2390.89 228.62 16.84 16.07 InPlay 6.08 2.38 5.91 7.6
Changeup 84.56 1228.63 289.61 12.11 -3.00 InPlay 5.65 2.64 6.40 9.7
Changeup 84.02 1285.38 277.23 17.85 -0.93 InPlay 5.54 2.67 6.44 9.2
Four-Seam 91.05 2286.12 217.15 14.76 20.70 InPlay 6.03 2.17 6.22 7.2
Splitter 83.95 1182.92 263.84 11.42 2.37 StrikeCalled 5.65 2.73 6.26 8.8
Four-Seam 91.20 2260.80 219.70 14.74 18.81 BallCalled 5.99 2.35 6.06 7.3
Four-Seam 91.35 2206.88 221.62 14.23 16.99 BallCalled 6.02 2.17 6.58 7.4
Sinker 89.45 2130.65 223.59 15.23 17.02 BallCalled 6.11 2.17 6.50 7.5
Sinker 89.29 2157.89 225.49 16.29 17.08 BallCalled 5.95 2.29 6.62 7.5
Sinker 89.03 2168.86 225.50 16.46 17.23 StrikeCalled 6.07 2.19 6.65 7.5
Sinker 88.40 2173.65 226.82 17.87 17.85 BallCalled 6.04 2.21 6.72 7.6
Sinker 91.37 2138.85 223.26 15.05 17.00 StrikeCalled 5.90 2.41 6.47 7.4
Changeup 84.28 1368.92 260.25 15.63 3.75 FoulBallNotFieldable 5.62 2.45 6.53 8.7
Splitter 85.09 1253.49 259.89 17.35 4.31 BallCalled 5.58 2.45 6.45 8.7
Sinker 91.14 2227.80 225.90 17.29 17.87 BallCalled 5.98 2.35 6.55 7.5
Changeup 83.80 1201.70 259.26 12.76 3.66 BallCalled 5.58 2.67 6.52 8.6
Sinker 89.55 2132.28 227.14 17.10 16.96 StrikeCalled 5.95 2.24 6.80 7.6
Changeup 83.84 1275.10 270.31 17.57 1.12 InPlay 5.65 1.68 6.40 9.0
Sinker 91.94 2277.08 228.45 18.06 17.04 FoulBallNotFieldable 6.04 1.48 6.52 7.6
Slider 82.29 2223.88 100.39 -12.27 3.61 StrikeCalled 5.60 2.26 6.01 3.3
Changeup 83.74 1447.65 221.72 10.12 12.50 StrikeSwinging 5.59 1.62 6.37 7.4
Slider 80.30 2269.76 88.23 -12.57 0.86 BallCalled 5.67 2.16 5.75 2.9
Changeup 84.88 1233.01 255.55 12.58 4.30 InPlay 5.70 1.39 6.35 8.5
Four-Seam 90.83 2172.04 218.53 14.37 19.07 StrikeCalled 6.07 1.31 6.12 7.3
Changeup 83.86 1189.43 239.99 7.38 5.52 StrikeCalled 5.70 1.26 6.23 8.0
Slider 83.41 2201.01 83.84 -4.57 0.68 BallCalled 5.38 1.99 6.10 2.8
Slider 83.10 2196.36 84.09 -8.50 0.34 StrikeSwinging 5.56 2.29 5.94 2.8
Four-Seam 90.77 2215.91 218.01 14.34 19.42 FoulBallNotFieldable 5.94 1.68 5.95 7.3
Slider 82.26 2068.90 89.81 -5.70 1.36 BallCalled 5.52 2.68 6.12 3.0
Four-Seam 92.92 2190.34 218.36 13.34 17.82 FoulBallNotFieldable 5.98 1.39 6.33 7.3
Four-Seam 92.76 2314.31 221.95 16.19 19.01 FoulBallNotFieldable 6.03 1.44 6.42 7.4
Splitter 86.51 1327.27 267.39 14.02 1.71 FoulBallNotFieldable 5.53 1.71 6.24 8.9
Slider 82.58 2357.52 110.60 -8.13 4.63 FoulBallNotFieldable 5.51 2.54 6.04 3.7
Changeup 84.19 1308.39 258.40 10.50 3.31 InPlay 5.38 1.82 6.37 8.6
Slider 80.65 2245.53 109.31 -6.29 3.34 FoulBallNotFieldable 5.57 3.12 5.99 3.6
Four-Seam 91.40 2255.83 221.78 14.80 17.76 StrikeCalled 6.08 2.10 5.98 7.4
Changeup 84.38 1230.81 278.36 18.03 -1.35 StrikeSwinging 5.57 2.56 6.13 9.3
Four-Seam 91.19 2212.38 214.94 13.60 20.73 StrikeCalled 5.95 2.33 5.97 7.2
Changeup 83.51 1259.56 274.75 16.87 -0.07 BallinDirt 5.35 2.81 6.28 9.2
Four-Seam 91.96 2268.28 223.51 16.04 18.05 FoulBallNotFieldable 5.92 2.39 6.29 7.5
Changeup 85.74 1209.79 254.02 14.31 5.42 StrikeSwinging 5.52 2.84 6.34 8.5
Four-Seam 89.97 2215.66 218.80 15.70 20.82 InPlay 6.00 1.34 6.37 7.3
Sinker 90.19 2173.25 226.30 18.08 18.55 FoulBallNotFieldable 6.06 2.26 6.29 7.5
Changeup 84.70 1310.51 268.34 11.82 1.52 FoulBallNotFieldable 5.59 2.77 6.26 8.9
Changeup 84.32 1217.85 257.92 15.68 4.61 InPlay 5.47 2.67 6.16 8.6
Slider 82.33 2138.23 101.53 -1.01 1.71 BallCalled 5.40 3.30 6.24 3.4
Four-Seam 91.07 2282.95 221.12 16.39 19.98 BallCalled 6.03 2.32 6.32 7.4
Sinker 90.44 2123.93 224.16 16.96 18.76 BallCalled 5.92 2.62 6.48 7.5
Sinker 88.81 2161.82 227.66 18.33 17.97 StrikeCalled 5.92 2.35 6.35 7.6
Sinker 88.53 2095.31 217.97 15.25 20.79 InPlay 5.74 2.31 6.66 7.3
# 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:  52
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Dennis Helwig")
Summary Pitch Table for Dennis Helwig
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 26.92% 14 1 13 7.14% 92.86% 84.27 1269.05 2.88 13.80 261.84 8.7 5.57 2.28 6.34
Four-Seam 26.92% 14 3 11 21.43% 78.57% 91.57 2253.96 18.91 15.00 220.15 7.3 6.02 1.87 6.26
Sinker 25.00% 13 5 8 38.46% 61.54% 89.96 2180.94 17.71 16.83 225.45 7.5 5.98 2.25 6.50
Slider 15.38% 8 4 4 50.00% 50.00% 82.11 2212.65 2.07 -7.38 95.97 3.2 5.53 2.54 6.02
Splitter 5.77% 3 1 2 33.33% 66.67% 85.18 1254.56 2.80 14.26 263.71 8.8 5.59 2.30 6.32
# 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("Dennis Helwig maximum FB velocity: ", max_fb_velocity, "mph\n")
## Dennis Helwig maximum FB velocity:  93.79 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Dennis_Helwig_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 Dennis Helwig",
       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")
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_point()`).

# 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
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement for", unique(detailed_pitch_table$AutoPitchType)),
       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")
  )