# Filter the data for the pitcher Charlie Hale
Brian_Foley_data <- data %>%
  filter(Pitcher == "Hale, Charlie")

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_Foley_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 Charlie Hale")
Detailed Pitch Table for Charlie Hale
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 84.27 2061.05 245.79 15.17 8.17 InPlay 5.41 3.17 5.41 8.2
Slider 76.81 2490.22 94.82 -13.10 2.64 StrikeCalled 5.43 3.26 5.07 3.2
Curveball 76.34 2501.64 95.56 -14.00 2.94 InPlay 5.43 3.38 5.09 3.2
Changeup 83.94 2148.81 228.89 15.23 14.73 StrikeCalled 5.54 3.24 5.81 7.6
Changeup 78.14 1541.97 255.20 15.17 5.57 BallCalled 5.37 3.35 5.37 8.5
Changeup 78.13 1567.63 260.16 17.62 4.48 BallCalled 5.30 2.96 5.33 8.7
Changeup 84.28 2098.65 264.32 16.58 2.79 BallCalled 5.48 3.02 5.60 8.8
Changeup 84.02 2106.62 251.60 13.28 5.73 InPlay 5.39 2.96 5.71 8.4
Slider 77.52 2529.47 111.62 -13.86 6.98 FoulBallNotFieldable 5.41 3.14 5.32 3.7
Changeup 76.83 1393.33 236.58 9.55 7.90 FoulBallNotFieldable 5.36 2.89 5.36 7.9
Changeup 76.82 1398.42 287.58 14.34 -2.98 StrikeSwinging 5.19 3.10 5.24 9.6
Slider 76.85 2432.76 103.32 -15.70 5.29 InPlay 5.42 3.05 5.23 3.4
Changeup 85.46 2218.43 244.12 13.03 7.48 StrikeCalled 5.46 2.99 5.68 8.1
Changeup 79.34 1447.97 279.39 11.85 -0.56 InPlay 5.33 3.00 5.33 9.3
Slider 78.14 2442.30 109.19 -12.05 6.08 InPlay 5.45 3.17 5.22 3.6
Changeup 84.80 2145.61 237.41 13.26 9.90 FoulBallNotFieldable 5.54 2.55 5.72 7.9
Slider 77.48 2465.28 107.00 -17.24 6.75 FoulBallNotFieldable 5.57 2.81 5.20 3.6
Slider 77.79 2471.23 90.20 -17.11 1.62 BallCalled 5.62 2.81 5.28 3.0
Changeup 84.40 2166.66 254.23 14.71 5.49 HitByPitch 5.49 2.89 5.41 8.5
Slider 77.49 2481.80 104.21 -14.35 5.25 BallCalled 5.51 3.21 5.32 3.5
Slider 75.54 2370.83 95.62 -13.73 3.00 InPlay 5.41 3.09 5.36 3.2
# 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 %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # 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:  21
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Charlie Hale")
Summary Pitch Table for Charlie Hale
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 57.14% 12 3 9 25.00% 75.00% 81.70 1857.93 5.73 14.15 253.77 8.5 5.40 3.01 5.50
Curveball 4.76% 1 0 1 0.00% 100.00% 76.34 2501.64 2.94 -14.00 95.56 3.2 5.43 3.38 5.09
Slider 38.10% 8 2 6 25.00% 75.00% 77.20 2460.49 4.70 -14.64 102.00 3.4 5.48 3.07 5.25
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Charlie Hale maximum FB velocity: ", max_fb_velocity, "mph\n")
## Charlie Hale maximum FB velocity:  -Inf mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_data %>%
  select(AutoPitchType, PlateLocHeight, PlateLocSide, PitchCall) %>%
  rename(
    PitchHeight = PlateLocHeight,
    PitchSide = PlateLocSide
  ) %>%
  mutate(
    SwingTake = ifelse(PitchCall %in% c("StrikeSwinging", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | 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.85, xmax = 0.85, 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 Charlie Hale",
       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"), 
       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")
  )