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

# 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 Nick Falter")
Detailed Pitch Table for Nick Falter
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 91.71 2498.81 226.46 13.65 14.11 StrikeCalled 5.55 1.64 5.48 7.5
Slider 86.71 2445.62 177.65 -0.31 8.74 FoulBallNotFieldable 5.61 1.65 4.88 5.9
Sinker 92.71 2543.65 239.71 16.60 10.72 BallCalled 5.55 1.65 5.46 8.0
Slider 82.28 2506.62 107.14 -2.52 2.06 InPlay 5.74 1.69 4.96 3.6
Sinker 91.36 2462.07 235.80 17.13 12.75 BallCalled 5.64 1.65 5.67 7.9
Slider 86.21 2437.48 205.09 3.57 8.80 InPlay 5.70 1.78 5.23 6.8
Sinker 92.30 2446.06 230.56 16.73 14.86 StrikeCalled 5.55 1.64 5.52 7.7
Slider 86.33 2425.44 200.04 2.82 8.81 InPlay 5.62 1.61 5.13 6.7
Sinker 92.31 2476.55 234.57 17.93 13.88 StrikeSwinging 5.62 1.60 5.38 7.8
Four-Seam 93.65 2552.64 232.58 17.76 14.66 BallCalled 5.67 1.58 5.64 7.8
Slider 85.24 2363.78 183.72 0.51 8.87 StrikeCalled 5.68 1.56 5.15 6.1
Four-Seam 94.02 2558.31 221.48 14.24 17.17 StrikeSwinging 5.67 1.65 5.35 7.4
Sinker 93.09 2466.04 235.37 17.22 12.98 FoulBallNotFieldable 5.59 1.67 5.33 7.8
Four-Seam 92.87 2480.52 230.62 14.96 13.49 FoulBallNotFieldable 5.57 1.56 5.42 7.7
Sinker 93.54 2523.83 235.76 15.85 11.81 InPlay 5.62 1.65 5.33 7.9
Sinker 91.84 2193.27 249.77 16.98 7.27 StrikeCalled 5.55 1.69 5.38 8.3
Sinker 91.97 2276.20 258.18 17.52 4.72 InPlay 5.56 1.66 5.28 8.6
Four-Seam 89.72 2450.93 215.45 11.04 16.68 BallCalled 5.54 1.85 4.98 7.2
Slider 85.44 2385.33 176.06 -0.59 9.80 BallCalled 5.52 1.63 4.95 5.9
Four-Seam 91.80 2498.88 222.56 13.94 16.27 StrikeSwinging 5.51 1.77 5.48 7.4
Slider 86.47 2402.06 193.44 2.31 10.80 StrikeSwinging 5.56 1.79 4.91 6.4
Four-Seam 91.51 2533.48 209.10 7.86 15.21 FoulBallNotFieldable 5.41 1.62 5.44 7.0
Four-Seam 91.72 2529.81 227.68 12.28 12.22 InPlay 5.42 1.76 5.37 7.6
Sinker 91.43 2203.72 235.74 15.31 11.46 BallCalled 5.44 1.76 5.92 7.9
Four-Seam 92.44 2419.71 222.99 10.23 12.00 FoulBallNotFieldable 5.39 1.59 5.65 7.4
Four-Seam 92.24 2491.01 229.21 14.24 13.40 BallCalled 5.44 1.73 5.33 7.6
Cutter 86.55 2359.15 178.87 -0.20 11.64 BallCalled 5.62 1.80 4.95 6.0
Four-Seam 93.25 2487.51 216.29 9.84 14.48 BallCalled 5.45 1.72 5.37 7.2
Four-Seam 92.35 2421.88 209.50 9.38 17.77 FoulBallNotFieldable 5.58 1.54 5.41 7.0
Slider 85.66 2351.17 180.21 0.01 5.22 InPlay 5.59 1.56 4.92 6.0
Four-Seam 92.49 2441.01 224.82 13.47 14.61 StrikeSwinging 5.62 1.64 5.37 7.5
Four-Seam 93.59 2526.86 221.08 12.38 15.20 StrikeSwinging 5.65 1.58 5.30 7.4
Slider 87.51 2376.96 173.69 -0.82 8.38 InPlay 5.64 1.57 5.14 5.8
Slider 83.79 2342.31 260.33 0.77 1.31 StrikeCalled 5.59 1.74 4.90 8.7
Sinker 90.71 2496.87 232.39 15.08 12.76 BallCalled 5.50 1.80 5.15 7.7
Four-Seam 91.51 2525.33 226.55 12.96 13.39 StrikeSwinging 5.59 1.79 5.28 7.6
Slider 83.05 2306.38 68.65 -7.13 -1.42 StrikeSwinging 5.61 1.52 5.00 2.3
Slider 85.77 2403.54 227.11 1.73 2.69 InPlay 5.57 1.67 4.98 7.6
Four-Seam 92.97 2509.48 230.89 15.09 13.39 StrikeCalled 5.56 1.53 5.49 7.7
Slider 85.91 2395.00 194.54 1.06 5.15 StrikeSwinging 5.58 1.57 5.00 6.5
Slider 85.98 2426.75 163.07 -1.39 5.71 BallCalled 5.70 1.42 5.01 5.4
Four-Seam 93.84 2543.81 221.21 12.27 15.08 StrikeSwinging 5.58 1.59 5.24 7.4
Four-Seam 92.79 2594.42 230.15 14.18 12.92 BallCalled 5.59 1.58 5.33 7.7
Slider 86.09 2437.69 201.71 2.65 7.87 BallCalled 5.59 1.64 4.87 6.7
Slider 84.50 2370.85 181.19 0.16 8.90 InPlay 5.64 1.76 5.04 6.0
Sinker 91.02 2492.04 227.20 14.78 14.86 FoulBallNotFieldable 5.41 1.71 5.57 7.6
Changeup 85.49 1724.01 272.30 18.00 0.54 BallCalled 5.46 1.70 5.10 9.1
Sinker 91.22 2286.74 255.13 21.85 6.99 FoulBallNotFieldable 5.45 1.80 5.44 8.5
Slider 87.70 2447.35 209.19 4.15 8.55 BallCalled 5.40 1.66 5.13 7.0
Four-Seam 93.29 2547.49 220.56 12.41 15.70 StrikeSwinging 5.48 1.70 5.27 7.4
Slider 86.20 2423.01 187.66 0.93 8.07 BallCalled 5.34 1.68 5.23 6.3
Four-Seam 92.43 2582.18 232.84 16.97 14.01 InPlay 5.41 1.73 5.46 7.8
Sinker 92.96 2521.77 232.92 14.73 12.25 BallCalled 5.41 1.52 5.72 7.8
Four-Seam 93.61 2547.78 227.75 11.78 11.78 StrikeCalled 5.42 1.61 5.60 7.6
Four-Seam 94.18 2559.51 232.55 14.10 11.88 BallCalled 5.40 1.48 5.77 7.8
Slider 86.87 2474.85 195.80 2.87 11.28 FoulBallNotFieldable 5.48 1.77 5.12 6.5
Slider 83.22 2353.99 101.65 -3.55 2.08 InPlay 5.60 1.62 5.02 3.4
Sinker 91.37 2549.08 230.24 15.45 14.02 StrikeSwinging 5.51 1.67 5.50 7.7
Cutter 87.18 2445.23 204.03 5.51 13.43 BallCalled 5.47 1.65 5.32 6.8
Slider 86.52 2448.69 188.92 1.70 12.07 StrikeCalled 5.58 1.71 5.08 6.3
Slider 81.93 2317.49 179.25 -0.03 3.50 BallCalled 5.66 1.68 4.99 6.0
Slider 84.20 2337.39 103.93 -4.27 2.37 BallCalled 5.62 1.58 5.10 3.5
Four-Seam 92.51 2500.36 219.08 12.74 16.82 InPlay 5.42 1.63 5.77 7.3
Slider 87.84 2513.85 206.41 5.17 11.44 BallCalled 5.44 1.77 5.50 6.9
Slider 86.54 2495.90 191.23 1.89 10.64 BallCalled 5.51 1.77 5.21 6.4
NA 92.35 2394.53 NA NA NA InPlay 5.99 2.00 5.49 NA
Four-Seam 92.72 2512.63 229.05 15.38 14.46 InPlay 5.42 1.61 5.76 7.6
Slider 87.12 2430.34 200.92 3.85 11.17 BallCalled 5.61 1.63 5.40 6.7
Four-Seam 93.22 2522.81 215.65 11.54 17.21 FoulBallNotFieldable 5.56 1.62 5.45 7.2
Four-Seam 93.14 2573.31 232.52 16.79 14.11 HitByPitch 5.55 1.65 5.42 7.8
Slider 81.70 2214.28 171.94 -0.57 5.46 StrikeCalled 5.64 1.64 5.11 5.7
Slider 87.31 2455.32 208.76 3.48 7.41 StrikeSwinging 5.54 1.64 5.00 7.0
Four-Seam 93.84 2604.21 225.54 14.35 15.24 StrikeSwinging 5.55 1.67 5.29 7.5
Slider 87.24 2453.88 199.11 3.76 11.96 FoulBallNotFieldable 5.56 1.61 5.14 6.6
Four-Seam 91.48 2557.99 227.54 14.91 14.77 FoulBallNotFieldable 5.46 1.64 5.60 7.6
Sinker 92.25 2539.25 241.39 19.13 11.44 InPlay 5.41 1.60 5.63 8.0
Sinker 93.16 2463.28 243.56 18.21 10.01 BallCalled 5.47 1.79 5.41 8.1
Sinker 92.43 2496.53 243.35 18.41 10.24 FoulBallNotFieldable 5.40 1.57 5.73 8.1
Sinker 92.35 2282.39 252.80 19.27 7.13 BallCalled 5.41 1.70 5.42 8.4
Sinker 92.56 2273.89 246.74 17.60 8.68 StrikeCalled 5.46 1.71 5.41 8.2
Slider 81.82 2300.04 104.70 -6.10 2.93 StrikeCalled 5.55 1.62 4.96 3.5
Four-Seam 92.95 2554.65 219.92 13.24 16.97 StrikeCalled 5.41 1.68 5.70 7.3
Slider 85.15 2372.71 234.47 4.32 4.40 BallCalled 5.54 1.57 4.98 7.8
Slider 86.72 2433.72 195.73 1.94 7.94 BallCalled 5.44 1.63 5.15 6.5
Sinker 91.66 2275.15 247.81 15.88 7.60 InPlay 5.40 1.74 5.40 8.3
Sinker 91.69 2237.89 263.03 19.47 3.59 StrikeCalled 5.52 1.58 5.23 8.8
Four-Seam 92.72 2581.23 212.76 10.49 17.44 BallCalled 5.61 1.52 5.56 7.1
Four-Seam 93.07 2531.20 216.76 12.02 17.22 StrikeSwinging 5.62 1.52 5.42 7.2
Four-Seam 93.52 2586.12 211.31 10.13 17.74 StrikeSwinging 5.54 1.67 5.44 7.0
# 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:  89
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Nick Falter")
Summary Pitch Table for Nick Falter
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 1.12% 1 1 0 100.00% 0.00% 85.49 1724.01 0.54 18.00 272.30 9.1 5.46 1.70 5.10
Cutter 2.25% 2 2 0 100.00% 0.00% 86.87 2402.19 12.54 2.65 191.45 6.4 5.54 1.73 5.14
Four-Seam 35.96% 32 7 25 21.88% 78.12% 92.72 2525.81 14.92 13.02 223.20 7.4 5.52 1.64 5.45
Sinker 23.60% 21 7 14 33.33% 66.67% 92.09 2405.06 10.48 17.20 241.52 8.1 5.50 1.67 5.47
Slider 35.96% 32 12 20 37.50% 62.50% 85.47 2401.56 6.97 0.70 180.42 6.0 5.58 1.65 5.07
NA 1.12% 1 0 1 0.00% 100.00% 92.35 2394.53 NaN NaN NaN NaN 5.99 2.00 5.49
# 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("Nick Falter maximum FB velocity: ", max_fb_velocity, "mph\n")
## Nick Falter maximum FB velocity:  94.18 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 Nick Falter",
       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")
  )