# Filter the data for the pitcher Ryan
Ryan_data <- Valley68 %>%
  filter(Pitcher == "Ryan, Dillon")

# Create a detailed table for each pitch
detailed_pitch_table <- Ryan_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 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 Ryan")
Detailed Pitch Table for Ryan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ClockTilt
Sinker 88.40 2300.13 238.33 21.11 14.20 StrikeCalled 7.9
Sinker 88.50 2332.28 244.09 19.67 10.73 BallCalled 8.1
Sinker 88.29 2203.41 244.89 22.94 12.04 BallCalled 8.2
Sinker 88.70 2312.58 248.16 20.02 9.22 BallCalled 8.3
Sinker 88.34 2205.32 245.18 19.34 10.15 FoulBallNotFieldable 8.2
Sinker 87.82 2266.50 245.09 21.34 11.12 FoulBallNotFieldable 8.2
Sinker 88.77 2227.81 246.69 20.57 10.03 StrikeCalled 8.2
Curveball 80.48 2308.30 41.03 -7.93 -7.75 HitByPitch 1.4
Sinker 87.88 2282.73 239.67 20.95 13.50 StrikeCalled 8.0
Curveball 81.75 2455.06 43.89 -8.78 -7.95 StrikeCalled 1.5
Curveball 82.23 2305.49 39.72 -8.03 -8.49 StrikeSwinging 1.3
Curveball 81.10 2354.41 28.95 -5.15 -8.12 InPlay 1.0
Sinker 87.68 2329.39 248.57 23.18 10.43 InPlay 8.3
Sinker 88.29 2362.96 243.45 23.01 12.80 BallCalled 8.1
Changeup 87.17 2328.84 241.21 20.84 12.67 StrikeCalled 8.0
Curveball 81.82 2350.99 31.06 -5.33 -7.68 InPlay 1.0
Sinker 87.76 2362.79 242.89 24.12 13.61 BallCalled 8.1
Sinker 87.52 2371.30 241.02 22.25 13.61 StrikeCalled 8.0
Curveball 81.87 2289.23 28.55 -4.17 -6.19 StrikeSwinging 1.0
Slider 82.52 2360.33 6.34 -0.46 -2.83 BallCalled 0.2
Sinker 87.96 2281.14 249.86 23.91 9.96 BallCalled 8.3
Sinker 87.57 2321.44 249.35 24.33 10.38 StrikeCalled 8.3
Sinker 86.80 2322.66 229.62 17.85 16.29 HitByPitch 7.7
Sinker 86.86 2220.62 239.35 19.45 12.72 StrikeCalled 8.0
Changeup 86.07 2275.84 245.75 22.50 11.35 BallCalled 8.2
Changeup 87.00 2281.72 245.32 20.58 10.60 BallCalled 8.2
Changeup 85.81 2204.78 239.15 20.67 13.59 BallCalled 8.0
Changeup 86.34 2233.66 238.93 20.37 13.53 StrikeSwinging 8.0
Sinker 87.11 2240.76 242.87 21.42 12.23 StrikeSwinging 8.1
Slider 80.04 2297.37 68.35 -8.40 -1.84 BallCalled 2.3
Sinker 86.93 2186.26 246.10 20.43 10.21 FoulBallNotFieldable 8.2
Sinker 87.14 2175.17 241.75 20.33 12.11 InPlay 8.1
Changeup 86.44 2215.17 236.83 19.95 14.35 StrikeCalled 7.9
Curveball 80.52 2364.76 14.36 -1.17 -3.17 BallCalled 0.5
Curveball 79.98 2273.76 20.32 -2.88 -6.29 BallCalled 0.7
Changeup 86.68 2308.99 244.59 21.02 11.15 StrikeCalled 8.2
Changeup 86.79 2359.29 244.52 20.99 11.17 FoulBallNotFieldable 8.2
Slider 82.54 2226.97 347.34 1.24 -4.08 FoulBallNotFieldable 11.6
Slider 81.22 2217.28 334.49 1.44 -1.73 InPlay 11.1
Changeup 86.42 2254.64 242.65 19.89 11.60 BallCalled 8.1
Changeup 86.49 2286.90 243.32 21.64 12.22 FoulBallNotFieldable 8.1
Curveball 81.33 2259.14 356.19 0.61 -7.75 BallCalled 11.9
Changeup 86.93 2274.96 244.91 20.47 10.76 StrikeCalled 8.2
Curveball 81.78 2263.36 13.57 -1.72 -5.82 BallCalled 0.5
Changeup 86.58 2259.34 233.06 18.93 15.43 BallCalled 7.8
Changeup 86.33 2259.40 240.26 19.61 12.35 StrikeCalled 8.0
Curveball 80.76 2398.09 8.40 -0.48 -1.79 FoulBallNotFieldable 0.3
Curveball 80.40 2349.02 27.07 -3.69 -5.57 BallCalled 0.9
Changeup 86.96 2219.61 242.22 17.49 10.45 BallCalled 8.1
Curveball 79.59 2325.00 6.77 -1.17 -7.99 BallCalled 0.2
Sinker 86.87 2207.01 236.98 20.54 14.65 StrikeCalled 7.9
Curveball 80.95 2210.92 0.53 -0.08 -7.36 InPlay 0.0
Changeup 86.47 2355.86 240.64 21.77 13.60 InPlay 8.0
# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    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
  )

# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Ryan")
Summary Pitch Table for Ryan
AutoPitchType TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt
Changeup 15 6 9 40.00% 60.00% 86.57 2274.60 12.32 20.45 241.56 8.1
Curveball 14 6 8 42.86% 57.14% 81.04 2321.97 -6.57 -3.57 47.17 1.6
Sinker 20 6 14 30.00% 70.00% 87.76 2275.61 12.00 21.34 243.20 8.1
Slider 4 2 2 50.00% 50.00% 81.58 2275.49 -2.62 -1.55 189.13 6.3
# Prepare data for plotting pitch locations
pitch_location_data <- Ryan_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 Ryan",
       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 Ryan",
       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")
  )