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

# Create a detailed table for each pitch
detailed_pitch_table <- Falter_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 Falter")
Detailed Pitch Table for Falter
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 89.90 2098.46 242.52 16.71 9.94 StrikeSwinging 5.34 1.72 6.20 8.1
Four-Seam 91.74 2441.34 220.24 12.11 15.41 StrikeCalled 5.39 1.70 6.30 7.3
Sinker 91.74 2300.81 242.23 18.30 10.82 FoulBall 5.35 1.82 6.17 8.1
Changeup 85.82 2095.82 255.85 18.55 5.96 BallCalled 5.40 1.70 6.24 8.5
Four-Seam 93.23 2459.30 224.25 13.64 15.21 BallCalled 5.43 1.86 5.91 7.5
Cutter 88.61 2480.09 189.36 1.24 8.62 InPlay 5.42 1.75 6.18 6.3
Four-Seam 91.55 2500.22 219.34 11.28 14.88 FoulBall 5.39 1.60 6.32 7.3
Sinker 91.26 2309.39 239.44 13.94 9.38 FoulBall 5.40 1.59 6.36 8.0
Cutter 88.71 2487.09 174.67 -1.02 12.05 StrikeSwinging 5.30 1.58 5.70 5.8
Four-Seam 93.23 2509.00 217.91 12.43 17.04 StrikeSwinging 5.35 1.55 6.38 7.3
Slider 84.19 2439.61 196.38 1.29 5.75 StrikeCalled 5.36 1.70 5.72 6.5
Four-Seam 89.72 2516.53 196.72 3.62 13.11 StrikeCalled 5.35 1.65 5.69 6.6
Four-Seam 90.97 2453.32 219.17 12.73 16.80 InPlay 5.27 1.55 6.23 7.3
Four-Seam 92.64 2480.93 220.23 12.51 15.87 StrikeCalled 5.27 1.55 6.32 7.3
Four-Seam 88.49 2468.37 197.81 5.58 18.44 StrikeSwinging 5.29 1.72 6.24 6.6
Cutter 86.90 2456.19 175.73 -0.80 11.88 BallCalled 5.36 1.59 5.74 5.9
Four-Seam 93.71 2567.82 214.36 12.18 18.88 StrikeSwinging 5.41 1.50 6.29 7.1
Four-Seam 93.93 2632.58 224.57 14.02 15.35 StrikeCalled 5.32 1.50 6.30 7.5
Cutter 88.23 2467.52 184.36 1.16 16.29 InPlay 5.26 1.76 5.69 6.1
Four-Seam 90.48 2386.81 220.14 11.57 14.79 StrikeCalled 5.22 1.70 5.98 7.3
Sinker 90.90 2264.54 243.81 16.19 9.05 InPlay 5.31 1.68 6.17 8.1
Four-Seam 92.24 2491.57 211.92 9.20 15.83 FoulBall 5.34 1.64 6.15 7.1
Slider 86.61 2479.62 187.82 1.29 10.56 StrikeCalled 5.33 1.50 5.89 6.3
Slider 83.32 2418.55 177.03 -0.43 9.57 StrikeCalled 5.35 1.51 5.56 5.9
Sinker 92.25 2303.08 231.13 15.92 14.05 StrikeCalled 5.38 1.57 6.11 7.7
Sinker 94.16 2362.98 229.04 15.20 14.34 StrikeSwinging 5.37 1.60 6.02 7.6
Four-Seam 93.63 2566.94 218.93 12.99 17.25 FoulBall 5.30 1.54 6.06 7.3
Changeup 86.06 1867.60 252.34 14.12 5.62 FoulBall 5.23 1.56 6.13 8.4
Changeup 86.11 1955.82 263.67 17.62 3.18 StrikeSwinging 5.27 1.61 6.07 8.8
Sinker 92.70 2154.76 235.24 15.66 11.97 FoulBall 5.27 1.69 6.18 7.8
Sinker 91.33 2248.50 239.78 15.17 10.02 FoulBall 5.32 1.61 6.25 8.0
Four-Seam 94.79 2548.78 216.15 12.04 17.65 StrikeCalled 5.35 1.62 5.93 7.2
Slider 83.10 2425.29 176.01 -0.49 8.24 StrikeCalled 5.35 1.54 5.60 5.9
Cutter 89.58 2580.28 173.71 -1.14 11.42 BallCalled 5.39 1.57 5.59 5.8
Four-Seam 93.93 2580.51 215.13 12.24 18.60 StrikeSwinging 5.27 1.58 6.25 7.2
Slider 82.58 2367.66 154.88 -4.09 10.05 StrikeCalled 5.32 1.42 5.93 5.2
Slider 83.41 2496.47 167.16 -1.43 7.44 BallCalled 5.35 1.43 5.98 5.6
Cutter 86.70 2508.26 182.92 0.55 11.76 StrikeSwinging 5.22 1.77 5.70 6.1
Slider 83.68 2427.89 177.91 -0.27 8.60 BallCalled 5.25 1.58 5.69 5.9
Four-Seam 93.03 2534.79 211.83 10.50 18.03 BallCalled 5.21 1.48 6.59 7.1
Four-Seam 93.97 2622.07 212.17 11.75 19.72 BallCalled 5.28 1.53 6.29 7.1
Four-Seam 93.64 2564.00 211.58 10.97 18.83 BallCalled 5.48 1.38 6.69 7.1
Slider 82.99 2443.55 202.11 1.89 6.08 BallCalled 5.26 1.43 6.02 6.7
Four-Seam 94.20 2549.23 215.96 12.36 18.06 StrikeCalled 5.25 1.50 6.27 7.2
Cutter 87.45 2514.06 165.86 -2.31 10.13 BallCalled 5.37 1.37 5.78 5.5
Sinker 92.17 2379.22 225.92 15.93 16.47 StrikeCalled 5.23 1.53 6.51 7.5
Four-Seam 94.02 2589.48 222.24 14.87 17.41 FoulBall 5.38 1.50 6.76 7.4
Sinker 93.41 2368.76 233.15 16.28 13.26 InPlay 5.32 1.54 6.18 7.8
Four-Seam 92.93 2420.94 212.97 10.64 17.45 StrikeCalled 5.41 1.45 6.38 7.1
Slider 83.66 2378.81 163.67 -1.17 5.28 StrikeSwinging 5.52 1.39 5.67 5.5
Four-Seam 93.55 2597.66 212.33 11.45 19.15 StrikeSwinging 5.51 1.38 6.31 7.1
Slider 81.53 2354.09 109.93 -6.43 3.74 BallCalled 5.30 1.62 5.45 3.7
Four-Seam 92.33 2536.78 218.25 14.08 18.96 StrikeSwinging 5.31 1.58 5.98 7.3
Cutter 86.95 2484.31 167.33 -2.18 10.75 BallCalled 5.33 1.56 5.70 5.6
Four-Seam 93.48 2565.96 221.24 12.65 15.45 BallCalled 5.13 1.62 6.26 7.4
Four-Seam 93.57 2582.20 213.86 11.38 18.00 StrikeSwinging 5.22 1.60 6.43 7.1
Four-Seam 92.33 2381.20 225.04 14.65 15.88 BallCalled 5.16 1.58 6.23 7.5
Four-Seam 92.31 2537.97 222.11 13.74 16.29 StrikeSwinging 5.41 1.54 6.20 7.4
Four-Seam 93.82 2555.80 216.46 11.84 17.10 InPlay 5.35 1.50 6.11 7.2
Sinker 90.39 2396.94 226.20 15.07 15.79 FoulBall 5.52 1.47 6.24 7.5
Cutter 86.58 2431.87 180.84 0.17 13.20 BallCalled 5.31 1.57 5.82 6.0
Four-Seam 92.64 2533.00 217.42 12.45 17.41 InPlay 5.39 1.60 6.13 7.2
Cutter 87.25 2409.82 192.33 2.44 12.25 BallCalled 5.40 1.63 5.95 6.4
Slider 86.05 2396.39 189.66 1.27 8.63 BallCalled 5.38 1.61 5.63 6.3
Four-Seam 91.73 2494.60 211.41 10.78 18.88 FoulBall 5.29 1.58 6.06 7.0
Slider 85.16 2386.45 163.59 -3.52 13.30 FoulBall 5.38 1.62 5.88 5.5
Sinker 91.39 2338.41 231.23 14.70 13.01 FoulBall 5.31 1.58 5.85 7.7
Changeup 85.22 1857.76 250.41 16.81 7.25 BallCalled 5.28 1.61 6.30 8.3
Four-Seam 91.93 2520.72 220.38 12.96 16.40 InPlay 5.30 1.54 6.26 7.3
# 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\n")
## Total number of pitches thrown:  69
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Falter")
Summary Pitch Table for Falter
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 5.80% 4 2 2 50.00% 50.00% 85.80 1944.25 5.50 16.77 255.57 8.5 5.30 1.62 6.19
Cutter 14.49% 10 6 4 60.00% 40.00% 87.70 2481.95 11.84 -0.19 178.71 6.0 5.34 1.61 5.78
Four-Seam 44.93% 31 6 25 19.35% 80.65% 92.70 2522.27 17.04 11.78 216.20 7.2 5.32 1.57 6.24
Sinker 17.39% 12 0 12 0.00% 100.00% 91.80 2293.82 12.34 15.76 234.97 7.8 5.34 1.62 6.19
Slider 17.39% 12 5 7 41.67% 58.33% 83.86 2417.86 8.10 -1.01 172.18 5.8 5.35 1.53 5.75
# 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("Falter maximum FB velocity: ", max_fb_velocity, "mph\n")
## Falter maximum FB velocity:  94.79 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Falter_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) + # Increase point size
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + # Red box
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + # Black box
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) + # Gray box
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) + # Adjust ratio to shrink vertical distance
  labs(title = "Pitch Locations for Falter",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each pitch type
  geom_text(aes(x = -1.75, y = 5, label = "RHH"), color = "black", size = 3, hjust = 0) + # Label for RHH
  geom_text(aes(x = 1.75, y = 5, label = "LHH"), color = "black", size = 3, hjust = 1) + # Label for LHH
  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
  labs(title = "Pitch Movement for Falter",
       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")
  )