Sunday, November 2, 2014

The 2014 Drivers' Championship Race Going in to Round 17

Some quick doodles for a new chapter of Wrangling F1 Data With R, looking at the state of the drivers' championship race as we go in to round 17.

#Load in the core utility functions to access ergast API

#Get the standings after each of the first 16 rounds of the 2014 season
for (j in seq(1,16)){
#Data is now in: df

Now we can have a look at the data. First, the race in the style of a lapchart, plotting the position standings after each round.


#The base chart
#Remove axis labels and colour legend
#Add a title
g=g+ggtitle("F1 Drivers' Championship Race, 2014")
#Add the line labels, resized (cex), and with an x-value offset
#Add right hand side padding to the chart so the labels don't overflow

This chart shows competition throughout the season  particularly between the first two places (Rosberg and Hamilton), fourth to sixth (Bottas, Vettel and Alonso), and ten, eleventh and twelfth (Magnussen, Perez and Raikkonen).

We can get a better feel for the competition in terms of the number of points separating the drivers.

#The only difference if to the base chart
#All the other elements of the chart definition are the same

(Note there is some occlusion of the labels which we would need to manage by hand using the directlabels dl.move() function applying the necessary vjust offset to each driverId group (e.g. alonso).)

Here we see how close fought the fourth to sixth battle has become, as the the points battle for tenth place. We also see a late season charge from Massa, who could still challenge Hulkenberg for eighth.

Let's annotate the chart a little more by placing a guideline showing between 10th and 11th positions.

#Generate a guide that is the mean points value of 10th and 11th positions
dfx=ddply(df[df['pos']==10 | df['pos']==11,],
  .(round), summarize, points=mean(points))

#Get the drivers fighting around 10th at the end of round 16
#Note that other drivers may have contended this position earlier in the season
df.battle=df[df$driverId %in% as.character(df[df$round==16 & df$pos>=10 & df$pos<=12,'driverId']),]
#Base chart
g=g+ylab(NULL)+xlab(NULL)+ggtitle("F1 Drivers' Championship Race, 2014")+guides(color=FALSE)

#Add in the guideline

Once again, we really need to tweak the label positions manually so that they are note overlapping if we want to use this chart as a presentation graphic.

Elements of this recipe may form part of a forthcoming chapter in the Wrangling F1 Data With R book.

No comments:

Post a Comment

There seem to be a few issues with posting comments. I think you need to preview your comment before you can submit it... Any problems, send me a message on twitter: @psychemedia