# Filter the data for the pitcher Falter
Falter_data <- Danbury618 %>%
  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 91.77 2453.62 231.25 15.03 13.18 StrikeCalled 5.28 1.59 6.30 7.7
Four-Seam 93.58 2528.50 232.83 17.07 14.09 FoulBall 5.37 1.50 6.19 7.8
Four-Seam 93.14 2519.11 225.85 13.27 13.98 InPlay 5.38 1.51 5.94 7.5
Four-Seam 92.58 2523.35 220.44 11.98 15.18 FoulBall 5.30 1.60 6.32 7.3
Sinker 92.27 2380.21 237.60 17.70 12.36 FoulBall 5.37 1.70 6.00 7.9
Cutter 87.23 2421.90 180.96 0.21 13.87 InPlay 5.37 1.59 6.02 6.0
Sinker 91.57 2401.67 247.36 20.17 9.68 BallCalled 5.51 1.50 6.53 8.2
Sinker 92.01 2417.19 244.67 22.30 11.86 StrikeCalled 5.73 1.55 6.34 8.2
Four-Seam 92.22 2584.60 217.30 13.44 18.95 StrikeSwinging 5.57 1.46 5.97 7.2
Cutter 87.74 2477.64 177.47 -0.70 17.21 StrikeSwinging 5.48 1.57 5.88 5.9
Cutter 87.00 2450.75 178.07 -0.53 17.09 StrikeCalled 5.38 1.53 5.76 5.9
Four-Seam 88.12 2486.85 196.95 3.92 14.08 BallCalled 5.50 1.51 5.93 6.6
Four-Seam 92.80 2545.78 220.59 14.47 18.22 FoulBall 5.46 1.67 6.08 7.4
Four-Seam 88.33 2420.18 184.28 0.99 14.28 FoulBall 5.46 1.46 6.17 6.1
Four-Seam 92.95 2562.94 219.36 14.63 18.95 StrikeSwinging 5.41 1.56 6.08 7.3
Slider 86.12 2478.23 187.81 1.48 12.13 BallCalled 5.22 1.56 5.82 6.3
Four-Seam 92.74 2569.98 225.43 15.66 16.63 BallCalled 5.23 1.54 6.60 7.5
Four-Seam 92.50 2558.75 223.64 14.44 16.32 InPlay 5.26 1.61 6.51 7.5
Four-Seam 92.26 2512.66 227.38 14.33 14.38 StrikeSwinging 5.25 1.60 6.40 7.6
Sinker 91.55 2345.83 234.85 19.23 14.74 InPlay 5.28 1.50 6.74 7.8
Sinker 92.57 2247.30 230.85 18.45 16.29 StrikeCalled 5.21 1.52 6.08 7.7
Four-Seam 93.53 2585.04 222.03 16.00 19.05 FoulBall 5.19 1.57 5.90 7.4
Changeup 86.06 1813.07 266.95 20.01 2.39 BallCalled 5.20 1.56 6.05 8.9
Sinker 93.40 2408.91 238.38 18.78 12.73 BallCalled 5.23 1.72 6.20 7.9
Four-Seam 94.00 2639.11 209.92 12.00 22.00 FoulBall 5.24 1.60 6.22 7.0
Four-Seam 88.98 2537.82 191.77 2.71 14.06 StrikeSwinging 5.22 1.55 6.38 6.4
Sinker 90.32 2527.60 234.58 20.36 15.78 StrikeCalled 5.52 1.60 6.22 7.8
Sinker 92.02 2548.03 241.04 20.71 12.72 StrikeCalled 5.53 1.47 6.50 8.0
Four-Seam 89.80 2573.31 209.41 6.82 13.31 InPlay 5.75 1.47 5.60 7.0
Sinker 93.09 2574.60 237.89 21.78 14.87 HitByPitch 5.16 1.58 6.50 7.9
Four-Seam 91.48 2335.94 223.73 15.16 17.04 BallCalled 5.29 1.50 6.39 7.5
Sinker 91.85 2478.92 230.27 16.74 15.22 InPlay 5.25 1.49 6.26 7.7
Sinker 91.20 2299.62 230.65 17.61 15.72 InPlay 5.23 1.50 6.34 7.7
Four-Seam 93.01 2513.84 234.28 20.54 16.01 InPlay 5.27 1.34 6.24 7.8
Slider 85.01 2311.45 204.35 5.27 12.82 BallCalled 5.33 1.48 6.08 6.8
Four-Seam 87.09 2404.68 204.37 5.92 14.22 StrikeCalled 5.31 1.50 5.95 6.8
Four-Seam 87.86 2380.97 214.13 8.65 13.89 BallCalled 5.26 1.44 6.15 7.1
Four-Seam 92.56 2583.42 228.07 17.88 17.22 FoulBall 5.30 1.41 6.29 7.6
Cutter 87.31 2410.53 206.37 4.84 10.77 BallCalled 5.22 1.41 6.06 6.9
Slider 81.95 2316.83 186.25 0.50 5.76 FoulBall 5.30 1.51 5.52 6.2
Sinker 92.42 2384.74 240.82 18.73 11.67 InPlay 5.30 1.43 6.59 8.0
Slider 85.37 2373.30 184.34 0.78 11.53 StrikeCalled 5.37 1.55 6.06 6.1
Four-Seam 92.06 2587.14 228.03 15.62 15.34 InPlay 5.30 1.50 5.92 7.6
Four-Seam 91.73 2505.21 214.25 11.38 17.87 BallCalled 5.23 1.62 6.16 7.1
Slider 85.43 2389.05 167.04 -1.82 9.13 BallCalled 5.19 1.61 6.18 5.6
Four-Seam 92.28 2506.66 217.37 11.54 16.30 BallCalled 5.05 1.54 5.95 7.2
Four-Seam 91.32 2532.09 204.79 7.35 17.02 InPlay 5.14 1.48 6.18 6.8
Cutter 86.81 2420.73 197.91 3.73 12.72 BallCalled 5.23 1.68 6.05 6.6
Four-Seam 93.44 2568.17 229.48 17.43 16.05 FoulBall 5.18 1.51 6.54 7.6
Changeup 85.95 1665.74 262.20 17.82 3.64 BallinDirt 5.15 1.58 6.09 8.7
Sinker 91.80 2407.36 229.64 15.22 14.03 InPlay 5.09 1.60 6.11 7.7
Four-Seam 92.07 2610.76 212.36 11.53 19.30 StrikeCalled 5.24 1.48 6.35 7.1
Slider 87.16 2428.00 189.48 1.72 11.46 InPlay 5.32 1.36 6.04 6.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:  53 \n\n
# 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 3.77% 2 1 1 50.00% 50.00% 86.00 1739.40 3.02 18.91 264.58 8.8 5.18 1.57 6.07
Cutter 9.43% 5 2 3 40.00% 60.00% 87.22 2436.31 14.33 1.51 188.16 6.3 5.34 1.56 5.95
Four-Seam 49.06% 26 6 20 23.08% 76.92% 91.63 2526.03 16.30 12.11 216.85 7.2 5.31 1.52 6.17
Sinker 26.42% 14 2 12 14.29% 85.71% 91.99 2419.69 13.63 18.77 236.42 7.9 5.34 1.55 6.34
Slider 11.32% 6 3 3 50.00% 50.00% 85.17 2382.81 10.47 1.32 186.54 6.2 5.29 1.51 5.95
# 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, '\\n')
## Falter maximum FB velocity:  94 \n
# 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')
  )