# From Points to (Messy) Lines

A week or so ago, I came up with a new chart type – race concordance charts – for looking at a motor circuit race from the on-track perspective of a particular driver. Here are a couple of examples from the 2017 F1 Grand Prix:

The gap is the time to the car on track ahead (negative gap, to the left) or behind (to the right). The colour indicates whether the car is on the same lap (light blue),  on the lap behind (orange to red), or a lap ahead (dark blue).

In the dots, we can “see” lines relating to the relative progress of particular cars. But what if we actually plot the progress of each of those other cars as a line? The colours represent different cars.

Here’s another view of the track from Hulkenberg’s perspective with a wider window, whoch by comparison with the previous chart suggests I need to handle better cars that do not drop off the track but do fall out of the display window… (At the moment, I only grab data for cars in the specified concordance window):

Note that we need to do a little bit of tidying up of the data so that we don’t connect lines for cars that flow off the left hand edge, for example, and then return several laps later from the right hand edge:

```#Get the data for the cars, as before
inscope=sqldf(paste0('SELECT l1.code as code,l1.acctime-l2.acctime as acctimedelta,
l2.lap-l1.lap as lapdelta, l2.lap as focuslap
FROM lapTimes as l1 join lapTimes as l2
WHERE l1.acctime < (l2.acctime + ', abs(limits[2]), ') AND l1.acctime > (l2.acctime - ', abs(limits[1]),')
AND l2.code="',code,'";'))

#If consecutive rows for same driver are on more than one focuslap apart, break the line
inscope=ddply(inscope,.(code),transform,g=cumsum(c(0,diff(focuslap)>1)))
#Continuous line segments have the same driver code and "group" number

g = ggplot(inscope)

#The interaction splits up the groups based on code and the contiguous focuslap group number
#We also need to ensure we plot acctimedelta relative to increasing focuslap
g=g+geom_line(aes(x=focuslap, y=acctimedelta, col=code,group=interaction(code, g)))
#...which means we then need to flip the axes
g=g+coord_flip()```

There may still be some artefacts in the line plotting based on lapping… I can’t quite think this through at the moment:-(