Skip to content

Instantly share code, notes, and snippets.

@gghatano
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gghatano/10616962 to your computer and use it in GitHub Desktop.
Save gghatano/10616962 to your computer and use it in GitHub Desktop.
library(Lahman)
library(dplyr)
library(magrittr)
library(pings)
# Batting: season stats data frame of all the players
all_dat <- Batting %>%
select(yearID, AB, H, HR) %>%
group_by(yearID) %>%
dplyr::summarise(H = sum(H, na.rm = TRUE),
HR = sum(HR, na.rm = TRUE),
AB = sum(AB, na.rm=TRUE)) %>%
filter(yearID > 1900) %>%
mutate(HIT_rate = H / AB, HR_rate = HR / AB) %>%
select(yearID, HIT_rate, HR_rate) %>%
reshape::melt(id.vars="yearID") %>%
setnames(c("year", "var", "rate"))
## visualize
library(ggplot2)
all_dat %>% filter(var=="HR_rate") %>%
ggplot() + geom_line(aes(x=year, y=rate)) +
ggtitle("season-HR rate") +
theme(plot.title=element_text(face="bold", size=24)) +
theme(axis.title.x=element_text(size=24)) +
theme(axis.title.y=element_text(size=24)) +
ggsave("season_HR.pdf", width=0.353*1024, height=0.353*628, unit="mm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment