# 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
Sinker 90.82 2482.99 250.91 20.14 8.23 FoulBallNotFieldable 5.48 1.59 5.24 8.4
Slider 84.96 2355.02 172.18 -0.70 6.22 BallCalled 5.46 1.75 4.99 5.7
Sinker 91.06 2349.55 233.37 15.58 12.72 StrikeSwinging 5.44 1.80 5.62 7.8
Slider 86.12 2404.15 163.03 -1.27 5.27 FoulBallNotFieldable 5.46 1.74 5.03 5.4
Sinker 92.46 2238.06 243.65 19.24 10.68 StrikeSwinging 5.43 1.71 5.42 8.1
Four-Seam 92.06 2465.65 226.24 13.64 14.15 StrikeCalled 5.41 1.68 5.53 7.5
Four-Seam 92.72 2496.38 226.39 13.93 14.35 BallCalled 5.46 1.63 5.69 7.5
Slider 83.80 2377.77 149.66 -3.29 6.97 StrikeSwinging 5.51 1.67 5.06 5.0
Slider 86.08 2337.63 194.66 1.28 6.03 FoulBallNotFieldable 5.52 1.65 5.09 6.5
Slider 81.51 2745.80 120.36 -3.42 3.34 InPlay 5.57 1.58 5.01 4.0
Sinker 91.34 2216.81 234.72 17.26 13.37 StrikeSwinging 5.39 1.64 5.67 7.8
Sinker 91.34 2448.71 226.55 14.29 14.61 FoulBallNotFieldable 5.45 1.78 5.46 7.6
Four-Seam 91.76 2469.27 222.92 11.86 13.79 BallCalled 5.39 1.69 5.25 7.4
Cutter 88.05 2532.67 201.94 3.44 9.50 InPlay 5.38 1.83 5.13 6.7
Four-Seam 91.72 2471.35 219.31 12.61 16.55 FoulBallNotFieldable 5.57 1.70 5.37 7.3
Changeup 84.53 1766.02 273.31 19.03 0.22 BallCalled 5.49 1.83 5.10 9.1
Sinker 91.66 2279.65 253.44 20.55 7.32 BallCalled 5.41 1.80 5.57 8.4
Four-Seam 91.48 2436.12 219.78 12.83 16.61 StrikeSwinging 5.45 1.75 5.31 7.3
Slider 86.38 2401.40 193.35 1.74 8.44 BallCalled 5.43 1.82 5.08 6.4
Slider 85.31 2355.99 181.96 0.20 7.05 InPlay 5.43 1.80 4.89 6.1
Sinker 90.80 2168.43 243.19 18.51 10.57 BallCalled 5.45 1.83 5.50 8.1
Sinker 90.57 2165.49 240.35 18.89 11.95 StrikeSwinging 5.49 1.73 5.44 8.0
Cutter 87.11 2421.76 168.95 -2.15 12.27 BallCalled 5.46 1.65 5.13 5.6
Four-Seam 91.99 2501.05 230.70 12.55 11.44 InPlay 5.49 1.82 5.26 7.7
Sinker 91.87 2499.71 229.82 14.93 13.73 BallCalled 5.48 1.73 5.14 7.7
Sinker 90.94 2213.12 242.77 18.19 10.50 StrikeSwinging 5.40 1.81 5.60 8.1
Sinker 89.83 2429.12 234.75 16.03 12.51 StrikeSwinging 5.47 1.81 4.98 7.8
Sinker 91.45 2234.72 243.76 16.78 9.45 StrikeSwinging 5.49 1.92 5.10 8.1
Four-Seam 92.66 2480.37 229.94 15.35 14.08 FoulBallNotFieldable 5.48 1.72 5.35 7.7
Slider 86.17 2358.31 176.23 -0.36 6.63 InPlay 5.45 1.82 4.91 5.9
Sinker 91.01 2127.76 242.30 18.90 11.12 StrikeCalled 5.37 1.81 5.61 8.1
Sinker 91.97 2203.38 231.89 16.20 13.89 FoulBallNotFieldable 5.47 1.77 5.46 7.7
Four-Seam 92.85 2523.07 226.46 14.21 14.73 FoulBallNotFieldable 5.52 1.67 5.20 7.5
Changeup 85.30 1809.46 275.40 18.88 -0.44 FoulBallNotFieldable 5.45 1.67 5.32 9.2
Changeup 86.54 1990.66 270.63 19.22 1.07 BallCalled 5.47 1.61 5.30 9.0
Slider 88.17 2497.24 181.63 0.24 9.56 StrikeSwinging 5.51 1.55 4.96 6.1
Sinker 90.61 2297.17 233.80 16.38 13.17 StrikeCalled 5.35 1.86 5.74 7.8
Four-Seam 87.43 2396.21 209.23 5.90 11.72 BallCalled 5.39 1.68 5.50 7.0
Sinker 91.45 2422.42 229.02 13.81 13.15 StrikeSwinging 5.31 1.68 5.88 7.6
Four-Seam 92.85 2516.33 218.74 12.82 17.13 BallCalled 5.47 1.67 5.51 7.3
Sinker 92.06 2471.69 232.52 16.27 13.63 BallCalled 5.33 1.75 5.87 7.8
Sinker 91.59 2272.65 241.83 18.23 10.90 InPlay 5.33 1.70 5.96 8.1
Sinker 91.44 2179.50 249.29 20.43 8.89 BallCalled 5.42 1.78 5.54 8.3
Four-Seam 91.23 2442.60 220.20 12.15 15.53 InPlay 5.37 1.70 5.90 7.3
Sinker 91.68 2385.96 230.10 15.30 13.98 BallCalled 5.55 1.77 5.66 7.7
Sinker 92.01 2402.79 234.76 16.75 12.99 FoulBallNotFieldable 5.56 1.83 5.88 7.8
Slider 85.74 2356.34 188.78 1.49 10.80 FoulBallNotFieldable 5.52 1.61 5.27 6.3
Sinker 92.69 2229.33 247.28 19.21 9.13 BallCalled 5.52 1.63 5.80 8.2
Slider 85.92 2341.02 197.71 1.83 6.82 InPlay 5.53 1.59 5.25 6.6
Changeup 83.44 1344.50 273.86 17.31 0.13 BallCalled 5.51 1.87 5.28 9.1
Sinker 91.39 2470.73 240.56 18.30 11.48 FoulBallNotFieldable 5.50 1.85 5.35 8.0
Slider 85.44 2410.21 146.29 -4.53 8.08 BallCalled 5.45 1.87 5.05 4.9
Four-Seam 90.10 2196.99 221.58 10.58 13.11 BallCalled 5.46 1.78 5.12 7.4
Four-Seam 91.04 2501.33 208.08 8.37 16.76 FoulBallNotFieldable 5.53 1.88 5.38 6.9
Slider 84.92 2380.57 151.06 -3.14 6.88 StrikeCalled 5.41 1.91 5.17 5.0
Four-Seam 91.36 2436.80 217.47 10.54 14.81 BallCalled 5.32 1.91 5.99 7.2
Sinker 90.49 2274.44 236.40 16.84 12.36 InPlay 5.45 1.82 5.29 7.9
Four-Seam 91.44 2536.60 222.26 13.24 15.68 StrikeSwinging 5.38 1.79 5.43 7.4
Changeup 85.18 1696.06 278.84 17.26 -1.48 InPlay 5.40 1.83 5.29 9.3
Sinker 89.93 2212.84 227.83 14.22 14.11 StrikeSwinging 5.46 1.95 4.95 7.6
Four-Seam 91.08 2468.01 229.06 14.13 13.40 BallCalled 5.46 1.79 5.46 7.6
Changeup 84.76 1651.10 274.66 17.11 -0.08 BallCalled 5.46 1.87 5.25 9.2
Four-Seam 91.86 2487.68 216.95 8.86 12.88 FoulBallNotFieldable 5.52 1.83 5.47 7.2
Cutter 87.93 2454.18 192.16 2.39 12.29 InPlay 5.50 1.73 5.09 6.4
Sinker 90.47 2118.79 237.31 18.37 13.01 BallCalled 5.50 1.85 5.44 7.9
Sinker 91.15 2440.05 229.16 16.09 15.14 BallCalled 5.50 1.89 5.45 7.6
Sinker 89.53 2082.91 241.53 18.44 11.17 InPlay 5.53 1.86 5.67 8.1
Sinker 90.98 2372.63 230.28 16.38 14.76 InPlay 5.45 1.93 5.65 7.7
Sinker 90.24 2002.47 238.14 18.04 12.36 FoulBallNotFieldable 5.52 1.82 5.39 7.9
Slider 84.39 2321.25 189.61 1.30 8.81 FoulBallNotFieldable 5.55 1.88 5.22 6.3
Sinker 91.60 2258.35 239.71 14.78 9.74 InPlay 5.57 1.86 5.66 8.0
Sinker 91.73 2270.50 227.46 12.30 12.37 StrikeCalled 5.50 1.90 5.57 7.6
Four-Seam 91.25 2459.62 224.80 11.49 12.67 BallinDirt 5.48 1.94 5.40 7.5
Slider 84.92 2284.79 189.12 0.56 4.64 BallCalled 5.49 1.94 5.13 6.3
Four-Seam 91.40 2485.82 215.04 9.56 14.74 FoulBallNotFieldable 5.60 1.91 5.52 7.2
Changeup 85.83 1552.54 253.74 14.49 5.46 BallCalled 5.50 1.89 5.47 8.5
Sinker 91.90 2158.40 235.27 12.07 9.45 FoulBallNotFieldable 5.48 1.82 5.57 7.8
Sinker 93.18 2190.90 225.02 12.56 13.69 FoulBallNotFieldable 5.51 1.84 5.66 7.5
Slider 86.13 2242.05 186.52 0.87 8.88 StrikeSwinging 5.55 1.83 5.03 6.2
Slider 81.30 2253.83 81.08 -3.69 1.04 StrikeSwinging 5.59 1.77 5.16 2.7
Slider 83.21 2222.28 154.36 -1.47 4.46 BallCalled 5.58 1.81 5.20 5.1
Sinker 92.70 2434.29 233.51 16.60 13.36 BallCalled 5.48 1.75 6.06 7.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:  82
# 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 8.54% 7 5 2 71.43% 28.57% 85.08 1687.19 0.70 17.61 271.49 9.1 5.47 1.80 5.29
Cutter 3.66% 3 1 2 33.33% 66.67% 87.70 2469.54 11.35 1.23 187.68 6.2 5.45 1.74 5.12
Four-Seam 23.17% 19 7 12 36.84% 63.16% 91.49 2461.64 14.43 11.82 221.32 7.4 5.46 1.77 5.45
Sinker 42.68% 35 10 25 28.57% 71.43% 91.31 2285.89 11.99 16.77 236.92 7.9 5.46 1.80 5.54
Slider 21.95% 18 5 13 27.78% 72.22% 85.03 2369.20 6.66 -0.69 167.64 5.6 5.50 1.76 5.08
# 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:  93.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 for", unique(detailed_pitch_table$AutoPitchType)),
       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")
  )