Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created December 12, 2016 12:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayesball/af4fb9e66b7aadc7fe4d188b034868ef to your computer and use it in GitHub Desktop.
Save bayesball/af4fb9e66b7aadc7fe4d188b034868ef to your computer and use it in GitHub Desktop.
Plots career trajectories for several pitchers nominated for the Hall of Fame in 2017
get_data <- function(pitcher){
require(Lahman)
require(dplyr)
get.birthyear <- function(player.id){
playerline <- filter(Master, playerID == player.id)
birthyear <- playerline$birthYear
birthmonth <- playerline$birthMonth
ifelse(birthmonth >= 7, birthyear + 1, birthyear)
}
names <- unlist(strsplit(pitcher, " "))
id <- filter(Master,
nameFirst==names[1], nameLast==names[2])$playerID
filter(Pitching, playerID==id) %>%
mutate(Age = yearID - get.birthyear(id)) -> d
summarize(group_by(d, yearID),
Age = first(Age), IPouts = sum(IPouts),
H = sum(H), BB = sum(BB), SO = sum(SO),
HR = sum(HR), HBP = sum(HBP),
W = sum(W), L = sum(L))
}
pdata <- rbind(data.frame(Player="Clemens",
get_data("Roger Clemens")),
data.frame(Player="Mussina",
get_data("Mike Mussina")),
data.frame(Player="Schilling",
get_data("Curt Schilling")),
data.frame(Player="Maddux",
get_data("Greg Maddux")),
data.frame(Player="Smoltz",
get_data("John Smoltz")),
data.frame(Player="Glavine",
get_data("Tom Glavine")))
pdata <- mutate(pdata, WHIP = (H + BB) / (IPouts / 3),
SO.Rate = SO / (IPouts / 3),
FIP=(13 * HR + 3 * (BB + HBP) - 2 * SO) /
(IPouts / 3),
Win_PCT = 100 * W / (W + L))
library(ggplot2)
library(ggthemes)
TH <- theme(
plot.title = element_text(
colour = "blue",
size = 18,
hjust = 0.5,
vjust = 0.8,
angle = 0
)
)
ggplot(pdata, aes(Age, Win_PCT)) +
geom_point() +
geom_smooth() +
ylim(30, 90) +
facet_wrap(~ Player, ncol=2) +
theme_solarized() +
ggtitle("Win Percentage Trajectories") + TH
ggplot(pdata, aes(Age, WHIP)) +
geom_point() +
geom_smooth() +
facet_wrap(~ Player, ncol=2) +
ylim(.8, 1.6) +
theme_solarized() +
ggtitle("WHIP Trajectories") + TH
ggplot(pdata, aes(Age, SO.Rate)) +
geom_point() +
geom_smooth() +
facet_wrap(~ Player, ncol=2) +
ylim(.5, 1.25) +
theme_solarized() +
ggtitle("SO Rate Trajectories") + TH
ggplot(pdata, aes(Age, FIP)) +
geom_point() +
geom_smooth() +
facet_wrap(~ Player, ncol=2) +
ylim(-1, 2) +
theme_solarized() +
ggtitle("FIP Trajectories") + TH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment