Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Created January 2, 2012 23:08
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 psychemedia/1552552 to your computer and use it in GitHub Desktop.
Save psychemedia/1552552 to your computer and use it in GitHub Desktop.
Graphical view over sector times for F1 2011 season
require(RCurl)
require(plyr)
require(ggplot2)
gsqAPI = function(key,query,gid=0){ return( read.csv( paste( sep="",'http://spreadsheets.google.com/tq?', 'tqx=out:csv','&tq=', curlEscape(query), '&key=', key, '&gid=', curlEscape(gid) ), na.strings = "null" ) ) }
key='0AmbQbL4Lrd61dEd0S1FqN2tDbTlnX0o4STFkNkc0NGc'
sheet=5
qualidata=gsqAPI(key,'select *',sheet)
#Order the levels in the race factor in terms of calendar order
qualiSectors2011$race=factor(qualiSectors2011$race,levels=c("AUSTRALIA","MALAYSIA","CHINA","TURKEY","SPAIN","MONACO","CANADA","EUROPE","GREAT BRITAIN","GERMANY","HUNGARY","BELGIUM","ITALY","SINGAPORE","JAPAN","KOREA","INDIA","ABU DHABI","BRAZIL"),ordered=T)
#Annotate the data with teams
sheet=4
query="select A,C,E"
teamdata=gsqAPI(key,query,sheet)
qualiSectors2011=merge(qualidata,teamdata,by=c("race","driverNum"))
#Order the teams
qualiSectors2011$team=factor(qualiSectors2011$team,levels=c("RBR-Renault","McLaren-Mercedes","Ferrari","Mercedes","Renault","Force India-Mercedes","Sauber-Ferrari","STR-Ferrari","Williams-Cosworth","Lotus-Renault","HRT-Cosworth","Virgin-Cosworth"),ordered=T)
teamcolours=c("blue","darkgray","red","lightsteelblue3","goldenrod3","darkorange","gray8","firebrick4","midnightblue","darkgreen","gray0","darkred")
#This is a hack - is there a better way?
nullmin=function(d) {if (is.finite(min(d,na.rm=T))) return(min(d,na.rm=T)) else return(NA)}
#Find the fastest overall time for each sector, over all drivers
minqsectors=ddply(.variables=c("race","sector"),.data=qualiSectors2011,.fun= function(d) data.frame(d,sectorMin=nullmin(d$sectortime)))
#Normalise the sector time for each driver according to the fastest sector time over all drivers
minqsectors=ddply(.variables=c("race","sector"),.data=minqsectors,.fun= function(d) data.frame(d,normSector=d$sectortime/d$sectorMin))
#Plot the loess model based on both driver's fastest quali times for each sector by race
ggplot(minqsectors)+stat_smooth(method="loess",aes(x=race,y=normSector,group=team,col=factor(team)), se=FALSE)+ylim(0.99,1.1) + facet_wrap(~sector) +opts(title="F1 2011 Fastest Qualifying Sector Times by Team Evolution",axis.text.x=theme_text(angle=-90))+scale_colour_manual(name="Teams",values = teamcolours)+xlab(NULL)+ylab("Fit on quali sectortime as % of fastest overall quali sector time")
#Plot the loess model based on both driver's fastest quali times for each sector by team
ggplot(minqsectors)+stat_smooth(method="loess",aes(x=race,y=normSector,group=sector,col=factor(sector)), se=FALSE)+ylim(0.99,1.1) + facet_wrap(~team) +opts(title="F1 2011 Fastest Qualifying Sector Times by Team Evolution",axis.text.x=theme_text(angle=-90))+xlab(NULL)+ylab("Fit on quali sectortime as % of fastest overall quali sector time")
#Find the fastest time recorded by each team for each qualifying sector
minqsectors=ddply(.variables=c("race","sector","team"),.data=minqsectors,.fun= function(d) data.frame(d,teamMinSector=nullmin(d$sectortime)))
#Normalise the best sector time for each team according to the fastest sector time over all drivers
minqsectors=ddply(.variables=c("race","sector"),.data=minqsectors,.fun= function(d) data.frame(d,normTeamSector=d$teamMinSector/d$sectorMin))
#Plot the loess model based on each team's fastest quali times for each sector by race
ggplot(minqsectors)+stat_smooth(method="loess",aes(x=race,y=normTeamSector,group=team,col=factor(team)), se=FALSE)+ylim(0.99,1.1) + facet_wrap(~sector) +opts(title="F1 2011 Fastest Qualifying Sector Times by Team Evolution",axis.text.x=theme_text(angle=-90))+scale_colour_manual(name="Teams",values = teamcolours)+xlab(NULL)+ylab("Min team quali sectortime as % of fastest overall quali sector time")
#Plot the loess model based on each team's fastest quali times for each sector
ggplot(minqsectors)+stat_smooth(method="loess",aes(x=race,y=normTeamSector,group=sector,col=factor(sector)), se=FALSE)+ylim(0.99,1.1) + facet_wrap(~team) +opts(title="F1 2011 Fastest Qualifying Sector Times by Team Evolution",axis.text.x=theme_text(angle=-90))+xlab(NULL)+ylab("Min team quali sectortime as % of fastest overall quali sector time")
#Find the mean time recorded for each qualifying sector over the best times for each team
minqsectors=ddply(.variables=c("race","sector"),.data=minqsectors,.fun= function(d) data.frame(d,meanTeamBestSector=mean(d$teamMinSector,na.rm=TRUE)))
#Normalise the best sector time for each team according to the mean best sector time over all teams
minqsectors=ddply(.variables=c("race","sector"),.data=minqsectors,.fun= function(d) data.frame(d,meanNormTeamSector=d$teamMinSector/d$meanTeamBestSector))
#Plot the loess model based on each team's fastest quali times for each sector by race normalised against mean of team best sector times
ggplot(minqsectors)+stat_smooth(method="loess",aes(x=race,y=meanNormTeamSector,group=team,col=factor(team)), se=FALSE)+ylim(0.9,1.1) + facet_wrap(~sector) +opts(title="F1 2011 Fastest Qualifying Sector Times by Team Evolution",axis.text.x=theme_text(angle=-90))+scale_colour_manual(name="Teams",values = teamcolours)+xlab(NULL)+ylab("Min team quali sectortime as % of fastest overall quali sector time")
#Plot the loess model based on each team's fastest quali times for each sector normalised against mean of team best sector times
ggplot(minqsectors)+stat_smooth(method="loess",aes(x=race,y=meanNormTeamSector,group=sector,col=factor(sector)), se=FALSE)+ylim(0.95,1.1) + facet_wrap(~team) +opts(title="F1 2011 Fastest Qualifying Sector Times by Team Evolution ",axis.text.x=theme_text(angle=-90))+xlab(NULL)+ylab("Min team quali sectortime as % of fastest overall quali sector time")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment