qauliSummary.R

Uploaded by:thirst

              orderTeams=function (teams) factor(teams,levels=c("Red Bull Racing-Renault","McLaren-Mercedes","Ferrari","Mercedes","Lotus-Renault","Force India-Mercedes","Sauber-Ferrari","STR-Ferrari","Williams-Renault","Caterham-Renault","HRT-Cosworth","Marussia-Cosworth"),ordered=T)
tlid=data.frame(driverName=c("Sebastian Vettel","Mark Webber","Jenson Button", "Lewis Hamilton", "Fernando Alonso","Felipe Massa","Michael Schumacher","Nico Rosberg", "Kimi Räikkönen","Romain Grosjean", "Paul di Resta", "Nico Hulkenberg","Kamui Kobayashi","Sergio Perez","Daniel Ricciardo","Jean-Eric Vergne","Pastor Maldonado","Bruno Senna","Heikki Kovalainen","Vitaly Petrov", "Pedro de la Rosa","Narain Karthikeyan","Timo Glock" ,"Charles Pic" ),TLID= c('VET','WEB','BUT','HAM','ALO','MAS','MSC','ROS','RAI','GRO','DIR','HUL','KOB','PER','RIC','VER','MAL','SEN','KOV','PET','DLR','KAR','GLO','PIC'))
  
  
temporaryFile <- tempfile()
fn=paste("https://api.scraperwiki.com/api/1.0/datastore/sqlite?format=csv&name=f1comscraper&query=select+*+from+`qualiSectors`&apikey=")
download.file(fn,destfile=temporaryFile, method="curl")
qualisectors=read.csv(temporaryFile)
qualisectors=merge(qualisectors,tlid,by='driverName')
belqs=subset(qualisectors,race=="BELGIUM")
belqs$driverName=reorder(belqs$driverName, belqs$driverNum)
belqs$TLID=reorder(belqs$TLID, belqs$driverNum)

temporaryFile <- tempfile()
fn=paste("https://api.scraperwiki.com/api/1.0/datastore/sqlite?format=csv&name=f1comscraper&query=select+*+from+`qualiSpeeds`&apikey=")
download.file(fn,destfile=temporaryFile, method="curl")
qualiSpeeds=read.csv(temporaryFile)
qualiSpeeds=merge(qualiSpeeds,tlid,by='driverName')
belqspeed=subset(qualiSpeeds,race=="BELGIUM")
belqspeed$driverName=reorder(belqspeed$driverName, belqspeed$driverNum)
belqspeed$TLID=reorder(belqspeed$TLID, belqspeed$driverNum)


require(plyr)
nullmin=function(d) {if (is.finite(min(d,na.rm=T))) return(min(d,na.rm=T)) else return(NA)}

#Find the fastest time recorded in each sector
minqx=ddply(.variables=c("sector"),.data=belqs,.fun= function(d) data.frame(minqxt=nullmin(min(d$sectortime,na.rm=T))))
#Normalise the each driver's session time
belqs=merge(belqs,minqx,by='sector')
belqs$norm=belqs$sectortime/belqs$minqxt
belqs$delta=belqs$sectortime-belqs$minqxt

#Find the fastest speeds recorded in each sector
maxsp= max(belqspeed$qspeed)
belqspeed$norm=belqspeed$qspeed/maxsp

require(ggplot2)
g=ggplot(belqs)+geom_point(aes(x=TLID,y=sectortime))+facet_wrap(~sector)
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Times")
g=g+opts(axis.text.x=theme_text(angle=-90,size=5))
g=g+xlab(NULL)+ylab("Sectortime (s)")
print(g)

g=ggplot(belqs)+geom_point(aes(x=TLID,y=norm))+facet_wrap(~sector)
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Times (Normalised)")
g=g+opts(axis.text.x=theme_text(angle=-90,size=5))
g=g+xlab(NULL)+ylab("Normalised sectortime")+scale_y_reverse()
print(g)


g=ggplot(belqs)+geom_point(aes(x=TLID,y=delta))+facet_wrap(~sector)
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Times (Deltas)")
g=g+opts(axis.text.x=theme_text(angle=-90,size=5))
g=g+xlab(NULL)+ylab("Delta from best (s)")+scale_y_reverse()
print(g)



g=ggplot(belqs)+geom_point(aes(x=TLID,y=delta,col=factor(sector)))
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Times (Deltas)")
g=g+opts(axis.text.x=theme_text(angle=-90,size=5))
g=g+scale_colour_discrete(name = "Sector")
g=g+xlab(NULL)+ylab("Delta from best (s)")+scale_y_reverse()
print(g)

g=ggplot(belqs)+geom_point(aes(x=TLID,y=norm,col=factor(sector)))
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Times (Norms)")
g=g+opts(axis.text.x=theme_text(angle=-90,size=5))
g=g+scale_colour_discrete(name = "Sector")
g=g+xlab(NULL)+ylab("Delta from best (s)")+scale_y_reverse()
print(g)
g2=g+geom_point(data=belqspeed,aes(x=TLID,y=2-norm),col='black')
g2=g2+opts(title="F1 2012 Belgium - Qualifying Sector Times (Norms, 2-normSpeed)")
print(g2)

g=qplot(TLID, data=belqs, geom="bar", weight = delta, fill=factor(sector)) 
g=g+xlab(NULL)+opts(axis.text.x=theme_text(angle=-90,size=6))
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Times (Deltas)")
g=g+scale_fill_hue(name="Sector")+ylab('Total delta (s)')
print(g)


g=ggplot(belqs)+geom_text(aes(x=pos,y=delta,label=TLID),size=3)+facet_wrap(~sector)
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Deltas vs position")
print(g)

g=ggplot(belqs)+geom_text(aes(x=pos,y=norm,label=TLID),size=3)+facet_wrap(~sector)
g=g+opts(title="F1 2012 Belgium - Qualifying Sector Deltas vs position")
print(g)