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

# 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 Andrew Luczak")
Detailed Pitch Table for Andrew Luczak
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 91.59 2462.54 233.76 19.32 15.40 StrikeCalled 5.11 2.87 5.96 7.8
Sinker 90.95 2340.41 252.21 19.79 7.48 StrikeCalled 4.87 2.98 6.13 8.4
Sinker 90.47 2479.87 243.31 20.49 11.43 BallCalled 4.69 2.87 6.06 8.1
Sinker 92.08 2437.24 234.76 19.80 15.15 InPlay 4.85 2.84 6.23 7.8
Curveball 77.99 2659.73 55.02 -12.70 -7.35 BallCalled 4.98 2.87 6.17 1.8
Sinker 91.46 2390.58 233.98 19.07 15.14 StrikeCalled 4.88 2.88 6.03 7.8
Sinker 91.37 2476.55 247.38 22.52 10.60 FoulBallNotFieldable 4.81 2.87 6.22 8.2
Curveball 78.38 2566.36 58.68 -14.72 -7.44 BallinDirt 4.90 2.90 6.09 2.0
Sinker 90.59 2324.49 238.00 20.33 13.90 FoulBallNotFieldable 4.74 2.79 6.44 7.9
Sinker 89.96 2423.21 235.32 16.25 12.34 StrikeCalled 4.68 2.77 6.34 7.8
Sinker 90.14 2475.91 241.75 21.26 12.48 FoulBallNotFieldable 4.98 2.79 6.27 8.1
Slider 79.48 2612.10 73.33 -13.04 -2.29 StrikeCalled 4.98 2.78 6.17 2.4
Slider 77.91 2637.38 67.90 -18.02 -5.76 BallinDirt 5.25 2.66 5.78 2.3
Sinker 90.27 2359.75 251.28 17.80 7.12 BallCalled 4.84 2.75 6.19 8.4
Curveball 75.46 2566.66 65.14 -14.74 -5.17 InPlay 5.09 2.76 5.71 2.2
Changeup 83.66 1959.87 249.50 17.29 7.78 BallCalled 5.18 2.53 6.02 8.3
Sinker 91.55 2417.87 250.76 20.01 8.08 FoulBallNotFieldable 4.96 2.80 5.79 8.4
Sinker 91.88 2438.16 240.83 16.19 10.14 BallCalled 5.00 2.64 6.09 8.0
Four-Seam 92.08 2301.58 223.89 14.88 16.64 FoulBallNotFieldable 5.23 2.47 6.38 7.5
Changeup 85.41 1886.29 225.37 12.84 13.97 InPlay 5.15 2.41 6.32 7.5
Slider 78.21 2605.62 80.92 -15.02 -0.85 FoulBallNotFieldable 5.08 2.57 5.69 2.7
Sinker 91.30 2360.18 236.41 18.69 13.45 FoulBallNotFieldable 5.07 2.49 5.89 7.9
Slider 79.13 2758.04 77.64 -15.62 -2.03 InPlay 5.14 2.47 5.85 2.6
Sinker 91.39 2292.85 229.57 18.25 16.63 StrikeCalled 5.05 2.65 6.18 7.7
Sinker 92.38 2280.98 231.50 17.67 15.13 BallCalled 5.04 2.35 6.08 7.7
Sinker 91.60 2334.55 247.67 18.58 8.66 StrikeCalled 4.70 2.65 6.08 8.3
Curveball 77.21 2595.42 71.83 -12.74 -2.78 StrikeCalled 4.96 2.79 6.00 2.4
Sinker 89.95 2545.59 244.97 21.05 11.02 StrikeCalled 5.09 2.35 5.88 8.2
Changeup 82.29 2027.09 240.86 18.82 11.94 BallCalled 5.26 2.39 6.21 8.0
Sinker 90.56 2464.96 249.79 20.24 8.58 BallCalled 4.91 2.54 6.18 8.3
Sinker 90.31 2451.85 242.04 19.71 11.60 BallCalled 5.08 2.46 6.06 8.1
Sinker 91.63 2523.46 237.23 18.74 13.19 StrikeCalled 5.04 2.46 5.79 7.9
Sinker 90.66 2441.82 246.47 19.40 9.55 InPlay 5.06 2.41 6.00 8.2
Curveball 76.87 2630.22 84.64 -10.23 0.56 HitByPitch 5.19 2.52 5.80 2.8
Sinker 89.68 2296.22 240.11 19.58 12.40 FoulBallNotFieldable 4.91 2.56 6.17 8.0
Sinker 90.19 2465.37 242.39 19.53 11.32 BallCalled 4.97 2.73 6.05 8.1
Sinker 89.51 2307.47 243.37 18.36 10.30 BallCalled 4.76 2.70 6.51 8.1
Sinker 89.35 2394.42 241.53 21.69 12.97 BallCalled 5.22 2.39 6.41 8.1
Sinker 90.89 2465.97 243.34 22.75 12.55 FoulBallNotFieldable 4.89 2.67 6.16 8.1
Sinker 90.68 2487.53 229.54 18.22 16.63 BallCalled 4.85 2.63 5.84 7.7
Sinker 89.49 2454.79 253.71 20.57 7.07 BallCalled 4.89 2.65 6.24 8.5
Sinker 90.82 2446.80 235.31 19.35 14.48 BallCalled 4.93 2.54 5.88 7.8
Sinker 90.98 2521.00 254.66 22.62 7.32 BallCalled 4.97 2.53 6.08 8.5
Sinker 91.39 2517.69 256.91 22.20 6.26 BallCalled 4.66 2.70 5.98 8.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:  44
# 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 6.82% 3 2 1 66.67% 33.33% 83.79 1957.75 11.23 16.32 238.58 7.9 5.20 2.44 6.18
Curveball 11.36% 5 1 4 20.00% 80.00% 77.18 2603.68 -4.44 -13.03 67.06 2.2 5.02 2.77 5.95
Four-Seam 2.27% 1 0 1 0.00% 100.00% 92.08 2301.58 16.64 14.88 223.89 7.5 5.23 2.47 6.38
Sinker 70.45% 31 14 17 45.16% 54.84% 90.81 2421.94 11.56 19.68 242.25 8.1 4.92 2.66 6.10
Slider 9.09% 4 0 4 0.00% 100.00% 78.68 2653.28 -2.73 -15.42 74.95 2.5 5.11 2.62 5.87
# 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.38 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", "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")
  )

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