Skip to content

Instantly share code, notes, and snippets.

@gghatano
Created January 10, 2014 00:26
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/8344753 to your computer and use it in GitHub Desktop.
Save gghatano/8344753 to your computer and use it in GitHub Desktop.
Career K/BB ranking in MLB data (21st)
# データ読み込み
Pitching <- read.csv("Pitching.csv")
# 重いので2000年以降に限る
Pitching <- subset(Pitching, yearID> 2000)
# plyrを使ってデータを要約
Pitching.KBB <- ddply(Pitching, .(playerID), summarize,
Career.SO = sum(SO, na.rm = TRUE), Career.BB = sum(BB, na.rm=TRUE),
Career.IPouts = sum(IPouts, na.rm = TRUE),
Career.KBB = sum(SO, na.rm=TRUE)/ sum(BB, na.rm = TRUE))
# 四球1以上でアウトを300個以上とった選手
Pitching.KBB <- subset(Pitching.KBB, Career.BB > 0 & Career.IPouts >= 300)
# K/BBで並べ替え
Pitching.KBB <- Pitching.KBB[order(Pitching.KBB$Career.KBB, decreasing = TRUE), ]
library(ggplot2)
# top 10 list
Pitching.KBB.toplist <- head(Pitching.KBB, 10)
# ggplot
p <- ggplot(Pitching.KBB.toplist, aes(x = reorder(playerID, Career.KBB), y = Career.KBB))
p <- p + geom_bar(stat="identity", fill = "lightblue", colour = "black")
#p <- p + scale_fill_brewer(palette="Pastel1")
p <- p + xlab("Name")
p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment