# Filter the data for the pitcher Dowlen
Dowlen_data <- Danbury611 %>%
  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
Changeup 85.87 1982.79 238.09 16.50 11.65 StrikeCalled 5.31 1.73 6.68 7.9
Curveball 77.97 1914.45 82.18 -12.34 -0.09 StrikeCalled 5.53 1.74 6.22 2.7
Four-Seam 87.27 2210.56 204.98 9.30 21.33 BallCalled 5.56 1.59 6.70 6.8
Changeup 83.53 1709.75 260.90 17.06 4.19 BallCalled 5.25 2.17 6.68 8.7
Curveball 79.33 1995.69 48.93 -6.53 -4.06 BallCalled 5.50 1.65 6.49 1.6
Sinker 88.16 2120.06 237.13 18.70 13.40 FoulBall 5.29 1.94 6.87 7.9
Sinker 86.55 2054.61 229.46 17.11 16.11 BallCalled 5.30 1.95 6.71 7.6
Curveball 75.78 1739.04 71.41 -13.03 -2.66 FoulBall 5.56 1.41 6.46 2.4
Changeup 86.53 2045.16 244.01 17.63 9.92 StrikeSwinging 5.25 1.91 6.82 8.1
Four-Seam 88.16 2186.38 208.98 10.03 19.38 BallCalled 5.51 1.64 6.79 7.0
Curveball 78.28 1947.51 44.38 -6.73 -5.34 BallCalled 5.60 1.36 6.58 1.5
Slider 79.91 1877.30 139.16 -4.58 7.14 InPlay 5.45 1.72 6.77 4.6
Curveball 76.94 1864.33 74.01 -13.21 -2.06 StrikeCalled 5.66 1.45 6.30 2.5
Changeup 86.56 2009.84 243.51 17.56 10.05 StrikeSwinging 5.34 1.70 6.86 8.1
Slider 79.90 2077.52 161.97 -3.29 11.89 BallCalled 5.34 2.05 6.41 5.4
Sinker 87.55 1994.96 229.79 15.99 14.77 FoulBall 5.34 1.70 6.89 7.7
Changeup 86.24 1849.75 234.09 15.41 12.35 FoulBall 5.41 1.79 7.02 7.8
Slider 80.81 2129.08 178.53 -0.27 12.23 InPlay 5.49 2.02 6.57 6.0
Changeup 86.97 2039.30 245.70 20.87 10.80 HitByPitch 5.31 1.97 6.58 8.2
Four-Seam 89.82 2165.48 218.76 15.78 21.15 BallCalled 4.82 1.78 6.44 7.3
Four-Seam 88.98 2154.43 219.55 15.94 20.74 InPlay 4.89 1.74 6.67 7.3
Sinker 89.84 2142.33 220.41 15.16 19.21 BallCalled 4.80 1.85 6.23 7.3
Curveball 75.76 3122.01 43.60 -13.86 -12.30 StrikeSwinging 4.94 1.89 6.08 1.5
Curveball 76.86 3065.44 54.45 -19.02 -11.37 StrikeSwinging 4.86 1.70 5.86 1.8
Four-Seam 90.23 2215.39 215.04 14.32 21.91 StrikeCalled 4.94 1.69 5.99 7.2
Curveball 76.30 3151.09 42.83 -16.08 -15.01 BallinDirt 4.93 1.80 5.62 1.4
Four-Seam 88.73 2159.81 208.39 11.62 22.95 StrikeCalled 4.90 1.70 6.38 6.9
Curveball 75.73 2948.13 51.57 -18.68 -12.54 BallinDirt 4.89 1.77 5.99 1.7
Four-Seam 88.65 2111.58 212.84 12.00 20.09 StrikeCalled 4.95 1.73 6.35 7.1
Four-Seam 89.25 2226.18 212.26 12.68 21.58 InPlay 4.91 1.80 6.41 7.1
Curveball 76.81 3137.06 69.23 -19.41 -5.26 BallinDirt 4.95 1.74 6.28 2.3
Four-Seam 88.82 2221.50 216.31 12.84 18.87 StrikeCalled 4.83 1.82 6.59 7.2
Four-Seam 89.18 2199.78 213.01 12.86 21.20 StrikeSwinging 4.81 1.82 6.73 7.1
Curveball 77.59 3129.38 43.46 -14.61 -13.38 BallinDirt 4.88 1.90 6.18 1.4
Four-Seam 89.68 2187.90 201.27 8.74 23.86 FoulBall 4.93 1.73 6.09 6.7
Curveball 75.28 3050.66 70.65 -20.53 -5.12 BallCalled 5.07 1.76 5.98 2.4
Sinker 89.75 2146.59 224.06 16.21 18.07 InPlay 4.93 1.68 6.29 7.5
# 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:  37
# 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
Changeup 16.22% 6 1 5 16.67% 83.33% 85.95 1939.43 9.83 17.50 244.38 8.1 5.31 1.88 6.77
Curveball 32.43% 12 3 9 25.00% 75.00% 76.89 2588.73 -7.43 -14.50 58.06 1.9 5.20 1.68 6.17
Four-Seam 29.73% 11 3 8 27.27% 72.73% 88.98 2185.36 21.19 12.37 211.94 7.1 5.00 1.73 6.47
Sinker 13.51% 5 2 3 40.00% 60.00% 88.37 2091.71 16.31 16.63 228.17 7.6 5.13 1.82 6.60
Slider 8.11% 3 1 2 33.33% 66.67% 80.21 2027.97 10.42 -2.71 159.89 5.3 5.43 1.93 6.58
# 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, "mph\n")
## Dowlen maximum FB velocity:  90.23 mph
# 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
  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 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")
  )