The Race to the F1 2012 Drivers’ Championship – Initial Sketches
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In part inspired by the chart described in The electoral map sans the map, I thought I’d start mulling over a quick sketch showing the race to the 2012 Formula One Drivers’ Championship.
The chart needs to show tension somehow, so in this first really quick and simple rough sketch, you really do have to put yourself in the graph and start reading it from left to right:
The data is pulled in from the Ergast API as JSON data, which is then parsed and visualised using R:
require(RJSONIO) require(ggplot2) #initialise a data frame champ <- data.frame(round=numeric(), driverID=character(), position=numeric(), points=numeric(),wins=numeric(), stringsAsFactors=FALSE) #This is a fudge at the moment - should be able to use a different API call to #get the list of races to date, rather than hardcoding latest round number for (j in 1:18){ resultsURL=paste("http://ergast.com/api/f1/2012/",j,"/driverStandings",".json",sep='') print(resultsURL) results.data.json=fromJSON(resultsURL,simplify=FALSE) rd=results.data.json$MRData$StandingsTable$StandingsLists[[1]]$DriverStandings for (i in 1:length(rd)){ champ=rbind(champ,data.frame(round=j, driverID=rd[[i]]$Driver$driverId, position=as.numeric(as.character(rd[[i]]$position)), points=as.numeric(as.character(rd[[i]]$points)), wins=as.numeric(as.character(rd[[i]]$wins)) )) } } champ #Horrible fudge - should really find a better way of filtering? test2=subset(champ,( driverID=='vettel' | driverID=='alonso' | driverID=='raikkonen'|driverID=='webber' | driverID=='hamilton'|driverID=='button' )) #Really rough sketch, in part inspired by http://junkcharts.typepad.com/junk_charts/2012/11/the-electoral-map-sans-the-map.html ggplot(test2)+geom_line(aes(x=round,y=points,group=driverID,col=driverID))+labs(title="F1 2012 - Race to the Championship") #I wonder if it would be worth annotating the chart with labels explaining any DNF reasons at parts where points stall?
So, that’s the quickest and dirtiest chart I could think of – where to take this next? One way would be to start making the chart look cleaner; another possibility would be to start looking at adding labels, highlights, and maybe pushing all but ALO and VET into the background? (GDS do some nice work in this vein, eg Updating the GOV.UK Performance Dashboard; this StoryTellingWithData post on stacked bar charts also has some great ideas about how to make simple, clean and effective use of text and highlighting…).
Let’s try cleaning it up a little, and then highlight the championship contenders?
test3=subset(test,( driverID=='vettel' | driverID=='alonso' )) test4=subset(test,( driverID=='raikkonen'|driverID=='webber' | driverID=='hamilton'|driverID=='button' )) ggplot(test4) + geom_line(aes(x=round,y=position,group=driverID),col='lightgrey') + geom_line(data=test3,aes(x=round,y=position,group=driverID,col=driverID)) + labs(title="F1 2012 - Race to the Championship")
Hmm… I’m not sure about those colours? Maybe use Blue for VET and Red for ALO?
I really hacked the path to this – there must be a cleaner way?!
ggplot(test4)+geom_line(aes(x=round,y=points,group=driverID),col='lightgrey') + geom_line(data=subset(test3,driverID=='vettel'),aes(x=round,y=points),col='blue') + geom_line(data=subset(test3,driverID=='alonso'),aes(x=round,y=points),col='red') + labs(title="F1 2012 - Race to the Championship")
Other chart types are possible too, I suppose? Such as something in the style of a lap chart?
ggplot(test2)+geom_line(aes(x=round,y=position,group=driverID,col=driverID))+labs(title="F1 2012 - Race to the Championship")
Hmmm… Just like the first sketch, this one is cluttered and confusing too… How about if we clean it as above to highlight just the contenders?
ggplot(test4) + geom_line(aes(x=round,y=points,group=driverID),col='lightgrey') + geom_line(data=test3,aes(x=round,y=points,group=driverID,col=driverID)) + labs(title="F1 2012 - Race to the Championship")
A little cleaner, maybe? And with the colour tweak:
ggplot(test4) + geom_line(aes(x=round,y=position,group=driverID),col='lightgrey') + geom_line(data=subset(test3,driverID=='vettel'),aes(x=round,y=position),col='blue') + geom_line(data=subset(test3,driverID=='alonso'),aes(x=round,y=position),col='red') + labs(title="F1 2012 - Race to the Championship")
Something that really jumps out at me in this chart are the gridlines – they really need fixing? But what would be best to show?
Hmm, before we do that, how about an animation? (Does WordPress.com allow animated gifs?)
Here’s the code (it requires the animation package):
library(animation) race.ani= function(...) { for (i in 1:18) { g=ggplot(subset(test3, round<=i)) + geom_line(aes(x=round,y=position,group=driverID),col='lightgrey')+geom_line(data=subset(test3,driverID=='vettel' & round<=i),aes(x=round,y=position),col='blue')+geom_line(data=subset(test3,driverID=='alonso' & round <=i),aes(x=round,y=position),col='red')+labs(title="F1 2012 - Race to the Championship")+xlim(1,18) print(g) } } saveMovie(race.ani(), interval = 0.4, outdir = getwd())
And for the other chart:
Hmmm…
How’s about another sort of view – the points difference between VET and ALO?
alo=subset(test3,driverID=='alonso') vet=subset(test3,driverID=='vettel') colnames(vet)=c("round","driverID","vposition","vpoints","vwins") colnames(alo)=c("round","driverID","aposition","apoints","awins") cf= merge(alo,vet,by=c('round')) ggplot(cf) + geom_bar( aes(x=round,y=vpoints-apoints,fill=(vpoints-apoints)>0), stat='identity') + labs(title="F1 2012 Championship - VET vs ALO")
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.