<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/hyetographs-hydrographs-and-highcharter/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nsaunders.wordpress.com/2017/02/07/hyetographs-hydrographs-and-highcharter/"> R – What You're Doing Is Rather Desperate</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Dual y-axes: yes or no? What about if one of them is also reversed, <em>i.e.</em> values increase from the top of the chart to the bottom?</p>
<p>Judging by <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/42057832/how-to-draw-rainfall-runoff-graph-in-r-using-ggplot">this StackOverflow question</a>, hydrologists are fond of both of these things. It asks whether <code>ggplot2</code> can be used to generate a “rainfall hyetograph and streamflow hydrograph”, which looks like this:</p>
<p><span></span><br>
<a target="_blank" rel="nofollow" href="https://i0.wp.com/i.stack.imgur.com/3Urk8.png?ssl=1"><img alt="" src="https://i0.wp.com/i.stack.imgur.com/3Urk8.png?resize=456%2C304&ssl=1" data-recalc-dims="1"></a></p>
<p>My first thought was “why?” but perhaps, as <a target="_blank" rel="nofollow" href="https://twitter.com/CFlensburg/status/828381727873658880">suggested on Twitter</a>, the chart signifies rain falling from above.</p>
<p>My view (and one held more widely) is that dual axes are to be discouraged unless (1) the variables measured in each case are directly comparable with regard to both magnitude and units, and (2) they don’t lead to clutter or visual confusion. This is why <code>ggplot2</code> makes it difficult to implement them, although there is limited support in recent version (see <em>e.g.</em> <a target="_blank" rel="nofollow" href="https://rpubs.com/MarkusLoew/226759">these examples</a>).</p>
<p>I suggested using instead a javascript library, such as Highcharts. It’s a measure of how good the <code>highcharter</code> package is that having been using it for only a day, I was able to throw together an initial solution pretty quickly:</p>
<pre>
library(highcharter)
library(dplyr)

rainfall &lt;- data.frame(date = as.Date(rep(c(&quot;2000-01-01&quot;, &quot;2000-01-02&quot;, &quot;2000-01-03&quot;, &quot;2000-01-04&quot;), 2), &quot;%Y-%m-%d&quot;), 
                       value = c(13.2, 9.5, 7.3, 0.2, 150, 135, 58, 38), 
                       variable = c(rep(&quot;rain&quot;, 4), rep(&quot;discharge&quot;, 4)))

hc &lt;- highchart() %>% 
  hc_yAxis_multiples(list(title = list(text = &quot;rainfall depth (mm)&quot;), reversed = TRUE), 
                     list(title = list(text = &quot;flow (m3/s)&quot;), 
                      opposite = TRUE)) %>% 
  hc_add_series(data = filter(rainfall, variable == &quot;rain&quot;) %>% mutate(value = -value) %>% .$value, type = &quot;column&quot;) %>% 
  hc_add_series(data = filter(rainfall, variable == &quot;discharge&quot;) %>% .$value, type = &quot;spline&quot;, yAxis = 1) %>% 
  hc_xAxis(categories = dataset$rain.date, title = list(text = &quot;date&quot;))
  hc
</pre>
<p><a target="_blank" rel="nofollow" href="http://rpubs.com/neilfws/247586">Here’s the interactive result</a>. And here’s a screenshot:</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/i.stack.imgur.com/SqUj5.png?ssl=1"><img alt="" src="https://i0.wp.com/i.stack.imgur.com/SqUj5.png?resize=456%2C335&ssl=1" data-recalc-dims="1"></a></p>
<p>So it can be done and if that’s a “standard” plot in hydrology, I suppose it needs to be done…but given free rein, it’s not how I’d do it. Here’s a <code>ggplot2</code> alternative, using a common x-axis and facets for different variables.</p>
<pre>
ggplot() + 
facet_grid(variable ~ ., scales = &quot;free&quot;, switch = &quot;y&quot;, labeller = labeller(variable = c(&quot;rain&quot; = &quot;rainfall depth (mm)&quot;, &quot;discharge&quot; = &quot;flow m3/s&quot;))) + 
geom_col(data = filter(rainfall, variable == &quot;rain&quot;), aes(date, value), fill = &quot;skyblue3) + 
theme_bw() + 
geom_line(data = filter(rainfall, variable == &quot;discharge&quot;), aes(date, value)) +
labs(title = &quot;A rainfall hyetograph and streamflow hydrograph&quot;)
</pre>
<p>Result:</p>
<p><a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/2017/02/07/hyetographs-hydrographs-and-highcharter/rainfall/"><img sizes="(max-width: 729px) 100vw, 729px" data-image-description="" data-orig-file="https://nsaunders.files.wordpress.com/2017/02/rainfall.png" data-orig-size="729,519" data-image-title="rainfall" data-comments-opened="1" class="wp-image-4689" data-recalc-dims="1" data-attachment-id="4689" data-permalink="https://nsaunders.wordpress.com/2017/02/07/hyetographs-hydrographs-and-highcharter/rainfall/" alt="rainfall" data-medium-file="https://nsaunders.files.wordpress.com/2017/02/rainfall.png?w=300" data-large-file="https://nsaunders.files.wordpress.com/2017/02/rainfall.png?w=729" 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://i1.wp.com/nsaunders.files.wordpress.com/2017/02/rainfall.png?fit=456%2C456&ssl=1"></a></p>
<p>I’d argue that this approach still allows for visual comparison of the two variables, without implying that they measure the same thing.</p>
<p>Filed under: <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/category/statistics/r/">R</a>, <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/category/statistics/">statistics</a> Tagged: <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/tag/ggplot2/">ggplot2</a>, <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/tag/highcharter/">highcharter</a>, <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/tag/highcharts/">highcharts</a>, <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/tag/hydrology/">hydrology</a>, <a target="_blank" rel="nofollow" href="https://nsaunders.wordpress.com/tag/package/">package</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nsaunders.wordpress.com/4685/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/nsaunders.wordpress.com/4685/" 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://nsaunders.wordpress.com/2017/02/07/hyetographs-hydrographs-and-highcharter/"> R – What You're Doing Is Rather Desperate</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/LeSnJewSHvk"></a></div>