-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitch exploration.R
196 lines (162 loc) · 6.07 KB
/
pitch exploration.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
library(ggthemes)
# Avg velocity by team
pitch_data %>%
group_by(mlb_team) %>%
summarize("Release Speed" = mean(release_speed)) %>%
arrange(desc(`Release Speed`)) %>%
mutate("rank" = rank(-`Release Speed`))
# Avg fastball velocity by team
pitch_data %>%
filter(pitch_type %in% c("FF","FT", "SI")) %>%
group_by(mlb_team) %>%
summarize("Release Speed" = mean(release_speed)) %>%
arrange(desc(`Release Speed`)) %>%
mutate("fastball rank" = rank(-`Release Speed`))
# Avg velocity by pitch type
pitch_data %>%
group_by(pitch_type) %>%
summarize("Release Speed" = mean(release_speed)) %>%
arrange(desc(`Release Speed`))
pitch_data %>%
group_by(pitch_type) %>%
summarize("Release Speed" = mean(release_speed)) %>%
arrange(desc(`Release Speed`)) %>%
mutate(pitch_type = recode(pitch_type,
FF = "Four-Seam Fastball",
FT = "Two-Seam Faseball",
SI = "Sinker",
FC = "Cutter",
PO = "Pitchout",
FO = "Forkball",
null = "Null",
FS = "Splitter",
SL = "Slider",
CH = "Changeup",
KC = "Knuckle-Curve",
SC = "Screwball",
CU = "Curveball",
IN = "Intentional Ball",
KN = "Knuckleball",
AB = "Automatic Ball",
EP = "Eephus",
UN = "Unknown"))
# Avg fastball velocity by team
pitch_data %>%
filter(pitch_type %in% c("FT", "FF", "SI")) %>%
group_by(mlb_team) %>%
summarize("avg_fb" = mean(release_speed)) %>%
ggplot(aes(x = mlb_team, y = avg_fb)) +
geom_bar(stat = "identity") +
coord_flip()
# Avg fastball by team - cleveland plot
pitch_data %>%
filter(pitch_type %in% c("FT", "FF", "SI")) %>%
group_by(mlb_team) %>%
summarize("avg_fb" = mean(release_speed)) %>%
ggplot(aes(x = avg_fb, y = reorder(mlb_team, avg_fb))) +
geom_point(size=3) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60", linetype="dashed")) +
xlab("Average Fastball Velocity") +
ylab("Team")
## Curve
pitch_data %>%
filter(pitch_type == "CU") %>%
group_by(mlb_team) %>%
summarize("avg_curve" = mean(release_speed)) %>%
ggplot(aes(x = avg_curve, y = reorder(mlb_team, avg_curve))) +
geom_point(size=3) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60", linetype="dashed"))
pitch_data %>%
group_by(mlb_team) %>%
summarize("avg_curve" = mean(release_speed)) %>%
ggplot(aes(x = avg_curve, y = reorder(mlb_team, avg_curve))) +
geom_point(size=3) +
theme_light() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60", linetype="dashed")) +
ggtitle("Average Curveball Velo by Team")
## Avg velo of four pitch types
pitch_data %>%
filter(pitch_type %in% c("CU", "FF", "SL", "CH")) %>%
group_by(mlb_team, pitch_type) %>%
summarize("avg_velo" = mean(release_speed)) %>%
ggplot(aes(x = avg_velo, y = mlb_team, col = pitch_type)) +
geom_point(size=3) +
theme_light() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60", linetype="dashed")) +
scale_color_discrete(name = "Pitch Type", breaks = c("CH", "CU", "FF", "SL"), labels = c("Change-Up", "Curveball", "Four-Seam FB", "Slider"))+
xlab("Average release velolcity (mph)")+
ylab("Team") +
ggtitle("Average velocity of four pitch types")
## Avg Fastball Velocity by Inning
pitch_data %>%
filter(pitch_type %in% c("FF", "FT", "SI")) %>%
group_by(inning) %>%
summarize("Average Velocity (mph)" = mean(release_speed)) %>%
ggplot(aes(x = inning, y = `Average Velocity (mph)`)) +
geom_point(size = 3) +
theme_light() +
theme(panel.grid.major.y = element_line(colour="grey60", linetype="dashed")) +
scale_x_continuous(breaks = 1:12, limits = c(1,12)) +
scale_y_continuous(breaks = 91:97, limits = c(92,95)) +
ggtitle("Average Fastball velo by inning") +
xlab("Inning")
## Avg Curveball velo by inning
pitch_data %>%
filter(pitch_type == "CU") %>%
group_by(inning) %>%
summarize("Average Velocity (mph)" = mean(release_speed)) %>%
ggplot(aes(x = inning, y = `Average Velocity (mph)`)) +
geom_point(size = 3) +
theme_light() +
theme(panel.grid.major.y = element_line(colour="grey60", linetype="dashed")) +
scale_x_continuous(breaks = 1:12, limits = c(1,12)) +
ggtitle("Average Curveball velo by inning")
#economist theme
pitch_data %>%
filter(pitch_type %in% c("FF", "FT", "FI")) %>%
group_by(inning) %>%
summarize("Average Velocity (mph)" = mean(release_speed)) %>%
ggplot(aes(x = inning, y = `Average Velocity (mph)`)) +
geom_point(size = 3) +
theme_economist() +
scale_x_continuous(breaks = 1:12, limits = c(1,12)) +
scale_y_continuous(breaks = 90:100) +
ggtitle("Average Fastball velo by inning")
#pitchtypes by team
team_pitch_avg <- pitch_data %>%
filter(pitch_type %in% c("CU", "FF", "SL", "CH")) %>%
group_by(mlb_team, pitch_type) %>%
summarize("avg_velo" = mean(release_speed))
## How many eephuses?
pitch_data %>%
filter(pitch_type == "EP") %>%
tally()
## Who threw all the Eephuses?
pitch_data %>%
filter(pitch_type == "EP") %>%
group_by(player_name) %>%
tally() %>%
arrange(desc(n))
## Eephus Balls/Strikes
pitch_data %>%
filter(pitch_type == "EP") %>%
group_by(type) %>%
tally() %>%
arrange(desc(n))
## Eephus outcomes
pitch_data %>%
filter(pitch_type == "EP") %>%
group_by(description) %>%
tally() %>%
arrange(desc(n))
## Shields Eephus: https://chicago.suntimes.com/sports/slower-is-better-for-the-reinvented-james-shields/