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
data <- read_excel("C:\\Users\\Franco Castagliuolo\\OneDrive - Bentley University\\Valley 75.xlsx")
# Filter the data for the pitcher Ben Alekson
Ben_Alekson_data <- data %>%
  filter(Pitcher == "Alekson, Ben")

# Create a detailed table for each pitch
detailed_pitch_table <- Ben_Alekson_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 Ben Alekson")
Detailed Pitch Table for Ben Alekson
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 90.68 2093.96 219.31 14.65 19.14 StrikeCalled 5.87 0.95 4.84 7.3
Sinker 88.72 2037.76 219.62 14.94 19.29 FoulBallNotFieldable 5.92 0.93 5.19 7.3
Changeup 81.57 1549.61 261.75 15.37 3.67 InPlay 5.81 1.06 4.79 8.7
Sinker 90.54 2094.41 228.25 14.75 14.40 BallCalled 5.85 0.92 4.88 7.6
Four-Seam 89.50 2105.23 216.80 12.95 18.55 BallCalled 6.02 0.92 4.97 7.2
Four-Seam 89.79 2142.42 220.48 14.37 18.09 StrikeCalled 5.97 0.91 4.94 7.3
Changeup 81.39 1516.72 261.31 17.09 4.07 StrikeCalled 5.91 0.92 4.88 8.7
Four-Seam 91.64 2209.91 224.04 15.99 17.73 BallCalled 5.96 0.99 5.21 7.5
Four-Seam 90.67 2186.06 223.27 14.79 17.01 StrikeSwinging 6.01 0.91 4.89 7.4
Changeup 82.10 1614.69 261.32 16.31 3.84 StrikeCalled 5.90 0.97 5.02 8.7
Changeup 82.25 1700.96 262.50 17.72 3.67 FoulBallNotFieldable 5.86 1.04 4.95 8.8
Changeup 80.89 1478.07 259.60 16.58 4.46 BallCalled 5.92 0.95 4.90 8.7
Sinker 90.92 2134.03 227.96 14.58 14.36 FoulBallNotFieldable 5.95 0.94 5.08 7.6
Changeup 83.09 1662.53 258.82 16.57 4.64 InPlay 5.93 0.89 4.83 8.6
Changeup 81.62 1559.16 277.83 20.35 -1.34 StrikeSwinging 5.73 1.12 4.94 9.3
Changeup 80.81 1515.27 277.32 17.13 -0.70 StrikeCalled 5.77 1.18 4.85 9.2
Changeup 81.60 1536.12 257.79 14.99 4.70 FoulBallNotFieldable 5.92 0.99 4.85 8.6
Changeup 82.15 1596.04 267.93 13.87 1.89 FoulBallNotFieldable 5.65 1.17 4.85 8.9
Sinker 90.37 2130.50 230.56 16.49 14.90 BallCalled 5.82 1.10 4.90 7.7
Changeup 82.10 1544.50 254.83 17.17 6.06 InPlay 5.82 1.20 4.85 8.5
Sinker 89.41 2153.81 221.79 14.51 17.59 BallCalled 5.87 0.82 5.05 7.4
Changeup 81.43 1515.91 256.56 15.39 5.08 BallCalled 5.88 0.95 4.75 8.6
Four-Seam 89.45 2111.41 210.59 11.08 20.05 StrikeCalled 5.86 0.84 5.03 7.0
Four-Seam 88.94 2111.77 206.11 9.77 21.22 BallCalled 5.99 0.81 5.17 6.9
Sinker 88.98 2153.38 220.51 16.32 20.45 StrikeCalled 5.94 0.73 5.32 7.4
Four-Seam 89.97 2186.28 208.71 10.97 21.36 StrikeSwinging 5.97 0.80 5.07 7.0
Four-Seam 89.24 2013.44 216.29 12.71 18.54 StrikeCalled 5.87 0.98 4.95 7.2
Slider 83.33 2028.63 207.95 4.22 9.23 BallCalled 5.95 1.03 5.09 6.9
Four-Seam 89.06 2135.67 207.27 9.86 20.36 BallCalled 5.97 0.96 5.20 6.9
Four-Seam 88.32 2104.25 211.06 11.90 21.02 InPlay 6.02 0.87 5.12 7.0
Changeup 80.77 1656.00 261.75 17.81 4.00 BallCalled 5.84 1.21 5.03 8.7
Four-Seam 91.27 2114.59 213.23 12.17 19.74 StrikeCalled 5.89 1.00 5.13 7.1
Changeup 82.62 1636.99 252.96 17.38 6.70 FoulBallNotFieldable 5.75 1.17 4.89 8.4
Four-Seam 91.44 2154.15 212.03 10.27 17.62 StrikeCalled 5.81 0.99 5.22 7.1
Four-Seam 89.51 1995.89 218.85 11.91 16.06 StrikeSwinging 5.90 1.00 4.93 7.3
Changeup 83.77 1441.69 262.11 16.49 3.63 StrikeCalled 5.92 0.99 4.94 8.7
Four-Seam 89.78 2085.32 211.35 10.72 18.86 StrikeSwinging 5.96 1.00 4.95 7.0
Four-Seam 88.31 2157.41 218.45 13.16 17.92 FoulBallNotFieldable 5.98 0.87 5.10 7.3
Changeup 82.23 1826.37 251.10 17.14 7.29 BallCalled 5.92 1.05 4.83 8.4
Four-Seam 90.20 2246.47 218.81 13.79 18.41 StrikeCalled 5.95 0.89 4.65 7.3
Four-Seam 91.18 2270.53 220.39 13.55 17.19 StrikeCalled 5.84 0.99 4.73 7.3
Four-Seam 89.51 2163.21 213.70 12.21 19.54 StrikeCalled 6.05 0.81 4.89 7.1
Changeup 82.40 1689.80 269.02 17.91 1.76 StrikeSwinging 5.97 0.96 4.80 9.0
Sinker 89.98 2217.31 225.06 15.88 17.08 FoulBallNotFieldable 5.98 0.82 4.91 7.5
Changeup 82.60 1613.56 242.98 12.45 7.68 BallCalled 5.97 0.96 4.94 8.1
Four-Seam 90.27 2156.58 221.72 14.69 17.67 InPlay 5.93 0.95 4.90 7.4
Four-Seam 89.82 2054.15 217.80 12.16 16.85 BallCalled 5.95 0.79 4.91 7.3
Changeup 81.03 1682.73 249.14 14.21 6.79 InPlay 5.91 1.04 4.99 8.3
Changeup 79.28 1554.28 241.70 12.97 8.44 BallCalled 5.98 1.09 4.80 8.1
Sinker 87.46 2007.92 222.50 14.05 16.57 StrikeCalled 6.05 0.88 4.89 7.4
Four-Seam 87.79 2101.65 214.33 12.16 19.00 BallCalled 6.00 0.92 4.93 7.1
Changeup 80.20 1776.00 240.08 16.78 11.11 FoulBallNotFieldable 5.97 0.94 4.81 8.0
Slider 81.94 1980.62 171.68 -1.05 8.65 FoulBallNotFieldable 5.91 0.97 4.89 5.7
Changeup 80.39 1560.98 261.53 14.39 3.59 HitByPitch 5.97 1.00 4.85 8.7
Changeup 79.76 1561.51 256.92 17.46 5.52 StrikeSwinging 5.87 1.20 4.84 8.6
Changeup 77.93 1531.34 244.49 13.54 7.97 BallCalled 5.89 1.14 4.97 8.1
Sinker 88.18 2116.14 221.83 15.03 18.04 FoulBallNotFieldable 5.95 1.07 5.09 7.4
Four-Seam 89.14 2167.79 214.91 12.15 18.74 InPlay 5.95 0.98 4.84 7.2
Four-Seam 89.37 2108.47 205.65 8.20 18.34 StrikeSwinging 6.03 0.97 4.96 6.9
Changeup 82.01 1629.87 255.79 17.59 5.91 StrikeCalled 5.95 1.00 5.01 8.5
Four-Seam 91.08 2281.39 211.76 12.39 21.27 BallCalled 6.02 0.71 4.83 7.1
Changeup 83.19 1872.55 232.86 14.39 12.35 BallCalled 6.03 0.85 4.78 7.8
Four-Seam 89.29 2165.73 219.97 15.56 19.87 FoulBallNotFieldable 6.06 0.83 4.95 7.3
NA 83.91 2111.02 NA NA NA InPlay 6.21 1.08 4.90 NA
# 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:  64
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Ben Alekson")
Summary Pitch Table for Ben Alekson
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 40.62% 26 8 18 30.77% 69.23% 81.51 1608.59 5.11 16.12 256.92 8.6 5.89 1.04 4.88
Four-Seam 40.62% 26 7 19 26.92% 73.08% 89.82 2139.37 18.85 12.47 215.26 7.2 5.96 0.91 4.97
Sinker 14.06% 9 3 6 33.33% 66.67% 89.40 2116.14 16.96 15.17 224.23 7.5 5.93 0.91 5.03
Slider 3.12% 2 1 1 50.00% 50.00% 82.63 2004.62 8.94 1.58 189.82 6.3 5.93 1.00 4.99
NA 1.56% 1 0 1 0.00% 100.00% 83.91 2111.02 NaN NaN NaN NaN 6.21 1.08 4.90
# 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("Ben Alekson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Ben Alekson maximum FB velocity:  91.64 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Ben_Alekson_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 Ben Alekson",
       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")
  )

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