Created
January 27, 2018 14:23
-
-
Save bayesball/b8370d728db5ca6607223f8b32ebc790 to your computer and use it in GitHub Desktop.
R function to plot home run rate, strikeout rate, walk rate, and wOBA for a specific baseball hitter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot_rates <- function(player_name){ | |
require(Lahman) | |
require(tidyverse) | |
require(gridExtra) | |
require(baseballr) | |
wts <- fg_guts() | |
names <- unlist(str_split(player_name, " ")) | |
Master %>% filter(nameLast == names[2], | |
nameFirst == names[1]) %>% | |
select(playerID) -> d | |
Batting %>% filter(playerID == d$playerID) %>% | |
group_by(yearID) %>% | |
summarize(AB = sum(AB), | |
BIP = sum(AB) - sum(SO), | |
H = sum(H), | |
HR = sum(HR), | |
SO = sum(SO), | |
BB = sum(BB), | |
IBB = sum(IBB), | |
X2B = sum(X2B), | |
X3B = sum(X3B), | |
HBP = sum(HBP), | |
SF = sum(SF)) %>% | |
mutate(X1B = H - X2B - X3B - HR, | |
uBB = BB - IBB, | |
Year = as.character(yearID)) -> pdata | |
inner_join(pdata, wts, by=c("yearID" = "season")) -> | |
pdata | |
pdata %>% mutate(wOBA = (wBB * uBB + wHBP * HBP + | |
w1B * X1B + w2B * X2B + w3B * X3B + wHR * HR) / | |
(AB + BB - IBB - SF + HBP)) -> pdata | |
Batting %>% filter(yearID %in% pdata$yearID) %>% | |
group_by(yearID, playerID) %>% | |
summarize(AB = sum(AB), | |
BIP = sum(AB) - sum(SO), | |
H = sum(H), | |
HR = sum(HR), | |
SO = sum(SO), | |
BB = sum(BB), | |
IBB = sum(IBB), | |
X2B = sum(X2B), | |
X3B = sum(X3B), | |
HBP = sum(HBP), | |
SF = sum(SF)) %>% | |
filter(AB >= 300) %>% | |
mutate(Year = as.character(yearID), | |
X1B = H - X2B - X3B - HR, | |
uBB = BB - IBB) -> S1 | |
inner_join(S1, wts, by=c("yearID" = "season")) -> | |
S1 | |
S1 %>% mutate(wOBA = (wBB * uBB + wHBP * HBP + | |
w1B * X1B + w2B * X2B + w3B * X3B + wHR * HR) / | |
(AB + BB - IBB - SF + HBP)) -> S1 | |
TH <- theme( | |
plot.title = element_text( | |
colour = "blue", | |
size = 16, | |
hjust = 0.5, | |
vjust = 0.8, | |
angle = 0 | |
)) | |
p1 <- ggplot(S1, aes(Year, HR / BIP)) + | |
geom_boxplot() + | |
geom_point(data = pdata, | |
aes(Year, HR / (AB - SO), | |
group=player_name), | |
color="red", size=2.5) + | |
ggtitle("Home Run Rate") + TH + xlab("") + | |
theme(axis.text.x = element_text(angle = 60, | |
hjust = 1)) | |
p2 <- ggplot(S1, aes(Year, SO / (AB + BB))) + | |
geom_boxplot() + | |
geom_point(data = pdata, | |
aes(Year, SO / (AB + BB), | |
group=player_name), | |
color="red", size=2.5) + | |
ggtitle("Strikeout Rate") + TH + xlab("") + | |
theme(axis.text.x = element_text(angle = 60, | |
hjust = 1)) | |
p3 <- ggplot(S1, aes(Year, BB / (AB + BB))) + | |
geom_boxplot() + | |
geom_point(data = pdata, | |
aes(Year, BB / (AB + BB), | |
group=player_name), | |
color="red", size=2.5) + | |
ggtitle("Walk Rate") + TH + xlab("") + | |
theme(axis.text.x = element_text(angle = 60, | |
hjust = 1)) | |
p4 <- ggplot(S1, aes(Year, wOBA)) + | |
geom_boxplot() + | |
geom_point(data = pdata, | |
aes(Year, wOBA, | |
group=player_name), | |
color="red", size=2.5) + | |
ggtitle("wOBA") + TH + xlab("") + | |
theme(axis.text.x = element_text(angle = 60, | |
hjust = 1)) | |
grid.arrange(p1, p2, p3, p4, ncol=2, | |
top=player_name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment