# 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 Andrew Luczak
Andrew_Luczak_data <- data %>%
  filter(Pitcher == "Luczak, Andrew")

# Create a detailed table for each pitch
detailed_pitch_table <- Andrew_Luczak_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 Andrew Luczak")
Detailed Pitch Table for Andrew Luczak
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 88.61 2372.26 249.16 22.12 9.81 BallCalled 5.24 2.55 5.58 8.3
Sinker 88.95 2323.84 252.35 22.13 8.27 StrikeCalled 5.24 2.57 5.50 8.4
Curveball 76.87 2460.83 53.54 -7.16 -3.56 InPlay 5.23 2.67 5.47 1.8
Changeup 87.54 2237.26 249.83 20.77 8.79 StrikeCalled 5.09 2.87 5.60 8.3
Curveball 79.04 2456.46 37.98 -4.29 -4.02 BallCalled 5.07 2.89 5.65 1.3
Curveball 79.17 2489.37 54.33 -12.12 -7.21 FoulBallNotFieldable 5.35 2.77 5.50 1.8
Sinker 88.37 2302.03 251.17 20.68 8.21 FoulBallNotFieldable 5.02 2.86 5.70 8.4
Slider 79.50 2489.89 43.58 -3.08 -1.78 InPlay 5.26 2.84 5.31 1.5
Curveball 79.10 2408.86 32.32 -3.98 -4.82 StrikeSwinging 5.31 2.88 5.41 1.1
Sinker 90.22 2241.75 244.35 18.95 10.17 StrikeSwinging 5.01 2.84 5.52 8.1
Slider 79.41 2623.07 74.35 -8.02 -0.96 FoulBallNotFieldable 5.23 2.80 5.34 2.5
Slider 81.36 2690.50 75.83 -9.99 -1.31 BallCalled 5.25 2.69 5.73 2.5
Sinker 92.00 2340.84 246.02 18.08 9.13 StrikeSwinging 5.00 2.87 5.91 8.2
Sinker 92.06 2243.65 226.23 18.31 18.65 StrikeCalled 5.38 2.67 5.90 7.5
Sinker 91.29 2286.29 243.17 18.22 10.33 BallCalled 4.98 2.88 5.67 8.1
Sinker 89.87 2157.27 243.01 19.17 10.86 InPlay 5.08 2.77 5.55 8.1
Curveball 78.51 2503.40 65.63 -10.56 -3.27 BallCalled 5.17 2.88 5.32 2.2
Curveball 79.70 2504.44 355.63 0.50 -5.23 StrikeCalled 5.38 2.64 5.81 11.9
Sinker 90.46 2203.85 241.05 16.31 10.23 StrikeSwinging 4.97 2.80 5.50 8.0
Sinker 90.81 2362.51 250.37 19.48 8.08 StrikeCalled 5.21 2.88 5.23 8.3
Sinker 90.88 2196.15 248.72 18.74 8.47 BallCalled 5.22 3.01 5.43 8.3
Sinker 90.63 2279.99 233.90 20.40 16.05 BallCalled 5.41 2.71 5.66 7.8
Sinker 90.33 2279.45 245.46 19.60 10.09 StrikeSwinging 5.04 2.88 5.39 8.2
Sinker 90.34 2220.38 230.32 19.17 17.06 BallCalled 5.46 2.71 5.55 7.7
Sinker 90.28 2265.68 247.23 20.27 9.64 StrikeCalled 5.10 2.72 5.53 8.2
Curveball 79.97 2569.65 353.98 1.20 -9.93 BallCalled 5.22 2.91 5.61 11.8
Sinker 90.09 2348.32 253.38 23.36 8.23 BallCalled 4.91 2.78 5.79 8.4
Sinker 90.09 2132.93 246.25 19.01 9.53 BallCalled 5.04 2.89 5.51 8.2
Sinker 88.72 2231.14 254.45 21.16 7.11 BallCalled 5.12 2.89 5.70 8.5
Sinker 88.92 2124.16 253.35 21.72 7.71 StrikeCalled 5.12 2.79 5.96 8.4
Sinker 89.11 2331.95 237.11 20.36 14.43 FoulBallNotFieldable 5.22 2.90 5.78 7.9
Sinker 90.92 2362.67 252.76 21.93 7.87 BallCalled 4.90 2.89 5.83 8.4
Sinker 89.69 2460.12 229.01 18.60 17.34 InPlay 5.29 2.62 5.73 7.6
# 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:  33
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Andrew Luczak")
Summary Pitch Table for Andrew Luczak
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 3.03% 1 0 1 0.00% 100.00% 87.54 2237.26 8.79 20.77 249.83 8.3 5.09 2.87 5.60
Curveball 21.21% 7 3 4 42.86% 57.14% 78.91 2484.72 -5.43 -5.20 136.20 4.6 5.25 2.81 5.54
Sinker 66.67% 22 9 13 40.91% 59.09% 90.12 2275.78 10.79 19.90 244.49 8.1 5.13 2.79 5.63
Slider 9.09% 3 1 2 33.33% 66.67% 80.09 2601.15 -1.35 -7.03 64.59 2.2 5.25 2.78 5.46
# 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("Andrew Luczak maximum FB velocity: ", max_fb_velocity, "mph\n")
## Andrew Luczak maximum FB velocity:  92.06 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Andrew_Luczak_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 Andrew Luczak",
       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")
  )