Skip to content

Instantly share code, notes, and snippets.

@bdilday
Last active Apr 15, 2018
Embed
What would you like to do?
R graph - top10 war by franchise
library(readr)
library(ggplot2)
library(dplyr)
library(Lahman)
br_war = read_csv("https://www.baseball-reference.com/data/war_daily_bat.txt")
m = Lahman::Teams
ndf = br_war %>% merge(m %>% select(teamIDBR, yearID, name, franchID), by.x=c("team_ID", "year_ID"), by.y=c("teamIDBR", "yearID")) %>% group_by(franchID, name, player_ID) %>% summarise(w=sum(as.numeric(WAR), na.rm=TRUE))
xx = ndf %>% arrange(franchID, -w) %>% mutate(i=row_number()) %>% group_by(franchID, name) %>% summarise(m=max(i)) %>% arrange(franchID,-m) %>% mutate(ir=row_number()) %>% group_by(franchID) %>% mutate(m2=max(m), m=sum(m)) %>% filter(ir==1) %>% select(franchID, m=m2)
p = ndf %>% merge(xx, by="franchID") %>% group_by(franchID) %>% arrange(-w) %>% mutate(i=row_number()) %>% filter(i<=10) %>% filter(m>=110) %>% ggplot(aes(x=i, y=w)) + geom_bar(stat='identity') + facet_wrap(~franchID) + theme_minimal() + labs(x='franchise rank', y='career WAR')
print(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment