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 Colin Dowlen
Colin_Dowlen_data <- Shore624 %>%
  filter(Pitcher == "Dowlen, Colin")

# Create a detailed table for each pitch
detailed_pitch_table <- Colin_Dowlen_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 Colin Dowlen")
Detailed Pitch Table for Colin Dowlen
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 87.22 2003.31 229.64 14.89 13.83 StrikeCalled 5.19 1.98 6.09 7.7
Curveball 76.10 2998.96 58.43 -20.25 -10.75 BallCalled 5.23 2.09 5.35 1.9
Changeup 86.18 1980.77 216.62 11.47 16.63 StrikeSwinging 5.20 2.25 5.88 7.2
Curveball 75.41 2937.59 52.34 -18.02 -12.21 StrikeCalled 5.21 2.15 5.21 1.7
Changeup 87.36 1980.81 231.16 14.00 12.49 BallCalled 5.07 2.00 6.04 7.7
Sinker 88.09 2052.79 222.96 14.59 16.83 StrikeCalled 5.10 2.15 6.06 7.4
Curveball 75.61 2975.31 38.05 -13.30 -15.31 StrikeCalled 5.29 2.08 5.37 1.3
Sinker 87.28 2042.89 240.24 16.02 10.38 BallCalled 5.25 2.03 5.48 8.0
Changeup 86.92 2006.47 233.73 15.00 12.21 BallCalled 5.09 2.05 6.01 7.8
Four-Seam 88.08 NA 219.54 13.48 17.48 StrikeCalled 5.22 2.05 5.99 7.3
Sinker 88.45 2065.82 229.50 14.32 13.42 StrikeCalled 5.22 1.92 5.98 7.7
Four-Seam 88.10 2081.83 216.70 13.70 19.53 BallCalled 5.22 1.92 6.02 7.2
Curveball 76.62 2991.49 60.63 -17.86 -8.51 FoulBallNotFieldable 5.35 2.02 5.46 2.0
Curveball 76.26 2962.42 61.61 -20.38 -9.24 StrikeSwinging 5.10 2.14 5.65 2.1
Sinker 88.99 2062.52 227.13 15.92 16.05 FoulBallNotFieldable 5.23 1.88 6.01 7.6
Sinker 88.43 1945.05 223.56 13.10 14.87 BallCalled 5.29 1.87 6.08 7.5
Curveball 75.10 2834.84 55.00 -19.05 -11.67 StrikeCalled 5.31 1.99 5.23 1.8
Curveball 75.26 2885.98 54.01 -20.89 -13.44 InPlay 5.34 2.04 5.37 1.8
# 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:  18
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Colin Dowlen")
Summary Pitch Table for Colin Dowlen
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 16.67% 3 2 1 66.67% 33.33% 86.82 1989.35 13.78 13.49 227.17 7.6 5.12 2.10 5.98
Curveball 38.89% 7 1 6 14.29% 85.71% 75.77 2940.94 -11.59 -18.54 54.30 1.8 5.26 2.07 5.38
Four-Seam 11.11% 2 1 1 50.00% 50.00% 88.09 2081.83 18.51 13.59 218.12 7.2 5.22 1.98 6.00
Sinker 33.33% 6 2 4 33.33% 66.67% 88.08 2028.73 14.23 14.81 228.84 7.7 5.21 1.97 5.95
# 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("Colin Dowlen maximum FB velocity: ", max_fb_velocity, "mph\n")
## Colin Dowlen maximum FB velocity:  88.99 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Colin_Dowlen_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 Colin Dowlen",
       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 Colin Dowlen",
       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")
  )