<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/from-points-to-messy-lines/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/"> Rstats – OUseful.Info, the blog…</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A week or so ago, I came up with a new chart type – <a target="_blank" rel="nofollow" href="https://blog.ouseful.info/2017/05/01/track-concordance-charts/">race concordance charts</a> – 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:</p>
<p><img sizes="(max-width: 602px) 100vw, 602px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_bot-1.png?w=450&amp;h=430&amp;fit=602%2C430" data-orig-size="1344,960" data-image-title="f1_2017_rus-trackconcordance_2017_RUS_BOT-1" class="wp-image-16229" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="16229" data-permalink="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/f1_2017_rus-trackconcordance_2017_rus_bot-1/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_bot-1.png?w=450&amp;h=430&amp;fit=602%2C430?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_bot-1.png?w=450&amp;h=430&amp;fit=602%2C430?w=830" data-image-meta="{" src="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_bot-1.png?w=450&h=430&fit=602%2C430"></p>
<p><img sizes="(max-width: 622px) 100vw, 622px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_hul-1.png?w=450&amp;h=444&amp;fit=622%2C444" data-orig-size="1344,960" data-image-title="f1_2017_rus-trackconcordance_2017_RUS_HUL-1" data-comments-opened="1" class="wp-image-16228" data-recalc-dims="1" data-attachment-id="16228" data-permalink="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/f1_2017_rus-trackconcordance_2017_rus_hul-1/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_hul-1.png?w=450&amp;h=444&amp;fit=622%2C444?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_hul-1.png?w=450&amp;h=444&amp;fit=622%2C444?w=830" data-image-meta="{" src="https://ouseful.files.wordpress.com/2017/05/f1_2017_rus-trackconcordance_2017_rus_hul-1.png?w=450&h=444&fit=622%2C444"> 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).</p>
<p>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.</p>
<p> </p>
<p><img sizes="(max-width: 477px) 100vw, 477px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/bot_conc_line2.png?w=456" data-orig-size="477,376" data-image-title="bot_conc_line" data-comments-opened="1" class="wp-image-16238" data-recalc-dims="1" data-attachment-id="16238" data-permalink="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/bot_conc_line/" alt="bot_conc_line" data-medium-file="https://ouseful.files.wordpress.com/2017/05/bot_conc_line2.png?w=456?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/bot_conc_line2.png?w=456?w=477" data-image-meta="{" src="https://ouseful.files.wordpress.com/2017/05/bot_conc_line2.png?w=456"><img sizes="(max-width: 477px) 100vw, 477px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/hul_conc_line2.png?w=456" data-orig-size="477,376" data-image-title="HUL_conc_line" data-comments-opened="1" class="wp-image-16239" data-recalc-dims="1" data-attachment-id="16239" data-permalink="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/hul_conc_line/" alt="HUL_conc_line" data-medium-file="https://ouseful.files.wordpress.com/2017/05/hul_conc_line2.png?w=456?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/hul_conc_line2.png?w=456?w=477" data-image-meta="{" src="https://ouseful.files.wordpress.com/2017/05/hul_conc_line2.png?w=456"></p>
<p>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):</p>
<p><img sizes="(max-width: 477px) 100vw, 477px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/hul-conc_line2.png?w=456" data-orig-size="477,376" data-image-title="HUL-conc_line2" data-comments-opened="1" class="wp-image-16241" data-recalc-dims="1" data-attachment-id="16241" data-permalink="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/hul-conc_line2/" alt="HUL-conc_line2" data-medium-file="https://ouseful.files.wordpress.com/2017/05/hul-conc_line2.png?w=456?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/hul-conc_line2.png?w=456?w=477" data-image-meta="{" src="https://ouseful.files.wordpress.com/2017/05/hul-conc_line2.png?w=456"></p>
<p>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:</p>
<pre>#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 &lt; (l2.acctime + ', abs(limits[2]), ') AND l1.acctime > (l2.acctime - ', abs(limits[1]),')
                       AND l2.code=&quot;',code,'&quot;;'))

  #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 &quot;group&quot; 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()</pre>
<p>There may still be some artefacts in the line plotting based on lapping… I can’t quite think this through at the moment:-(</p>
<p>So here’s my reading:</p>
<ul>
<li>near horizontal lines that go slightly up and to the right, and where a lot of places in the window are lost in a single lap are a result of pit stop by the car that lost the places; if we have access to pit information, we could perhaps dot these lines?</li>
<li>the “waist” in the chart for HUL shows cars coming together for a safety car, and then HUL losing pace to some cars whilst making advances on others;</li>
<li>lines with a constant gradient show a  consistent gain or loss of time, per lap, over several laps;</li>
<li>a near vertical line shows a car keeping pace, and neither making nor losing time compared to the focus car.</li>
</ul>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/16227/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/16227/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.ouseful.info/2017/05/08/from-points-to-messy-lines/"> Rstats – OUseful.Info, the blog…</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/lje-zEzVrxM"></a></div>