# Filter the data for the pitcher Dowlen
Dowlen_data <- Adams619 %>%
  filter(Pitcher == 'Dowlen, Colin')

# Create a detailed table for each pitch
detailed_pitch_table <- Dowlen_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 Dowlen')
Detailed Pitch Table for Dowlen
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 89.32 2191.61 227.18 14.37 14.54 FoulBallNotFieldable 5.13 2.12 5.82 7.6
Curveball 75.74 3032.01 62.29 -21.68 -9.69 BallCalled 5.17 1.99 4.77 2.1
Sinker 88.50 2127.30 226.39 12.05 12.59 BallCalled 5.13 2.07 5.67 7.5
Sinker 88.33 2086.02 231.23 12.39 11.06 BallCalled 5.16 1.99 5.57 7.7
Sinker 88.87 2159.69 232.46 13.34 11.35 InPlay 5.03 1.94 6.07 7.7
Curveball 75.91 3073.58 51.06 -19.75 -14.20 BallCalled 5.25 1.89 4.85 1.7
Curveball 75.85 3021.41 46.61 -16.40 -13.88 StrikeSwinging 5.22 1.91 5.12 1.6
Four-Seam 90.07 2271.48 221.99 9.92 12.18 StrikeCalled 5.16 1.61 5.61 7.4
Curveball 75.87 3005.08 57.23 -18.54 -10.34 StrikeCalled 4.92 2.03 5.34 1.9
Four-Seam 88.54 2184.15 215.43 10.85 16.41 InPlay 5.16 1.90 5.79 7.2
Curveball 75.51 3054.94 50.36 -19.23 -14.10 StrikeCalled 5.28 1.78 4.79 1.7
Curveball 75.15 3086.96 47.01 -18.41 -15.35 FoulBallNotFieldable 5.25 1.83 4.62 1.6
Curveball 76.42 3116.70 47.46 -17.24 -14.11 BallCalled 5.28 1.80 4.91 1.6
Four-Seam 90.13 2272.09 215.66 8.96 13.58 BallCalled 5.20 1.72 5.81 7.2
Sinker 87.95 2124.54 225.97 10.32 11.14 BallCalled 5.07 1.71 5.79 7.5
Curveball 75.27 2916.20 46.20 -16.39 -13.93 BallCalled 5.22 1.75 4.86 1.5
Curveball 74.23 2916.80 54.01 -17.63 -10.91 StrikeCalled 5.18 1.79 4.85 1.8
Sinker 88.41 2058.71 224.68 10.09 11.42 InPlay 5.06 1.71 5.90 7.5
Four-Seam 89.34 2240.92 211.20 8.57 15.38 BallCalled 5.20 1.62 5.98 7.0
Curveball 75.85 3000.36 69.79 -20.66 -5.80 InPlay 5.29 1.82 5.11 2.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)
## Total number of pitches thrown: 20
# Display the summary table
knitr::kable(pitch_summary, caption = 'Summary Pitch Table for Dowlen')
Summary Pitch Table for Dowlen
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Curveball 50.00% 10 4 6 40.00% 60.00% 75.58 3022.40 -12.23 -18.59 53.20 1.8 5.21 1.86 4.92
Four-Seam 20.00% 4 2 2 50.00% 50.00% 89.52 2242.16 14.39 9.57 216.07 7.2 5.18 1.71 5.80
Sinker 30.00% 6 3 3 50.00% 50.00% 88.56 2124.64 12.02 12.09 227.99 7.6 5.10 1.92 5.80
# 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('Dowlen maximum FB velocity:', max_fb_velocity)
## Dowlen maximum FB velocity: 90.13
# Prepare data for plotting pitch locations
pitch_location_data <- Dowlen_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 Dowlen',
       x = 'Horizontal Location (feet)',
       y = 'Vertical Location (feet)',
       color = 'Swing/Take',
       shape = 'Chase') +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each 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')
  )

# 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 Dowlen',
       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')
  )