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
Shore624 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Shore 624/Shore 624.xlsx")
# Filter the data for the pitcher Dennis Helwig
Dennis_Helwig_data <- Shore624 %>%
  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 91.44 2251.89 206.85 10.25 21.35 BallCalled 6.26 1.44 6.28 6.9
Four-Seam 90.63 2188.42 210.71 11.49 20.45 FoulBallNotFieldable 6.16 1.50 6.19 7.0
Four-Seam 92.12 2189.64 217.59 13.84 19.11 InPlay 6.36 1.51 6.25 7.3
Four-Seam 90.81 2203.09 220.04 14.30 18.14 StrikeCalled 6.30 1.66 5.97 7.3
Sinker 92.76 2256.07 227.93 17.45 16.83 BallCalled 6.32 1.60 6.06 7.6
Changeup 84.55 1370.65 260.13 15.59 3.82 FoulBallNotFieldable 5.93 1.94 6.02 8.7
Four-Seam 92.90 2279.70 215.74 12.53 18.55 StrikeSwinging 6.13 1.50 6.41 7.2
Slider 80.03 2320.11 85.50 -15.08 0.31 StrikeCalled 5.92 2.44 5.66 2.9
Curveball 80.15 2353.05 60.66 -7.64 -2.87 InPlay 5.87 2.09 5.64 2.0
Four-Seam 89.90 2265.24 219.05 15.71 20.49 BallCalled 6.18 2.66 6.34 7.3
Sinker 88.76 2091.55 223.13 16.09 18.44 BallCalled 6.25 2.56 6.11 7.4
Four-Seam 89.37 2205.48 201.85 8.32 21.92 InPlay 6.19 2.45 6.36 6.7
Changeup 83.51 1415.45 247.09 13.71 7.13 BallCalled 5.94 1.99 6.10 8.2
Four-Seam 91.36 2250.57 223.53 15.25 17.32 BallCalled 6.28 1.66 5.83 7.5
Changeup 84.61 1404.37 254.98 13.77 4.95 StrikeCalled 5.82 2.13 6.15 8.5
Four-Seam 89.42 2160.07 218.52 14.10 18.98 BallCalled 6.03 1.81 6.34 7.3
Sinker 89.60 2137.02 222.13 15.17 17.98 InPlay 6.01 1.90 6.44 7.4
Four-Seam 91.35 2195.64 216.59 13.77 19.79 StrikeCalled 6.20 1.47 6.03 7.2
Four-Seam 92.06 2225.61 210.89 11.71 20.74 StrikeCalled 6.23 1.34 6.19 7.0
Slider 83.05 1189.83 115.68 -7.24 4.87 BallCalled 5.72 2.20 5.77 3.9
Slider 82.53 2348.73 92.90 -12.60 2.07 BallCalled 5.96 2.18 5.87 3.1
Four-Seam 92.07 2222.34 208.44 10.46 20.49 FoulBallNotFieldable 6.37 1.16 6.26 6.9
Sinker 91.51 2239.24 226.03 14.62 15.36 StrikeSwinging 6.21 1.39 6.18 7.5
Slider 80.91 2384.91 52.47 -4.74 -2.28 BallCalled 5.98 2.24 5.80 1.7
Four-Seam 91.10 2265.78 220.89 15.63 19.10 BallCalled 6.20 1.68 5.87 7.4
Four-Seam 90.12 2214.67 217.43 15.02 20.74 StrikeSwinging 6.14 1.95 6.03 7.2
Four-Seam 90.57 2253.73 217.35 14.20 19.69 BallCalled 6.15 1.73 6.00 7.2
Four-Seam 90.33 2227.05 218.72 15.07 19.86 BallCalled 6.24 1.52 6.01 7.3
Four-Seam 88.52 2190.08 218.89 14.93 19.64 StrikeCalled 6.27 2.32 6.11 7.3
Four-Seam 89.02 2195.01 220.09 14.81 18.74 BallCalled 6.25 2.48 5.99 7.3
Sinker 87.72 2139.50 219.93 14.73 18.76 FoulBallNotFieldable 6.18 2.46 6.17 7.3
Curveball 81.49 1661.43 68.91 -7.10 -1.39 BallCalled 5.64 3.32 5.87 2.3
Four-Seam 90.06 2193.22 211.59 12.73 21.90 FoulBallNotFieldable 6.26 2.32 5.97 7.1
Four-Seam 90.95 2274.11 216.81 14.57 20.56 FoulBallNotFieldable 6.27 2.45 6.07 7.2
Four-Seam 91.54 2220.47 213.58 12.14 19.53 FoulBallNotFieldable 6.26 2.30 6.22 7.1
Four-Seam 92.16 2269.07 208.20 11.51 22.57 StrikeSwinging 6.35 2.46 6.27 6.9
Changeup 82.32 1349.74 252.49 15.24 6.07 BallCalled 5.70 2.16 6.27 8.4
Sinker 89.49 2207.86 235.59 18.06 13.46 InPlay 6.14 1.97 6.09 7.9
Changeup 82.78 1299.34 247.80 13.77 6.78 BallCalled 5.95 1.76 6.13 8.3
Four-Seam 90.09 2123.90 218.55 13.16 17.72 InPlay 6.28 1.41 5.90 7.3
Slider 78.86 2230.78 85.56 -9.66 0.66 StrikeCalled 5.99 2.45 5.59 2.9
Changeup 83.34 1292.01 259.23 13.72 3.85 BallCalled 5.70 1.89 6.11 8.6
Four-Seam 91.03 2224.32 223.74 14.40 16.23 BallCalled 6.12 1.75 6.26 7.5
Four-Seam 89.37 2166.28 215.23 10.98 16.74 BallCalled 6.16 2.14 6.25 7.2
Four-Seam 89.19 2150.46 200.41 6.24 17.94 FoulBallNotFieldable 6.15 1.89 6.52 6.7
Slider 79.27 2152.83 97.33 -8.77 2.47 InPlay 5.91 2.22 5.81 3.2
Four-Seam 90.06 2186.18 221.65 14.79 17.70 InPlay 6.20 2.62 6.34 7.4
Four-Seam 90.70 2278.63 218.93 12.76 16.86 StrikeCalled 6.11 2.03 5.98 7.3
Four-Seam 90.16 2208.25 206.17 8.83 19.08 BallCalled 6.17 1.61 5.80 6.9
Four-Seam 91.25 2301.28 216.37 12.17 17.62 InPlay 6.27 1.67 5.74 7.2
Changeup 83.95 1357.27 259.10 14.36 3.94 BallCalled 5.90 1.75 6.37 8.6
Four-Seam 90.74 2265.82 215.51 11.59 17.35 FoulBallNotFieldable 6.20 1.78 5.93 7.2
Four-Seam 91.07 2160.07 213.59 12.32 19.63 BallCalled 6.15 1.75 6.22 7.1
Changeup 85.73 1422.67 243.30 14.02 8.21 StrikeSwinging 5.96 1.96 6.17 8.1
Four-Seam 90.54 2262.29 214.56 12.76 19.63 StrikeCalled 6.33 1.58 6.01 7.2
Four-Seam 90.05 2222.37 218.85 15.40 20.25 BallCalled 6.33 1.44 6.00 7.3
Four-Seam 89.33 2218.04 216.54 12.74 18.26 FoulBallNotFieldable 6.11 1.61 6.67 7.2
Slider 82.37 2183.69 64.62 -4.82 -1.06 BallCalled 5.98 1.91 5.69 2.2
Four-Seam 89.89 2226.42 206.54 11.10 23.40 StrikeSwinging 6.25 1.44 5.95 6.9
Four-Seam 91.38 2231.53 203.70 9.45 22.63 BallCalled 6.24 1.52 5.93 6.8
Sinker 89.49 2148.37 222.96 14.50 16.68 InPlay 6.31 1.17 6.33 7.4
Four-Seam 88.31 2165.03 212.57 12.00 19.86 InPlay 6.16 2.39 6.32 7.1
# 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:  62
# 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 12.90% 8 5 3 62.50% 37.50% 83.85 1363.94 5.59 14.27 253.02 8.4 5.86 1.95 6.16
Curveball 3.23% 2 1 1 50.00% 50.00% 80.82 2007.24 -2.13 -7.37 64.78 2.1 5.76 2.70 5.76
Four-Seam 61.29% 38 14 24 36.84% 63.16% 90.55 2219.26 19.49 12.71 214.64 7.2 6.22 1.84 6.13
Sinker 11.29% 7 2 5 28.57% 71.43% 89.90 2174.23 16.79 15.80 225.39 7.5 6.20 1.86 6.20
Slider 11.29% 7 4 3 57.14% 42.86% 81.00 2115.84 1.01 -8.99 84.87 2.8 5.92 2.23 5.74
# 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:  92.9 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) + # 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 Dennis Helwig",
       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 Dennis Helwig",
       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")
  )