knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(readxl)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(knitr)
# Load the data
Upper626 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Upper 626/Upper 626.xlsx")
# Filter the data for the pitcher Ben Alekson
Ben_Alekson_data <- Upper626 %>%
filter(Pitcher == "Alekson, Ben")
# Create a detailed table for each pitch
detailed_pitch_table <- Ben_Alekson_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 Ben Alekson")
Detailed Pitch Table for Ben Alekson
Four-Seam |
90.27 |
2159.14 |
219.48 |
17.89 |
23.14 |
FoulBallNotFieldable |
5.89 |
0.91 |
4.99 |
7.3 |
Four-Seam |
90.11 |
2131.88 |
211.39 |
11.60 |
20.15 |
FoulBallNotFieldable |
6.00 |
0.90 |
5.00 |
7.0 |
Four-Seam |
90.19 |
2193.26 |
215.46 |
12.78 |
19.01 |
BallCalled |
6.00 |
0.98 |
4.87 |
7.2 |
Four-Seam |
90.65 |
2218.46 |
214.39 |
12.87 |
19.90 |
FoulBallNotFieldable |
6.01 |
0.90 |
4.86 |
7.1 |
Changeup |
83.04 |
1570.97 |
242.52 |
12.82 |
7.88 |
InPlay |
5.94 |
1.11 |
5.01 |
8.1 |
Four-Seam |
90.01 |
2137.48 |
211.67 |
10.76 |
18.50 |
StrikeCalled |
5.89 |
1.04 |
5.04 |
7.1 |
Sinker |
90.51 |
2172.64 |
223.51 |
15.96 |
17.90 |
BallCalled |
5.83 |
1.25 |
4.98 |
7.5 |
Sinker |
90.41 |
2099.89 |
222.34 |
16.16 |
18.83 |
StrikeSwinging |
5.89 |
0.95 |
4.99 |
7.4 |
Four-Seam |
90.40 |
2163.38 |
206.67 |
9.37 |
19.77 |
FoulBallNotFieldable |
5.78 |
1.23 |
4.80 |
6.9 |
Four-Seam |
90.50 |
2232.98 |
219.76 |
18.35 |
23.51 |
StrikeSwinging |
5.81 |
1.15 |
4.96 |
7.3 |
Four-Seam |
91.08 |
2168.13 |
214.38 |
15.56 |
24.10 |
StrikeSwinging |
5.95 |
0.98 |
5.13 |
7.1 |
Four-Seam |
90.15 |
2159.48 |
216.97 |
16.69 |
23.59 |
InPlay |
5.89 |
1.03 |
4.87 |
7.2 |
Four-Seam |
90.85 |
2169.31 |
219.76 |
17.73 |
22.69 |
StrikeCalled |
5.99 |
0.98 |
4.98 |
7.3 |
Four-Seam |
90.90 |
2181.63 |
219.20 |
15.88 |
20.84 |
StrikeSwinging |
5.91 |
0.96 |
4.91 |
7.3 |
Four-Seam |
90.74 |
2198.56 |
221.13 |
18.33 |
22.45 |
FoulBallNotFieldable |
5.95 |
0.96 |
4.90 |
7.4 |
Sinker |
91.17 |
2163.71 |
225.14 |
14.84 |
15.85 |
StrikeCalled |
5.86 |
0.94 |
5.17 |
7.5 |
Changeup |
81.92 |
1602.35 |
244.24 |
15.35 |
8.79 |
FoulBallNotFieldable |
5.80 |
1.19 |
5.12 |
8.1 |
Four-Seam |
90.25 |
2255.89 |
220.71 |
15.01 |
18.79 |
BallCalled |
5.91 |
1.02 |
4.91 |
7.4 |
Changeup |
82.04 |
1650.05 |
249.85 |
16.59 |
7.45 |
BallCalled |
5.86 |
1.22 |
5.01 |
8.3 |
Four-Seam |
89.87 |
2136.06 |
218.73 |
14.35 |
19.27 |
StrikeCalled |
6.03 |
0.92 |
5.03 |
7.3 |
Four-Seam |
90.84 |
2155.90 |
222.34 |
15.59 |
18.48 |
StrikeSwinging |
6.00 |
0.97 |
4.99 |
7.4 |
Changeup |
82.89 |
1775.30 |
242.18 |
17.27 |
10.55 |
StrikeCalled |
5.96 |
1.07 |
5.02 |
8.1 |
Changeup |
81.61 |
1670.01 |
244.42 |
17.48 |
9.84 |
BallCalled |
5.92 |
1.10 |
5.05 |
8.1 |
Changeup |
83.18 |
1811.77 |
244.85 |
17.84 |
9.78 |
InPlay |
5.85 |
1.06 |
5.17 |
8.2 |
Changeup |
82.18 |
1773.53 |
240.84 |
14.24 |
9.34 |
FoulBallNotFieldable |
5.95 |
1.02 |
5.11 |
8.0 |
Four-Seam |
91.70 |
2185.02 |
217.96 |
13.35 |
18.22 |
BallCalled |
5.87 |
1.06 |
5.14 |
7.3 |
Changeup |
82.92 |
1821.05 |
236.01 |
14.48 |
11.09 |
FoulBallNotFieldable |
5.93 |
1.11 |
4.99 |
7.9 |
Four-Seam |
91.83 |
2259.59 |
225.05 |
14.70 |
15.76 |
StrikeSwinging |
5.96 |
0.91 |
5.21 |
7.5 |
Sinker |
89.25 |
2094.80 |
230.45 |
16.26 |
14.71 |
InPlay |
5.98 |
0.88 |
5.22 |
7.7 |
Slider |
80.64 |
1947.42 |
194.83 |
0.48 |
3.30 |
BallCalled |
5.90 |
0.97 |
5.09 |
6.5 |
Four-Seam |
89.60 |
2132.53 |
219.51 |
13.58 |
17.72 |
StrikeCalled |
5.90 |
0.97 |
5.31 |
7.3 |
Four-Seam |
90.98 |
2254.82 |
213.54 |
12.99 |
20.83 |
FoulBallNotFieldable |
5.88 |
0.95 |
5.19 |
7.1 |
Changeup |
82.33 |
1431.33 |
249.44 |
12.82 |
6.09 |
FoulBallNotFieldable |
5.98 |
0.96 |
5.05 |
8.3 |
Four-Seam |
90.00 |
2173.22 |
218.85 |
12.94 |
17.29 |
InPlay |
5.97 |
0.77 |
4.96 |
7.3 |
Four-Seam |
91.81 |
2260.38 |
220.65 |
14.24 |
17.79 |
FoulBallNotFieldable |
5.86 |
0.97 |
5.12 |
7.4 |
Slider |
83.71 |
2044.52 |
137.57 |
-4.08 |
5.64 |
BallCalled |
5.91 |
0.85 |
5.10 |
4.6 |
Sinker |
90.58 |
2161.14 |
226.52 |
15.55 |
15.92 |
FoulBallNotFieldable |
5.97 |
0.81 |
5.11 |
7.6 |
Slider |
84.57 |
2071.16 |
175.70 |
-0.49 |
7.69 |
BallCalled |
5.93 |
0.88 |
4.98 |
5.9 |
Four-Seam |
92.13 |
2232.79 |
204.86 |
8.46 |
19.35 |
StrikeSwinging |
5.99 |
0.81 |
5.18 |
6.8 |
Four-Seam |
90.01 |
2141.74 |
220.78 |
15.23 |
18.87 |
BallCalled |
5.81 |
1.10 |
5.12 |
7.4 |
Four-Seam |
90.04 |
2100.55 |
217.29 |
13.83 |
19.35 |
StrikeCalled |
5.82 |
1.00 |
5.13 |
7.2 |
Four-Seam |
90.65 |
2167.07 |
218.29 |
14.61 |
19.66 |
FoulBallNotFieldable |
5.86 |
1.08 |
5.03 |
7.3 |
Four-Seam |
91.56 |
2209.63 |
215.80 |
11.99 |
17.70 |
BallCalled |
5.88 |
1.12 |
5.21 |
7.2 |
Changeup |
84.10 |
1694.70 |
242.34 |
13.89 |
8.53 |
InPlay |
5.86 |
1.13 |
5.06 |
8.1 |
Four-Seam |
90.11 |
2155.67 |
210.23 |
11.97 |
21.77 |
BallCalled |
5.94 |
0.81 |
4.97 |
7.0 |
Four-Seam |
89.27 |
2176.90 |
211.67 |
11.10 |
19.21 |
StrikeCalled |
5.93 |
0.89 |
5.07 |
7.1 |
Changeup |
81.56 |
1618.41 |
255.35 |
14.64 |
5.18 |
StrikeSwinging |
5.94 |
0.94 |
5.06 |
8.5 |
Changeup |
82.64 |
1648.09 |
252.65 |
11.39 |
4.89 |
FoulBallNotFieldable |
5.93 |
0.91 |
5.00 |
8.4 |
Four-Seam |
90.89 |
2137.86 |
214.20 |
11.99 |
18.79 |
FoulBallNotFieldable |
5.96 |
0.81 |
5.09 |
7.1 |
Changeup |
83.10 |
1707.03 |
247.48 |
16.11 |
8.01 |
FoulBallNotFieldable |
5.87 |
1.12 |
4.94 |
8.2 |
Slider |
83.28 |
2073.22 |
158.12 |
-1.71 |
5.55 |
BallCalled |
5.84 |
0.94 |
5.03 |
5.3 |
Four-Seam |
90.79 |
2180.19 |
218.72 |
13.82 |
18.43 |
InPlay |
5.88 |
0.96 |
5.07 |
7.3 |
Four-Seam |
91.02 |
2176.57 |
195.85 |
6.35 |
23.56 |
BallCalled |
5.94 |
0.98 |
5.10 |
6.5 |
Four-Seam |
89.91 |
2201.17 |
221.07 |
15.12 |
18.57 |
StrikeSwinging |
5.91 |
0.94 |
5.06 |
7.4 |
Sinker |
89.77 |
2182.00 |
224.09 |
15.40 |
17.09 |
InPlay |
5.91 |
0.99 |
5.03 |
7.5 |
Changeup |
84.04 |
1644.37 |
244.56 |
19.08 |
10.37 |
StrikeSwinging |
5.84 |
1.21 |
5.06 |
8.2 |
Changeup |
84.41 |
1869.45 |
245.46 |
20.29 |
10.58 |
StrikeSwinging |
5.89 |
1.13 |
4.92 |
8.2 |
Changeup |
84.56 |
1924.37 |
240.09 |
19.94 |
12.80 |
FoulBallNotFieldable |
5.88 |
1.13 |
5.04 |
8.0 |
Four-Seam |
91.54 |
2210.51 |
215.20 |
11.72 |
17.77 |
FoulBallNotFieldable |
5.84 |
1.06 |
5.36 |
7.2 |
Changeup |
84.92 |
1982.86 |
247.35 |
21.99 |
10.54 |
BallCalled |
5.87 |
1.04 |
5.21 |
8.2 |
Four-Seam |
91.74 |
2203.92 |
224.69 |
16.23 |
17.56 |
BallCalled |
5.86 |
1.11 |
5.17 |
7.5 |
Changeup |
84.91 |
1649.66 |
224.72 |
14.91 |
16.33 |
StrikeSwinging |
5.91 |
1.17 |
5.03 |
7.5 |
Changeup |
80.53 |
1723.69 |
242.81 |
15.08 |
9.12 |
InPlay |
5.88 |
1.08 |
4.89 |
8.1 |
Changeup |
82.44 |
2100.30 |
243.87 |
18.06 |
10.34 |
BallCalled |
5.83 |
1.11 |
4.96 |
8.1 |
Four-Seam |
89.94 |
2126.91 |
220.46 |
13.91 |
17.57 |
StrikeCalled |
5.81 |
1.09 |
4.99 |
7.3 |
Changeup |
82.85 |
1895.53 |
239.51 |
15.75 |
10.58 |
BallCalled |
5.85 |
1.05 |
5.17 |
8.0 |
Four-Seam |
91.23 |
2213.34 |
206.30 |
9.29 |
19.97 |
BallCalled |
5.87 |
1.11 |
5.13 |
6.9 |
Four-Seam |
88.99 |
2070.44 |
217.11 |
12.17 |
17.30 |
InPlay |
5.92 |
0.98 |
5.20 |
7.2 |
# 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")
## Total number of pitches thrown: 68
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Ben Alekson")
Summary Pitch Table for Ben Alekson
Changeup |
30.88% |
21 |
5 |
16 |
23.81% |
76.19% |
82.96 |
1741.18 |
9.43 |
16.19 |
243.84 |
8.1 |
5.89 |
1.09 |
5.04 |
Four-Seam |
54.41% |
37 |
9 |
28 |
24.32% |
75.68% |
90.61 |
2179.25 |
19.65 |
13.58 |
216.22 |
7.2 |
5.91 |
0.98 |
5.06 |
Sinker |
8.82% |
6 |
1 |
5 |
16.67% |
83.33% |
90.28 |
2145.70 |
16.72 |
15.70 |
225.34 |
7.5 |
5.91 |
0.97 |
5.08 |
Slider |
5.88% |
4 |
4 |
0 |
100.00% |
0.00% |
83.05 |
2034.08 |
5.54 |
-1.45 |
166.56 |
5.6 |
5.89 |
0.91 |
5.05 |
# 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("Ben Alekson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Ben Alekson maximum FB velocity: 92.13 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Ben_Alekson_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 Ben Alekson",
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 Ben Alekson",
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")
)
