<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/figure-aesthetics-or-overlays/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.ouseful.info/2017/05/02/figure-aesthetics-or-overlays/"> 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>Tinkering with a <a target="_blank" rel="nofollow" href="https://blog.ouseful.info/2017/05/01/track-concordance-charts/">new chart type</a> over the weekend, I spotted something rather odd in in my F1 track history charts – what look to be outliers in the form of cars <em>that hadn’t been lapped on that lap</em> appearing <em>behind</em> the lap leader of the next lap, on track.</p>
<p><img sizes="(max-width: 600px) 100vw, 600px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/track_history-outlier.png?w=450#038;h=492" data-orig-size="1658,1360" data-image-title="track_history-outlier" class="wp-image-16208" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="16208" data-permalink="https://blog.ouseful.info/2017/05/02/figure-aesthetics-or-overlays/track_history-outlier/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/track_history-outlier.png?w=450#038;h=492?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/track_history-outlier.png?w=450#038;h=492?w=830" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://ouseful.files.wordpress.com/2017/05/track_history-outlier.png?w=450#038;h=492"></p>
<p>If you count the number of cars on that leadlap, it’s also greater than the number of cars in the race on that lap.</p>
<p>How could that be? Cars being unlapped, perhaps, and so “appearing twice” on a particular leadlap – that is, recording <em>two</em> laptimes between consecutive passes of the start/finish line by the race leader?</p>
<p><img sizes="(max-width: 207px) 100vw, 207px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/unlap.png?w=207&amp;resize=207%2C370#038;h=370" data-orig-size="470,840" data-image-title="unlap" data-comments-opened="1" class="wp-image-16206" data-recalc-dims="1" data-attachment-id="16206" data-permalink="https://blog.ouseful.info/2017/05/02/figure-aesthetics-or-overlays/unlap/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/unlap.png?w=207&amp;resize=207%2C370#038;h=370?w=168" data-large-file="https://ouseful.files.wordpress.com/2017/05/unlap.png?w=207&amp;resize=207%2C370#038;h=370?w=470" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://ouseful.files.wordpress.com/2017/05/unlap.png?w=207&resize=207%2C370#038;h=370"></p>
<p>My fix for this was to add an “unlap” attribute that detects whether</p>
<pre>#Overplot unlaps
lapTimes=ddply(lapTimes,.(leadlap,code),transform,unlap= seq_along(leadlap))</pre>
<p>This groups by leadlap an car, and counts 1 for each occurrence. So if the <code>unlap</code> count is <em>greater</em> than 1, a car a has completed more than 1 lap in a given leadlap.</p>
<p>My first thought was to add this as an <em>overprint</em> on the original chart:</p>
<pre>#Overprint unlaps
g = g + geom_point(data = lapTimes[lapTimes['unlap']>1,],
                   aes(x = trackdiff, y = leadlap, col=(leadlap-lap)), pch = 0)
</pre>
<p>This renders as follows:</p>
<p><img sizes="(max-width: 291px) 100vw, 291px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/unlapover.png?w=291&amp;resize=291%2C147#038;h=147" data-orig-size="392,198" data-image-title="unlapover" data-comments-opened="1" class="wp-image-16209" data-recalc-dims="1" data-attachment-id="16209" data-permalink="https://blog.ouseful.info/2017/05/02/figure-aesthetics-or-overlays/unlapover/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/unlapover.png?w=291&amp;resize=291%2C147#038;h=147?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/unlapover.png?w=291&amp;resize=291%2C147#038;h=147?w=392" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://ouseful.files.wordpress.com/2017/05/unlapover.png?w=291&resize=291%2C147#038;h=147"></p>
<p>Whilst it works, as an approach it is inelegant, and had me up in the night pondering the use of overlays rather than <em>aesthetics</em>.</p>
<p>Because we can also view the fact that the car was on its second pass of the start/finish line for a given lead lap as a key property of the car and depict that <em>directly</em> via an aesthetic mapping of that property onto the symbol type:</p>
<pre>  g = g + geom_point(aes( x = trackdiff, y = leadlap,
                          col = (lap == leadlap),
                          pch= (unlap==1) ))+scale_shape_identity()</pre>
<p>This renders just a single mark on the chart, depicting the diff to the leader *as well as * the unlapping characteristic, rather than the two marks used previously, one for the diff, the second, overprinting, mark to depict the unlapping nature of that mark.</p>
<p><img sizes="(max-width: 339px) 100vw, 339px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/track_history_outlier_pch.png?w=339&amp;resize=339%2C150#038;h=150" data-orig-size="534,236" data-image-title="track_history_outlier_pch" data-comments-opened="1" class="wp-image-16207" data-recalc-dims="1" data-attachment-id="16207" data-permalink="https://blog.ouseful.info/2017/05/02/figure-aesthetics-or-overlays/track_history_outlier_pch/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/track_history_outlier_pch.png?w=339&amp;resize=339%2C150#038;h=150?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/track_history_outlier_pch.png?w=339&amp;resize=339%2C150#038;h=150?w=534" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://ouseful.files.wordpress.com/2017/05/track_history_outlier_pch.png?w=339&resize=339%2C150#038;h=150"></p>
<p>So now I’m wondering – when <em>would</em> it make sense to use multiple marks by overprinting?</p>
<p>Here’s one example where I think it does make sense: where I pass an argument into the chart plotter to highlight a particular driver by infilling a marker with a symbol to identify that driver.</p>
<pre>#Drivers of interest passed in using construction: code=list(c(&quot;STR&quot;,&quot;+&quot;),c(&quot;RAI&quot;,&quot;*&quot;))
if (!is.na(code)){
  for (t in code) {
    g = g + geom_point(data = lapTimes[lapTimes['code'] == t[1], ],
                       aes(x = trackdiff, y = leadlap),
                       pch = t[2])
  }
}</pre>
<p><img sizes="(max-width: 345px) 100vw, 345px" data-image-description="" data-orig-file="https://ouseful.files.wordpress.com/2017/05/overprint1.png?w=345&amp;resize=345%2C73#038;h=73" data-orig-size="492,104" data-image-title="overprint" data-comments-opened="1" class="wp-image-16211" data-recalc-dims="1" data-attachment-id="16211" data-permalink="https://blog.ouseful.info/2017/05/02/figure-aesthetics-or-overlays/overprint/" alt="" data-medium-file="https://ouseful.files.wordpress.com/2017/05/overprint1.png?w=345&amp;resize=345%2C73#038;h=73?w=300" data-large-file="https://ouseful.files.wordpress.com/2017/05/overprint1.png?w=345&amp;resize=345%2C73#038;h=73?w=492" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://ouseful.files.wordpress.com/2017/05/overprint1.png?w=345&resize=345%2C73#038;h=73"></p>
<p>In this case, the + symbol is not a property of the car, it is an additional information attribute that I want to add to that car, but not the other cars. That is, it is a property of <em>my</em> interest, not a property of the car itself.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/16205/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/16205/" 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/02/figure-aesthetics-or-overlays/"> 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/Yyas83f1kWg"></a></div>