A glimpse on Sachin's ODI scores
R Graphics with ggplot2
Vinu CT
14 Sep 2012
A glimpse on Sachin's ODI scores
Experience with R (Bring your laptop with R installed.)
library("ggplot2")
s10 <- read.csv(file="http://www.iimb.ernet.in/~vinuct10/ggplot/sachin.csv")
tail(s10,3)
## Subsetting the dataset based on few dismissal
s10a <- subset(s10,Dismissal %in% c("bowled", "caught", "lbw","not out"))
s10a$Inns <- factor(s10a$Inns)
| X | StartDate | Runs | BF | SR | Pos | Dismissal | Inns | Opposition | Ground | result | Score | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 450 | 450 | 2012-03-13 | 6 | 19 | 31.57 | 2 | caught | 1 | v Sri Lanka | Dhaka | won | 304/3 |
| 451 | 451 | 2012-03-16 | 114 | 147 | 77.55 | 2 | caught | 1 | v Bangladesh | Dhaka | lost | 289/5 |
| 452 | 452 | 2012-03-18 | 52 | 48 | 108.33 | 2 | caught | 2 | v Pakistan | Dhaka | won | 330/4 |
s10c <- subset(s10,Opposition %in% c("v Australia","v South Africa","v New Zealand") ) ggplot(data=s10c)+geom_point(aes(x=BF,y=SR,colour=Opposition)) ggplot(data=s10c)+geom_point(aes(x=BF,y=SR,shape=Opposition)) ggplot(data=s10c)+geom_point(aes(x=BF,y=SR,size=Opposition)) ggplot(data=s10c)+geom_point(aes(x=BF,y=SR,colour=Opposition,shape=result)) ggplot(data=s10c)+geom_point(aes(x=BF,y=SR,colour=Opposition)) +
geom_smooth(aes(x=BF,y=SR,colour=Opposition )) + xlab("Ball Faced") + ylab("Runs Scored")
## Examples of geoms: line,point,box,bar,...
s10b <- subset(s10,result %in% c("won", "lost") & Opposition %in% c("v Australia","v South Africa","v New Zealand"))
s10b$result <- factor(s10b$result)
s10b$opp <- factor(s10b$Opposition)
ggplot(s10b,aes(opp,Runs))+geom_violin(aes(fill=opp)) require(chron) qplot(x=factor(years(StartDate)), y=Runs, geom="boxplot", fill=I("blue"), data=s10b)+ xlab("Year")
## Examples of stats: smooth, boxplot,... qplot(Runs, data=s10b,fill=result, geom=c("histogram")) qplot(Runs, data=s10b,colour=result,geom=c("density")) ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs ))+ geom_line(data=s10,aes(BF,BF))+geom_smooth(aes(x=BF,y=Runs )) + xlab("Ball Faced") + ylab("Runs Scored")
## Scales
ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs,colour=result))+ scale_colour_manual(values=c("lost"="red","won"="green"))
ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs,size=result,shape=result))+ scale_size_manual(values=c("lost"=5,"won"=3))+ scale_shape_manual(values=c("lost"=15,"won"=25))
ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs,colour=result))+ scale_colour_discrete(name="Parinaam",labels=c("lost"="HAAR","won"="JEET"))
## Facets
s10c= subset(s10,(Opposition %in% c("v Australia","v England","v South Africa","v Sri Lanka")) & (result %in% c("lost", "won")) )
s10c$yr= cut(as.numeric(format(as.Date(s10c$StartDate), format="%Y")),4)
qplot(data=s10c, factor(Inns),fill=result,geom="bar",position="fill",facets= Opposition~yr)
## Themes
s10a <- subset(s10,Dismissal %in% c("bowled", "caught", "lbw","not out"))
s10a$Dismissal <- factor(s10a$Dismissal)
s10a$Inns <- factor(s10a$Inns)
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)+theme_bw()
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)+theme(panel.background = element_rect(fill="lightblue") )
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)+ theme(plot.background= element_rect(fill="yellow"),panel.background= element_rect(fill='purple'))
Slides themes are taken from Vadim Makeev's Shower templeate