<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/dual-axes-time-series-plots-may-be-ok-sometimes-after-all/"></div></div><div><a data-url="https://www.r-bloggers.com/dual-axes-time-series-plots-may-be-ok-sometimes-after-all/" data-counturl="https://www.r-bloggers.com/dual-axes-time-series-plots-may-be-ok-sometimes-after-all/" data-count="horizontal" data-text="Dual axes time series plots may be ok sometimes after all" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2016/08/18/dualaxes"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Are they really as bad as all that?</h2>
<p>I’ve been mulling over time series charts with two different y axes, which are widely deprecated in the world of people who see ourselves as professional data workers. Looking down on dual axis time series charts is one of the things that mark one as a member of a serious data visualiser – after shaking our heads at pie charts, and cringing in horror at three-dimensional chart junk. But I’ve come to the surprising conclusion (surprising for me) that the arguments against them don’t stack up – at least not to the stage of justifying a blanket ban.</p>
<p>Before I go further, here’s my best example of one of these charts with real data, as a talking point. The data come from the <a target="_blank" href="http://www.rbnz.govt.nz/statistics/key-graphs/key-graph-exchange-rate">Reserve Bank of New Zealand</a>, and my chart below is essentially a version of their graphic that I’ve enhanced to meet my own style requirements. For the record, my improvement steps were:</p>
<ul>
<li>choose vertical scales that make the two series as comparable as possible, equivalent graphically to how they would be drawn it they were indexed rather than original values</li>
<li>remove distracting shadows</li>
<li>remove borders</li>
<li>make the data stand out more</li>
<li>colour code the axes to match the data series, using colours with equal chroma and luminance so they are perceptually equal</li>
</ul>
<p><img alt="dual" src="http://ellisp.github.io/img/0051-dualgood.svg"></p>
<p>Stephen Few looks at the issue of dual axes plots in <a target="_blank" href="https://www.perceptualedge.com/articles/visual_business_intelligence/dual-scaled_axes.pdf">a well reasoned piece</a> and concludes cautiously that they are never justified. A <a target="_blank" href="https://kieranhealy.org/blog/archives/2016/01/16/two-y-axes/">nice piece by Keran Healy</a> looks at a particular example and concludes that the dual axes encourage sloppy thinking with regard to time series causality. There are a lot of shorter pieces such as <a target="_blank" href="http://junkcharts.typepad.com/junk_charts/2006/05/the_crossover_l.html">this one</a> pouring scorn on the technique too. Hadley Wickham is <a target="_blank" href="https://www.reddit.com/r/dataisbeautiful/comments/3mp9r7/im_hadley_wickham_chief_scientist_at_rstudio_and/">famously opposed to dual axes</a> and has made it difficult to use them in ggplot2 (I have to say, in the case of <a target="_blank" href="https://rpubs.com/kohske/dual_axis_in_ggplot2">this example</a> I agree with Professor Wickham’s comment “MY EYES. MY EYES. OH THE HUMANITY”).</p>
<p>Here’s my attempt at a paraphrase of the case against dual axis charts:</p>
<ol>
<li>The designer has to make choices about scales and this can have a big impact on the viewer</li>
<li>In particular, “cross-over points” where one series cross another are results of the design choices, not intrinsic to the data, and viewers (particularly unsophisticated viewers) will not appreciate this and think there is more significance in cross over than is actually the case</li>
<li>They make it easier to lazily associate correlation with causation, not taking into account autocorrelation and other time-series issues</li>
<li>Because of the issues above, in malicious hands they make it possible to deliberately mislead</li>
<li>They often look cluttered and aesthetically unpleasing</li>
</ol>
<p>All of these points have some substance, but having carefully thought it through I think that they are not conclusive. I reached this position not by disagreeing with the statements above, but by observing that the same criticisms can be made of most if not all standard methods of representing the sort of data for which a dual axis time series chart would be considered. Most importantly, in preparing this post I came up with the idea (surely not original to me but I can’t find the prior references) that one can mitigate many of the problems of dual axes time series plots by choosing the limits of the two y axes in such a way that the two series are rendered on the page <em>as though they had been converted to indices first</em>.</p>
<p>Let’s look at the alternatives to dual axes charts in the case of the foreign exchange data plotted earlier.</p>
<h2>Facets can sometimes make comparisons more difficult</h2>
<p>The most commonly recommended alternative to dual axes is a free scaled facet plot like this one:</p>
<p><img alt="facets" src="http://ellisp.github.io/img/0051-facets.svg"></p>
<p>This is a good graphic, no doubt about it, and I think will often be my preferred approach to this type of data. Interestingly, this is still basically a dual axis plot. We’ve let the vertical axes take two different scales, but by displacing the two facets from eachother we avoid problems like the false “cross-over” points, and we improve the uncluttered aesthetic appeal. However, it’s worth noting that the choice of scale is still a design choice. This graphic, drawn with <code>ggplot2</code> which has good defaults and does not allow easy configuration of faceted vertical axes, rightly chooses vertical scales to maximise use of the page – but the difficulty in over-riding that is a <code>ggplot</code> feature, not a feature of faceted scales in general.</p>
<p>Because the choice of scale is a choice by the designer, objections 1 and 4 (arbitrary differing scales, and vulnerable to malicious misleading) apply to this sort of graphic as much as a dual axis plot. Consider this alternative version:</p>
<p><img alt="facets-bad" src="http://ellisp.github.io/img/0051-facets-bad.svg"></p>
<p>It’s a similar plot; I’ve just changed the vertical limits of the bottom of the two charts. I had to exit the facet paradigm of <code>ggplot2</code> to do this because it rightly makes it difficult to warp the y axes of individual facets; but with a little effort I managed to make it look to a naive viewer as though the US exchange rate is more volatile than the trade weighted index.</p>
<p>What the facet version <em>does</em> do is make it harder to do micro comparisons. For example, compared to my original superimposed dual axis plot, it’s no longer possible to tell in either of the facet versions the periods when changes to the trade weighted index preceded those to the US exchange rate and vice versa.</p>
<p>Finally, splitting the plot into facets does precisely nothing about avoiding problem #3. The fact is, confusing correlation with causation is rife, and making the correlation less obvious is not a sustainable solution.</p>
<h2>Indexes don’t solve the problem fully</h2>
<p>Here’s a second common way of dealing with this problem – converting both variables to a common index so they can use the same scale. In fact, I’m the author of the ‘stat_index()’ function in the <a target="_blank" href="https://cran.r-project.org/web/packages/ggseas/index.html"><code>ggseas</code> package</a> which makes it easy to do this in the <code>ggplot2</code> universe, and I’m a fan of this approach in the right circumstance too. Here’s how it looks with this data:</p>
<p><img alt="index" src="http://ellisp.github.io/img/0051-index1.svg"></p>
<p>[By the way, if the shape of this graphic looks familiar, that’s because it is basically identical to my first dual axis chart, which I started the blog post with. I chose the range of the scales shown by my two dual axes in that chart in such a way that the two series would be drawn effectively identically to this index plot.]</p>
<p>With this indexing, we’ve now solved problem #1 about the arbitrary scaling of the axes. Unlike in the standard dual (free) axis version or its close cousin the free vertical axis facet plot, there is now a natural vertical scale which we can’t change for just one data series at a time. However, problems 2, 3, 4, 5 are not addressed in any way by this. Consider how the choice of reference period for the index impacts on the crossover point of the series, as shown in this alternative:</p>
<p><img alt="index360" src="http://ellisp.github.io/img/0051-index360.svg"></p>
<p>This revised plot is still ok, but an unsophisticated viewer might see it as telling a different story from the first in terms of which series “catches up” with the other and when (the cross-over objection again). By constraining ourselves to an index we leave a few less options for arbitrarily changing scales in misleading ways, but we still to need to make design choices that materially impact on the look of the data.</p>
<p>You might think the answer is “always make the earliest point in the series the reference point of the index”, so these plots are always showing growth from the beginning of time, but it doesn’t take long to think of times when that’s going to be an unhelpful restriction.</p>
<p>In converting to an index we haven’t addressed the problem of spurious correlation (of course not – how could a graphic do that anyway, except by failing to show the patterns?). We have still left the designer in charge of key decision about cross over of the series. And in the process, we have taken a <strong>decisive hit in interpretability by losing the original scale</strong>. Many of my stakeholders hate indexes with a passion, because of their abstract nature and the difficulty in explaining how they relate to units they are familiar with.</p>
<p>Below, I suggest a method of dual axis plots that has the non-misleading scale properties of an index chart while retaining interpretability.</p>
<h2>Connected scatterplots are cluttered, and too complex for most audiences</h2>
<p>For analytical purposes I think a connected scatter plot should be the standard choice for examining two continuous variables change over time (but see below for caveats on this). Here’s the continuous scatter plot in action for this particular dataset:</p>
<p><img alt="csp" src="http://ellisp.github.io/img/0051-csp.svg"></p>
<p>The strengths and weakenesses of this are apparent. It’s a good plot for seeing the relationship of the two (although a connected scatter plot of the first differences, or growth rates, is often better) but it’s cluttered; the data is drawn over itself as time goes on; and its going to be an <strong>unfamiliar visual shock</strong> for most non-specialist audiences. It gets worse with longer time series and with seasonal data. This is a good plot for the exploratory and analytical phases of data analysis but unlikely to be a final presentation graphic.</p>
<h2>How <em>not</em> to use dual axes</h2>
<p>Before I go on to good practice, let’s clear up some potential straw men. Here’s some examples of bad practice that I agree with critics should be very definitely avoided.</p>
<h3>Don’t muck up one of the scales</h3>
<p><img alt="bad1" src="http://ellisp.github.io/img/0051-dualbad1.svg"></p>
<p><img alt="bad2" src="http://ellisp.github.io/img/0051-dualbad2.svg"></p>
<p>Those are both instances of arbitrarily playing with the scales of one of the axes to use less vertical space and hence artificially misleading – it looks like one of the series has less variance than the other. As I pointed out earlier, this can be done even if you have two separate charts; I would argue that the dual axes in fact is revealed as having power precisely because the misleading effect is so important when done with the data series crossing eachother. With great data comes great responsibility. In this case, the responsibility is to use a justifiable, objective-if-possible, algorithm to set the scales of the two vertical axes.</p>
<h3>Don’t use two completely conceptually different scales (and in particular, <em>don’t</em> scrawl growth rates all over the original data)</h3>
<p>Here’s another case that I think is probably never justified, although it is very commonly used:</p>
<p><img alt="bad3" src="http://ellisp.github.io/img/0051-dualbad3.svg"></p>
<p>I hate this format that overlays growth rates on the original data – I find it a crime against aesthetics to start with, but more importantly I find it very difficult to read and I’m sure that’s the case for many viewers. When the scales are so different I see no justification for overlaying the two data series – use two facets in this case.</p>
<h2>The choice to use them</h2>
<p>Here’s what I think should be taken into account in the choice to use dual axis time series charts:</p>
<ul>
<li>As Few suggests, the case to use these charts is strongest when the eye is drawn to the pattern over time of an individual series ie horizontal rather than vertical comparisons. Vertical comparisons between the two series are misleading and need to be discouraged.</li>
<li><em>Don’t</em> use two axes when the unit of measure is the same (eg pass rates out of 100 in exams in two subjects). There’s just too much scope for confusion. Use a single axis.</li>
<li><em>Don’t</em> use two axes the scales are fundamentally different (eg temperature, and change in temperature). Use two charts (or two facets, depending on how you look at it).</li>
<li><em>Do</em> <strong>consider</strong> their use when the scales are comparable (eg two things in which getting larger on the graphic has a similar interpretation – like the two exchange rate variables in my examples)</li>
<li>Use dual axes rather than an index when the interpretability cost of converting to a common index is greater than acceptable (sorry, I don’t have a guideline for when that would be….)</li>
<li>Don’t use when you really have three data series to show, not just two (this should be obvious, but…)</li>
</ul>
<h2>Good practice when you do use them</h2>
<p>In general, playing around with these has led me to the following conclusions for good dual axes charts:</p>
<ul>
<li>Use a <strong>consistent process to choose the limits of the two vertical axes</strong>, an objective, automated algorithm if possible. One way of doing this, which I’ve made the default in my R function below, is to set the limits of the two axes so that the actual lines drawn <strong>look the same as if they were of indices</strong>, even though the scales give you actual values.</li>
<li>In particular, <strong>make the scales of the axes conceptually similar</strong>. Don’t show growth on one and original data on the other. Don’t use logarithms on one and natural scale on the other. And so on. If meeting this principle is difficult because of the complex concepts, measurement and scales you have, you probably don’t want to use dual axes.</li>
<li><strong>Colour-code</strong> the axes. This is the single biggest thing that would make most of the dual-axes graphics I see more readable. And even if you’re using Excel it should still be possible. I’m surprised it’s not standard (and believe me it isn’t even common).</li>
<li>Use colours with <strong>equal chroma and luminance</strong>, so only the hue varies. This gives equal perceptual weight to both the catogories.</li>
<li><strong>Avoid the acronyms “LHS” and “RHS”</strong> for left-hand side and right-hand side. This is exclusive jargon that puts off many users.</li>
</ul>
<h2>The <code>dualplot()</code> function</h2>
<p>To help me produce good quality dual axes time series charts with scales that aren’t misleading and good colour schemes, I built a little function in R.</p>
<p>The <code>dualplot()</code> function lets you choose the end points of the two vertical scales in such a way that the data are rendered on the screen as though they had been converted to an index. In fact, you can think of the result as a graphic of indexed series – as recommended by many critics of dual-axes plots – that just has the original scales back-transformed and added as annotations (this isn’t how it works under the hood but the effect is the same). The <code>ylim.ref</code> argument let’s you specify which element in the time series to use as the reference point for indexing (it defaults to 1, and won’t work for values other than 1 when the two series to be plotted are of different lengths).</p>
<p>Here’s an example output of <code>dualplot()</code> which replicates the visual impression of the index chart I used before with January 2014 as the reference point for indexing – this now becomes one of the crossover points of the two series, and the vertical scales are forced to be non-misleading (ie the same on the page as they would be for an index chart):</p>
<p><img alt="dual" src="http://ellisp.github.io/img/0051-dualgood2.svg"></p>
<p>You can load the <code>dualplot()</code> function by:</p>
<figure>
<pre><code data-lang="r"><span>source</span><span>(</span><span>&quot;https://gist.github.com/ellisp/4002241def4e2b360189e58c3f461b4a/raw/0e5dc52262e86b5d21b914db3553e58b0b8d1d20/dualplot.R&quot;</span><span>)</span><span> </span></code></pre>
</figure>
<p>…or here’s the original code:</p>
<p></p>
<p>You’re welcome to use it or adapt it but I can’t guarantee its stability just yet – I may change it as developing and testing continues. More thorough testing on a wider range of data is needed, but would make this blog post too long so I’ll get round to it in the next month or so.</p>
<h2>Code</h2>
<p>Here’s the code in R that downloaded the data and produced all the graphics above.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>readxl</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggseas</span><span>)</span><span> </span><span># for stat_index
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span>
</span><span># Download data from the Reserve Bank of New Zealand
</span><span>download.file</span><span>(</span><span>&quot;http://www.rbnz.govt.nz/-/media/ReserveBank/Files/Statistics/Key%20graphs/graphdata.xlsx?la=en&quot;</span><span>,</span><span>
</span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;rbnz.xlsx&quot;</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span># Import some of that data into R and create a numeric TimePeriod variable from the original
# string that shows year and month:
</span><span>forex</span><span> </span><span>&lt;-</span><span> </span><span>read_excel</span><span>(</span><span>&quot;rbnz.xlsx&quot;</span><span>,</span><span> </span><span>sheet</span><span> </span><span>=</span><span> </span><span>&quot;8_NZDUSD&quot;</span><span>,</span><span> </span><span>skip</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>year</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>substring</span><span>(</span><span>DATE</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>4</span><span>)),</span><span>
</span><span>month</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>substring</span><span>(</span><span>DATE</span><span>,</span><span> </span><span>6</span><span>,</span><span> </span><span>7</span><span>)),</span><span>
</span><span>TimePeriod</span><span> </span><span>=</span><span> </span><span>year</span><span> </span><span>+</span><span> </span><span>(</span><span>month</span><span> </span><span>-</span><span> </span><span>0.5</span><span>)</span><span> </span><span>/</span><span> </span><span>12</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>DATE</span><span>,</span><span> </span><span>-</span><span>year</span><span>,</span><span> </span><span>-</span><span>month</span><span>)</span><span>
</span><span># Tidy up names:
</span><span>names</span><span>(</span><span>forex</span><span>)[</span><span>1</span><span>:</span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;NZDUSD&quot;</span><span>,</span><span> </span><span>&quot;TWI&quot;</span><span>)</span><span>
</span><span># Create a long, thin (&quot;tidy&quot;) version for use with ggplot2:
</span><span>forex_m</span><span> </span><span>&lt;-</span><span> </span><span>forex</span><span> </span><span>%>%</span><span> </span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>TimePeriod</span><span>)</span><span> </span><span># Set the basic foundation of the coming ggplot graphics:
</span><span>basicplot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>forex_m</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>variable</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Data from RBNZ; graphic by http://ellisp.github.io&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>#-----------------facet versions-------------------
# Good facet plot:
</span><span>basicplot</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>variable</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Comparing two time series with facets may reduce comparability&quot;</span><span>)</span><span>
</span><span># Misleading facet plot from playing around with one of the scales:
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>forex_m</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>variable</span><span> </span><span>==</span><span> </span><span>&quot;NZDUSD&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;USD purchased for one NZD&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;NZDUSD&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>forex_m</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>variable</span><span> </span><span>!=</span><span> </span><span>&quot;NZDUSD&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Trade weighted index&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;TWI&quot;</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>#--------------index--------------
</span><span>
</span><span># Good index plot:
</span><span>basicplot</span><span> </span><span>+</span><span>
</span><span>stat_index</span><span>(</span><span>index.ref</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Index (January 1984 = 100)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Usually accepted version of comparing two time series&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Converted to an index, reference period first point in time&quot;</span><span>)</span><span>
</span><span># Also a good index plot, but showing that arbitrary choices are still being made:
</span><span>basicplot</span><span> </span><span>+</span><span> </span><span>stat_index</span><span>(</span><span>index.ref</span><span> </span><span>=</span><span> </span><span>361</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Index (January 2014 = 100)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;But then, a different picture?&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Converted to an index, reference period chosen arbitrarily later in the series&quot;</span><span>)</span><span>
</span><span>#---------------connected scatterplot-------------
</span><span>forex</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>round</span><span>(</span><span>TimePeriod</span><span> </span><span>-</span><span> </span><span>floor</span><span>(</span><span>TimePeriod</span><span>),</span><span> </span><span>3</span><span>)</span><span> </span><span>==</span><span> </span><span>0.042</span><span>,</span><span> </span><span>substring</span><span>(</span><span>TimePeriod</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>4</span><span>),</span><span> </span><span>&quot;&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span> </span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>NZDUSD</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>TWI</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>label</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_path</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>fontface</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_gradientn</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>colours</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;grey75&quot;</span><span>,</span><span> </span><span>&quot;darkblue&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Connected scatter plot may be the best analytically\nbut is intimidating to non-specialists&quot;</span><span>)</span><span>
</span><span>#------------dual axis version-------------
# As we're drawing a number of these, we want a function to make it easier.
# Here's one I prepared earlier:
</span><span>source</span><span>(</span><span>&quot;https://gist.github.com/ellisp/4002241def4e2b360189e58c3f461b4a/raw/0e5dc52262e86b5d21b914db3553e58b0b8d1d20/dualplot.R&quot;</span><span>)</span><span> </span><span># bad:
</span><span>with</span><span>(</span><span>forex</span><span>,</span><span> </span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>NZDUSD</span><span>,</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>TWI</span><span>,</span><span> </span><span>colgrid</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>,</span><span> </span><span>ylim2</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>20</span><span>,</span><span> </span><span>200</span><span>)))</span><span>
</span><span># bad:
</span><span>with</span><span>(</span><span>forex</span><span>,</span><span> </span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>NZDUSD</span><span>,</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>TWI</span><span>,</span><span> </span><span>colgrid</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>,</span><span> </span><span>ylim1</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>1</span><span>)))</span><span>
</span><span># verybad:
</span><span>forex2</span><span> </span><span>&lt;-</span><span> </span><span>forex</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>NZDUSD_growth</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>NA</span><span>,</span><span> </span><span>diff</span><span>(</span><span>NZDUSD</span><span>))</span><span> </span><span>/</span><span> </span><span>NZDUSD</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span>
</span><span>with</span><span>(</span><span>forex2</span><span>[</span><span>-1</span><span>,</span><span> </span><span>],</span><span> </span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>NZDUSD</span><span>,</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>NZDUSD_growth</span><span>,</span><span> </span><span>colgrid</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>,</span><span> </span><span>ylim2</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-15</span><span>,</span><span> </span><span>15</span><span>)))</span><span> </span><span># ok:
</span><span>with</span><span>(</span><span>forex</span><span>,</span><span> </span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>NZDUSD</span><span>,</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>TWI</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>1.2</span><span>,</span><span> </span><span>colgrid</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;NZ dollar exchange rate &amp; trade-weighted index&quot;</span><span>,</span><span>
</span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;NZ dollars for one US dollar&quot;</span><span>,</span><span>
</span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Index&quot;</span><span>,</span><span>
</span><span>yleg1</span><span> </span><span>=</span><span> </span><span>&quot;NZD / USD exchange rate (left axis)&quot;</span><span>,</span><span>
</span><span>yleg2</span><span> </span><span>=</span><span> </span><span>&quot;Trade-weighted index (right axis)&quot;</span><span>,</span><span>
</span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>5</span><span>,</span><span>6</span><span>,</span><span>3</span><span>,</span><span>6</span><span>)))</span><span>
</span><span>mtext</span><span>(</span><span>side</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>&quot;Data from RBNZ; graphic by http://ellisp.github.io&quot;</span><span>,</span><span> </span><span>adj</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>line</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span># ok again, equivalent to reference point for indexing of January 2014
</span><span>with</span><span>(</span><span>forex</span><span>,</span><span> </span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>NZDUSD</span><span>,</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>TWI</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>1.2</span><span>,</span><span> </span><span>colgrid</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;NZ dollar exchange rate &amp; trade-weighted index&quot;</span><span>,</span><span>
</span><span>ylim.ref</span><span> </span><span>=</span><span> </span><span>361</span><span>,</span><span> </span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;NZ dollars for one US dollar&quot;</span><span>,</span><span>
</span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Index&quot;</span><span>,</span><span>
</span><span>yleg1</span><span> </span><span>=</span><span> </span><span>&quot;NZD / USD exchange rate (left axis)&quot;</span><span>,</span><span>
</span><span>yleg2</span><span> </span><span>=</span><span> </span><span>&quot;Trade-weighted index (right axis)&quot;</span><span>,</span><span>
</span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>5</span><span>,</span><span>6</span><span>,</span><span>3</span><span>,</span><span>6</span><span>)))</span><span>
</span><span>mtext</span><span>(</span><span>side</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>&quot;Data from RBNZ; graphic by http://ellisp.github.io&quot;</span><span>,</span><span> </span><span>adj</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>line</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span>#--------tidy up-----------
</span><span>unlink</span><span>(</span><span>&quot;rbnz.xlsx&quot;</span><span>)</span></code></pre>
</figure>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/dual-axes-time-series-plots-may-be-ok-sometimes-after-all/"></div></div><div><a data-url="https://www.r-bloggers.com/dual-axes-time-series-plots-may-be-ok-sometimes-after-all/" data-counturl="https://www.r-bloggers.com/dual-axes-time-series-plots-may-be-ok-sometimes-after-all/" data-count="vertical" data-text="Dual axes time series plots may be ok sometimes after all" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2016/08/18/dualaxes"> Peter's stats stuff - R</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/7r1N9GKEOgI"></a></div>