<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-with-various-more-awkward-data/"></div></div><div><a data-url="https://www.r-bloggers.com/dual-axes-time-series-plots-with-various-more-awkward-data/" data-counturl="https://www.r-bloggers.com/dual-axes-time-series-plots-with-various-more-awkward-data/" data-count="horizontal" data-text="Dual axes time series plots with various more awkward data" 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/28/dualaxes2"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In my most <a target="_blank" href="http://ellisp.github.io/blog/2016/08/18/dualaxes">recent blog post</a> I introduced the <code>dualplot()</code> R function, which allows you to create time series plots with two different scales on the vertical axes in a way that minimises the potential problems of misinterpretation. See that earlier post for a discussion of the pros and cons of the whole approach, which I won’t repeat here.</p>
<p>I’ve now made some minor enhancements:</p>
<ul>
<li>improved handling of series that start at different times</li>
<li>fixed some minor problems with series that have different frequencies (eg monthly versus quarterly)</li>
<li>added a fairly sensible default choice of axes for when one or both of the series dip into negative territory</li>
<li>improved default legend and axis titles for situations when the data are columns in a data frame</li>
<li>improved x axis labels for when x is a an object with date or time characteristics</li>
</ul>
<p>The main idea behind my first version was that the two series would be rendered on the screen as though they had first been converted to indexes (like the Consumer Price Index), and then have the original scales restored so the original absolute values can still be seen. The choice to be made then becomes which point to choose as the point to make the series use as their reference (eg the period at which they are defined as 1, 100 or 1000, if we were to leave them as indices).</p>
<h2>When time series start at different times</h2>
<h3>Default to use first available “cross over point”</h3>
<p>For the (common) situation that the two series you wish to plot start at different times, I decided it rarely made sense to have both series <strong>start</strong> at the same vertical position on the page at their respective earliest point. So I’ve set the default to make the two series appear as though their reference period is the <strong>first time period when both series have data</strong>. In effect, this makes the later series appear to grow out of the first series, and you can see its relative growth from that period onwards.</p>
<p>This is illustrated nicely in the first plot below, which shows how the share price of <a target="_blank" href="https://www.nzx.com/companies/FCG">Fonterra Cooperative Group</a> has performed performed relative to BHP Billiton. For non-Australasian readers, Fonterra (who’s historical origin is as a dairy farming cooperative) is one of the most important economic enterprises in New Zealand; and BHP is an <a target="_blank" href="https://en.wikipedia.org/wiki/BHP_Billiton">Anglo-Australian multinational mining giant</a>.</p>
<p><img alt="starts2" src="http://ellisp.github.io/img/0052-starts2.svg"></p>
<p>As well as seeing the relative growth since data on both series has become available, we can still see the approximate absolute values, eg just under $6 for a Fonterra Cooperative Group share.</p>
<p>Readers who have been paying attention will notice that in drawing this plot I appear to have broken one of my own rules on when to use a dual axes graphic, which was to <strong>not</strong> use two axes when the unit of both series is fundamentally the same. In this case, both series are showing dollars per share. I had a hard think about this and think that this is (marginally) a case where it is acceptable to use two different axes. While the <strong>units</strong> are the same (dollars per share), the magnitudes are sufficiently different that it is worthwhile, if we wish to compare growth paths (typically the issue of most interest with shares), to examine them on different scales.</p>
<p>Here’s the code to load that data from Yahoo Finance and draw the chart with my <code>dualplot()</code> function:</p>
<figure>
<pre><code data-lang="r"><span># Load up functionality
</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>quantmod</span><span>)</span><span> </span><span># for getSymbols for share price data
</span><span>library</span><span>(</span><span>ggseas</span><span>)</span><span> </span><span># for nzbop data
</span><span>
</span><span># The dualplot() function:
</span><span>source</span><span>(</span><span>&quot;https://gist.githubusercontent.com/ellisp/4002241def4e2b360189e58c3f461b4a/raw/9ab547bff18f73e783aaf30a7e4851c9a2f95b80/dualplot.R&quot;</span><span>)</span><span> </span><span>#=================Different starting points==================
# Load some example data from the stock exchange
# Fonterra data only available from Yahoo for the past few years
</span><span>fonterra</span><span> </span><span>&lt;-</span><span> </span><span>getSymbols</span><span>(</span><span>'FCG.NZ'</span><span>,</span><span> </span><span>src</span><span>=</span><span>'yahoo'</span><span>,</span><span> </span><span>auto.assign</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>bhp</span><span> </span><span>&lt;-</span><span> </span><span>getSymbols</span><span>(</span><span>'BHP.AX'</span><span>,</span><span> </span><span>src</span><span>=</span><span>'yahoo'</span><span>,</span><span> </span><span>auto.assign</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span># default - a cross over point happen at the earliest point of the shorter series
</span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>bhp</span><span>),</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>bhp</span><span>$</span><span>BHP.AX.Close</span><span>,</span><span>
</span><span>x</span><span>2</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>fonterra</span><span>),</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>fonterra</span><span>$</span><span>FCG.NZ.Close</span><span>,</span><span> </span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;BHP&quot;</span><span>,</span><span> </span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Fonterra&quot;</span><span>,</span><span> </span><span>legx</span><span> </span><span>=</span><span> </span><span>&quot;topright&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Price per share of two key firms&quot;</span><span>)</span></code></pre>
</figure>
<p>Note that this is a common case where both series would have been converted to an index somehow in many graphics. The absolute value of share prices is not meaningful to most people so there is little lost in converting them to indices. However, they do mean something for people who are actively concerned with them, so there is value in my dual axis approach which retains the absolute value of the two series while preserving the non-misleading characteristics of the index-based approach.</p>
<h3>Starting at the same vertical position</h3>
<p>Sometime it might make sense, depending on the data and the purpose of the graphic, to start the later series at the same vertical height as the first series. This is equivalent visually to plots of indexes where the reference point for each series is the first time period of the series. The tweaked version of <code>dualplot()</code> lets the user specify this (or more complex) combinations of reference points.</p>
<p><img alt="starts3" src="http://ellisp.github.io/img/0052-starts3.svg"></p>
<figure>
<pre><code data-lang="r"><span># or can override eg - each one begins at the same vertical height on its earliest point
</span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>bhp</span><span>),</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>bhp</span><span>$</span><span>BHP.AX.Close</span><span>,</span><span>
</span><span>x</span><span>2</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>fonterra</span><span>),</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>fonterra</span><span>$</span><span>FCG.NZ.Close</span><span>,</span><span> </span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;BHP&quot;</span><span>,</span><span> </span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Fonterra&quot;</span><span>,</span><span> </span><span>legx</span><span> </span><span>=</span><span> </span><span>&quot;topright&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Price per share of two key firms\n(starting at same vertical position)&quot;</span><span>,</span><span>
</span><span>ylim.ref</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>silent</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<h3>Converging to a common vertical position</h3>
<p>… or you might want to adopt a teleological view of history and and see how growth in the past has led to the latest point:</p>
<p><img alt="starts4" src="http://ellisp.github.io/img/0052-starts4.svg"></p>
<p>In this case, the last two charts are almost identical, but that’s not always going to be the result. In this particular case, these two charts being similar just means the decline in BHP Billiton share price from the beginning of the chart to the end of the chart is roughly the same (a bit over 10%) as the decline in Fonterra Cooperative Group share price from the end of 2012 to the end of the chart.</p>
<figure>
<pre><code data-lang="r"><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>bhp</span><span>),</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>bhp</span><span>$</span><span>BHP.AX.Close</span><span>,</span><span>
</span><span>x</span><span>2</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>fonterra</span><span>),</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>fonterra</span><span>$</span><span>FCG.NZ.Close</span><span>,</span><span> </span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;BHP&quot;</span><span>,</span><span> </span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Fonterra&quot;</span><span>,</span><span> </span><span>legx</span><span> </span><span>=</span><span> </span><span>&quot;topright&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Price per share of two key firms\n(finishing at same vertical position)&quot;</span><span>,</span><span>
</span><span>ylim.ref</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>nrow</span><span>(</span><span>bhp</span><span>),</span><span> </span><span>nrow</span><span>(</span><span>fonterra</span><span>)))</span></code></pre>
</figure>
<h2>Time series that have different frequencies</h2>
<p>I’ve tested the <code>dualplot()</code> function more thoroughly against data where the two series have different frequencies. Here’s an example comparing a longer series of Air New Zealand share prices against New Zealand’s national exports of services (which includes Air New Zealand sales to foreigners).</p>
<p><img alt="frequency" src="http://ellisp.github.io/img/0052-frequency1.svg"></p>
<p>We can see that both series took a hit from the Global Financial crisis with a dip in the years after 2008; but the Air New Zealand share price was impacted much more severely than were New Zealand service exports as a whole (which of course comprise multiple industries and firms).</p>
<p>To do this I used an old copy of New Zealand balance of payments data that is one of the example datasets in the <a target="_blank" href="https://cran.r-project.org/web/packages/ggseas/index.html"><code>ggseas</code> R package</a> for seasonal adjustment on the fly. The data are not meant to be up to date, they just illustrate superimposing quarterly data on daily data.</p>
<p>There’s nothing special about the call to <code>dualplot()</code> to do this so I used this as a chance to show some of the optional features of the function; such as drawing gridlines (white ones in this case) and passing arguments through to <code>legend()</code> to colour the space behind the legend white.</p>
<figure>
<pre><code data-lang="r"><span>airnz</span><span> </span><span>&lt;-</span><span> </span><span>getSymbols</span><span>(</span><span>'AIR.NZ'</span><span>,</span><span> </span><span>src</span><span>=</span><span>'yahoo'</span><span>,</span><span> </span><span>auto.assign</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>from</span><span> </span><span>=</span><span> </span><span>&quot;2000-01-01&quot;</span><span>)</span><span> </span><span>services</span><span> </span><span>&lt;-</span><span> </span><span>nzbop</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Category</span><span> </span><span>==</span><span> </span><span>&quot;Services; Exports total&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>TimePeriod</span><span> </span><span>></span><span> </span><span>as.Date</span><span>(</span><span>&quot;1997-12-30&quot;</span><span>))</span><span>
</span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>services</span><span>$</span><span>TimePeriod</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>services</span><span>$</span><span>Value</span><span>,</span><span> </span><span>x</span><span>2</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>airnz</span><span>),</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>airnz</span><span>$</span><span>AIR.NZ.Close</span><span>,</span><span>
</span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;New Zealand service exports ($m)\n&quot;</span><span>,</span><span> </span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Air New Zealand share price&quot;</span><span>,</span><span>
</span><span>yleg1</span><span> </span><span>=</span><span> </span><span>&quot;All service exports ($m) (left axis)&quot;</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>4</span><span>,</span><span>2</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;rosybrown&quot;</span><span>,</span><span> </span><span>&quot;steelblue&quot;</span><span>),</span><span>
</span><span>main</span><span> </span><span>=</span><span>&quot;New Zealand service exports and Air New Zealand share price over time&quot;</span><span>,</span><span>
</span><span>colgrid</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;o&quot;</span><span>,</span><span> </span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>box.col</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>)</span></code></pre>
</figure>
<p>For a second example, let’s go back to Fonterra, and this time compare their daily share price to weekly movements in the the world whole milk dairy powder price which I sourced from <a target="_blank" href="https://www.quandl.com/data/GDT/WMP_PI-Whole-Milk-Powder-Price-Indices">Quandl</a>.</p>
<p><img alt="frequency" src="http://ellisp.github.io/img/0052-milk-price.svg"></p>
<p>Having the scales carefully chosen to mimic indices means that we can see immediately the lower level of volatility in the share price than the dairy price; something that would be hidden by other visualisation methods such as showing each series on its own facet with a free y axis chosen to use the full plotting area.</p>
<figure>
<pre><code data-lang="r"><span>dairy</span><span> </span><span>&lt;-</span><span> </span><span>Quandl</span><span>(</span><span>&quot;GDT/WMP_PI&quot;</span><span>)</span><span>
</span><span>par</span><span>(</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>)</span><span>
</span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>dairy</span><span>[</span><span> </span><span>,</span><span> </span><span>1</span><span>],</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>dairy</span><span>[,</span><span> </span><span>8</span><span>],</span><span> </span><span>x</span><span>2</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>fonterra</span><span>),</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>fonterra</span><span>$</span><span>FCG.NZ.Close</span><span>,</span><span>
</span><span>ylab1</span><span> </span><span>=</span><span> </span><span>&quot;Whole milk powder price index\n&quot;</span><span>,</span><span>
</span><span>ylab2</span><span> </span><span>=</span><span> </span><span>&quot;Fonterra Cooperative Group\nshare price ($)&quot;</span><span>,</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>colorspace</span><span>::</span><span>rainbow_hcl</span><span>(</span><span>2</span><span>,</span><span> </span><span>c</span><span> </span><span>=</span><span> </span><span>80</span><span>,</span><span> </span><span>l</span><span> </span><span>=</span><span> </span><span>50</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>45</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>270</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;Fonterra share prices are less volatile than global milk prices&quot;</span><span>)</span></code></pre>
</figure>
<p>Before I move on, a note on colour. Usually I wouldn’t use so many palettes in a single document for basically the same purpose (identification between two qualitative variables). The effect is (to me) somewhat jarring. But as I’m demonstrating use of a new graphics function some of the normal requirements of good aesthetics are suspended.</p>
<h2>Negative data</h2>
<p>When data goes negative, graphing an index rarely makes sense. Most indexes are of data that is strictly positive, such as share prices or the consumer price index. This creates a new problem of what scale to use for a dual axis plot, and there is no clear answer, which makes me inclined to doubt that such a graphic is often justified. However, to get at least a reasonable default, I’ve set it so that if one of the series is negative the graphic will choose two vertical scales such that the plotting area includes the mean of each series plus three standard deviations in each direction.</p>
<p><img alt="negatives" src="http://ellisp.github.io/img/0052-negatives.svg"></p>
<p>Here’s the code for that example usage with negative data; I struggled to think of a good (non-misleading) use case with negative data so had to simulate two series:</p>
<figure>
<pre><code data-lang="r"><span>set.seed</span><span>(</span><span>123</span><span>)</span><span>
</span><span>data1</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>100</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>arima.sim</span><span>(</span><span>list</span><span>(</span><span>ar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.9</span><span>)),</span><span> </span><span>100</span><span>))</span><span>
</span><span>data2</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>100</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>arima.sim</span><span>(</span><span>list</span><span>(</span><span>ar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.9</span><span>)),</span><span> </span><span>100</span><span>)</span><span> </span><span>*</span><span> </span><span>50</span><span> </span><span>-</span><span> </span><span>3</span><span>)</span><span>
</span><span>dualplot</span><span>(</span><span>x</span><span>1</span><span> </span><span>=</span><span> </span><span>data1</span><span>$</span><span>x</span><span>,</span><span> </span><span>y</span><span>1</span><span> </span><span>=</span><span> </span><span>data1</span><span>$</span><span>y</span><span>,</span><span> </span><span>x</span><span>2</span><span>=</span><span> </span><span>data1</span><span>$</span><span>x</span><span>,</span><span> </span><span>y</span><span>2</span><span> </span><span>=</span><span> </span><span>data2</span><span>$</span><span>y</span><span>)</span></code></pre>
</figure>
<h2>But the dual axis approach has limitations</h2>
<p>Finally, as an offshoot of preparing this blog post, here’s a use case that reminded me that dual axes plots are very often not going to be a good graphic. They are cluttered, and can be hard to interpret.</p>
<p>A graphic that does not superimpose two data series in the same plotting area is best in this case. After various attempts to relate the volume of Air New Zealand share sales with prices, I ended up with this as my best graphic:</p>
<p><img alt="airnz" src="http://ellisp.github.io/img/0052-airnz.svg"></p>
<p>The vertical lines connecting the bursts of high sales volumes in the lower plot to the price changes in the upper plot work far better than superimposing the series (cluttered and hard to see connections); un-annotated facets; or a connected scatter plot (of either the original or transformed data or growth rates).</p>
<p>Here’s the code behind that graphic:</p>
<figure>
<pre><code data-lang="r"><span>airnz3</span><span> </span><span>&lt;-</span><span> </span><span>airnz</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>TimePeriod</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>airnz</span><span>),</span><span>
</span><span>PeakVol</span><span> </span><span>=</span><span> </span><span>AIR.NZ.Volume</span><span> </span><span>></span><span> </span><span>2</span><span> </span><span>*</span><span> </span><span>10</span><span> </span><span>^</span><span> </span><span>7</span><span>)</span><span> </span><span>peaks</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>airnz3</span><span>,</span><span> </span><span>PeakVol</span><span>)</span><span>$</span><span>TimePeriod</span><span>
</span><span>airnz3</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>TimePeriod</span><span>,</span><span> </span><span>AIR.NZ.Close</span><span>,</span><span> </span><span>AIR.NZ.Volume</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>SquareRootOfVolume</span><span> </span><span>=</span><span> </span><span>sqrt</span><span>(</span><span>AIR.NZ.Volume</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>ClosingPrice</span><span> </span><span>=</span><span> </span><span>AIR.NZ.Close</span><span>)</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>-</span><span>AIR.NZ.Volume</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>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>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>peaks</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;brown&quot;</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;&quot;</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Four big trading events for Air New Zealand shares since 2000&quot;</span><span>)</span></code></pre>
</figure>
<h2><code>dualplot()</code></h2>
<p>Here’s the code for the latest version of dualplot; it’s a Gist on GitHub. The code above includes an example line to source it directly into an R program.</p>
<p></p>
<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-with-various-more-awkward-data/"></div></div><div><a data-url="https://www.r-bloggers.com/dual-axes-time-series-plots-with-various-more-awkward-data/" data-counturl="https://www.r-bloggers.com/dual-axes-time-series-plots-with-various-more-awkward-data/" data-count="vertical" data-text="Dual axes time series plots with various more awkward data" 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/28/dualaxes2"> 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/aHJKawqr5jI"></a></div>