# 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
NA NA NA NA NA NA StrikeSwinging NA NA NA NA
Slider 83.12 2322.32 143.61 -3.47 6.17 StrikeSwinging 5.62 1.83 5.42 4.8
Four-Seam 90.23 2499.73 216.64 10.32 14.95 BallCalled 5.49 1.98 5.73 7.2
Slider 80.13 2469.70 82.30 -7.16 0.43 InPlay 5.72 1.77 5.18 2.7
Slider 82.99 2377.42 138.68 -2.68 4.46 FoulBallNotFieldable 5.64 1.93 5.26 4.6
Slider 84.94 2361.38 162.15 -1.62 6.30 StrikeCalled 5.58 1.76 5.42 5.4
Slider 84.57 2346.29 156.79 -1.33 4.35 StrikeCalled 5.62 1.79 5.25 5.2
Slider 79.76 2556.66 91.22 -5.13 1.40 StrikeCalled 5.75 1.68 5.18 3.0
Slider 83.68 2351.22 130.60 -4.67 5.38 InPlay 5.61 1.87 5.35 4.4
Slider 84.04 2425.46 200.67 2.49 7.92 BallCalled 5.51 1.75 5.43 6.7
Four-Seam 91.20 2453.67 222.75 14.95 17.40 StrikeCalled 5.51 1.90 6.08 7.4
Slider 85.38 2425.21 181.21 0.16 9.10 BallCalled 5.51 1.93 5.52 6.0
Sinker 90.73 2450.13 228.19 14.38 14.07 InPlay 5.56 1.83 5.96 7.6
Sinker 90.17 2372.35 225.65 17.33 18.27 BallCalled 5.61 1.75 5.73 7.5
Four-Seam 90.41 2428.89 220.18 15.40 19.58 BallCalled 5.58 1.77 5.93 7.3
Slider 83.69 2326.96 168.05 -2.25 11.98 StrikeCalled 5.63 1.85 5.19 5.6
Sinker 90.27 2377.66 254.61 21.32 7.22 InPlay 5.61 1.81 5.63 8.5
Four-Seam 90.44 2394.66 219.58 14.87 19.28 StrikeCalled 5.57 1.77 5.90 7.3
Sinker 90.88 2331.32 233.22 17.67 14.50 StrikeCalled 5.59 1.75 5.95 7.8
Four-Seam 91.22 2440.23 215.21 12.95 19.62 FoulBallNotFieldable 5.59 1.71 5.83 7.2
Slider 81.33 2354.71 114.33 -5.44 3.90 InPlay 5.69 1.78 5.13 3.8
Slider 84.42 2364.53 199.59 2.18 7.42 BallCalled 5.61 1.75 5.31 6.7
Slider 84.75 2440.19 182.01 0.15 5.54 BallCalled 5.68 1.90 5.14 6.1
Sinker 89.62 2382.43 230.17 14.58 13.32 StrikeCalled 5.56 1.81 6.03 7.7
Slider 84.17 2353.06 176.92 -0.34 7.55 BallCalled 5.53 1.73 5.41 5.9
Sinker 89.82 2335.06 240.65 16.62 10.51 BallCalled 5.60 1.84 5.73 8.0
Sinker 90.77 2414.27 227.47 13.81 13.82 FoulBallNotFieldable 5.66 1.79 5.89 7.6
Slider 84.86 2401.63 178.88 -0.16 9.37 StrikeCalled 5.68 1.81 5.18 6.0
Slider 86.20 2441.48 188.45 0.79 6.61 BallCalled 5.66 1.72 5.30 6.3
Sinker 92.08 2278.03 254.12 19.23 6.60 BallCalled 5.62 1.71 5.73 8.5
Four-Seam 91.73 2539.92 217.45 10.55 14.89 InPlay 5.61 1.77 5.79 7.2
Slider 87.09 2380.25 203.73 2.97 7.92 StrikeCalled 5.56 1.74 5.50 6.8
Sinker 90.73 2331.61 250.14 20.32 8.49 FoulBallNotFieldable 5.58 1.80 5.75 8.3
Four-Seam 91.67 2431.30 221.82 12.86 15.33 BallCalled 5.63 1.70 5.84 7.4
Changeup 83.36 1515.24 262.16 15.82 3.43 InPlay 5.59 1.81 5.57 8.7
Four-Seam 92.21 2423.44 224.69 15.08 16.30 FoulBallNotFieldable 5.55 1.83 5.90 7.5
Slider 84.61 2314.15 147.29 -2.96 5.84 InPlay 5.63 1.86 5.26 4.9
Sinker 91.28 2497.10 228.70 13.53 12.97 StrikeSwinging 5.69 1.84 5.78 7.6
Slider 84.92 2324.53 182.78 0.26 6.48 StrikeCalled 5.59 1.84 5.26 6.1
Four-Seam 92.62 2556.65 228.39 14.59 14.08 BallCalled 5.66 1.73 5.88 7.6
Sinker 93.27 2514.20 236.59 15.26 11.14 BallCalled 5.63 1.80 5.91 7.9
Slider 84.92 2378.62 133.62 -3.63 4.90 StrikeSwinging 5.57 1.71 5.50 4.5
Four-Seam 92.53 2552.66 223.34 11.65 13.43 BallCalled 5.53 1.80 6.04 7.4
Slider 85.74 2402.32 175.60 -0.45 6.99 BallCalled 5.66 1.80 5.32 5.9
Slider 85.81 2372.32 160.18 -1.66 5.86 StrikeSwinging 5.64 1.77 5.38 5.3
NA NA NA NA NA NA Undefined NA NA NA NA
Sinker 92.51 2525.28 230.11 13.98 12.75 StrikeSwinging 5.58 1.87 5.95 7.7
Slider 83.45 2349.82 65.83 -3.47 -0.16 BallCalled 5.60 1.71 5.61 2.2
Sinker 90.40 2202.14 241.69 16.00 9.79 StrikeCalled 5.49 1.78 6.08 8.1
Slider 83.05 2259.65 157.87 -2.98 8.69 StrikeSwinging 5.61 1.79 5.35 5.3
Four-Seam 90.08 2524.01 216.94 11.88 16.95 BallCalled 5.61 1.87 5.87 7.2
Slider 84.38 2279.52 202.14 3.09 8.74 InPlay 5.54 1.80 5.47 6.7
Four-Seam 90.19 2410.82 217.84 12.51 17.43 StrikeSwinging 5.69 1.74 5.58 7.3
Sinker 90.55 2237.86 251.46 19.07 7.68 InPlay 5.53 1.84 5.91 8.4
Sinker 90.90 2512.58 230.61 15.91 14.33 InPlay 5.54 1.82 5.99 7.7
Slider 86.01 2388.85 184.37 0.77 11.27 BallCalled 5.63 1.69 5.20 6.1
Four-Seam 90.61 2508.80 206.92 9.57 20.17 StrikeSwinging 5.56 1.84 5.92 6.9
Cutter 85.59 2407.65 165.20 -2.44 10.46 BallCalled 5.62 1.76 5.21 5.5
Four-Seam 91.97 2564.62 204.47 8.42 19.78 StrikeSwinging 5.56 1.72 5.85 6.8
Slider 86.47 2456.12 174.79 -0.83 10.33 InPlay 5.58 1.73 5.11 5.8
Sinker 90.98 2261.75 270.29 23.52 1.18 BallCalled 5.55 1.86 6.19 9.0
Slider 84.27 2311.13 172.35 -1.36 11.33 StrikeCalled 5.60 1.83 5.49 5.7
Four-Seam 90.19 2473.09 209.91 10.09 18.87 BallCalled 5.52 1.87 5.93 7.0
Four-Seam 90.88 2541.80 222.59 14.04 16.55 StrikeSwinging 5.55 1.85 5.95 7.4
Slider 85.03 2403.36 148.49 -4.27 8.23 InPlay 5.60 1.76 5.31 4.9
Slider 84.32 2285.64 193.82 2.02 9.52 BallCalled 5.44 1.88 5.55 6.5
Four-Seam 90.67 2456.59 222.26 12.72 15.29 StrikeCalled 5.47 1.96 5.96 7.4
Slider 84.36 2382.45 171.12 -1.43 10.49 BallCalled 5.50 1.88 5.42 5.7
Sinker 88.17 2189.07 248.36 16.83 7.95 InPlay 5.40 1.88 6.04 8.3
Four-Seam 92.30 2459.13 215.61 11.13 16.75 StrikeCalled 5.62 1.80 5.78 7.2
Slider 85.15 2363.95 186.04 0.91 9.74 BallCalled 5.51 1.82 5.73 6.2
Four-Seam 91.30 2560.50 227.37 14.92 15.00 BallCalled 5.53 1.80 6.01 7.6
Four-Seam 91.39 2554.37 219.36 12.61 16.61 FoulBallNotFieldable 5.46 1.84 6.04 7.3
Changeup 84.21 1504.10 267.78 17.23 2.00 InPlay 5.58 1.86 5.54 8.9
# 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:  74
# 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 2.70% 2 0 2 0.00% 100.00% 83.78 1509.67 2.71 16.52 264.97 8.8 5.58 1.83 5.56
Cutter 1.35% 1 1 0 100.00% 0.00% 85.59 2407.65 10.46 -2.44 165.20 5.5 5.62 1.76 5.21
Four-Seam 27.03% 20 8 12 40.00% 60.00% 91.19 2488.74 16.91 12.56 218.67 7.3 5.56 1.81 5.89
Sinker 22.97% 17 5 12 29.41% 70.59% 90.77 2365.46 10.86 17.02 240.12 8.0 5.58 1.81 5.90
Slider 43.24% 32 12 20 37.50% 62.50% 84.30 2374.09 7.00 -1.30 161.11 5.4 5.60 1.80 5.35
NA 2.70% 2 0 2 0.00% 100.00% NaN NaN NaN NaN NaN NaN NaN NaN NaN
# 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("Nick Falter maximum FB velocity: ", max_fb_velocity, "mph\n")
## Nick Falter maximum FB velocity:  93.27 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 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")
  )