<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/joy-plot-of-length-frequencies/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://derekogle.com/fishR/2017-07-28-JoyPlot"> fishR Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<hr>
<p>There has been a bit of a buzz recently about so-called “joyplots.” <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggjoy/vignettes/introduction.html" target="_blank">Wilke</a> described joyplots as “partially overlapping line plots that create the impression of a mountain range.” I would describe them as partially overlapping density plots (akin to a smoothed histogram). Examples of joyplots are <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggjoy/vignettes/gallery.html" target="_blank">here</a>, <a rel="nofollow" href="http://blog.revolutionanalytics.com/2017/07/joyplots.html" target="_blank">here</a>, and <a rel="nofollow" href="https://vuorre.netlify.com/post/2017/visualizing-varying-effects-posteriors-with-joyplots/" target="_blank">here</a> (from among many).</p>
<p>I thought that joyplots might provide a nice visualization of length frequencies over time. For example, we have a paper forthcoming in the <em>North American Journal of Fisheries Management</em> that examines (among other things) the lengths of Kiyi (<em>Coregonus kiyi</em>) captured in trawl tows in Lake Superior from 2003 to 2014. Figure 1 is a modified version (stripped of code that increased fonts, changed colors, etc.) of the length frequency histograms we included in the paper. Figure 2 is a near default joyplot of the same data.</p>
<p><img alt="plot of chunk KiyiHist" src="https://i0.wp.com/derekogle.com/fishR/figures/KiyiHist-1.png?w=456" data-recalc-dims="1"></p>
<p>Figure 1: Histograms of the total lengths of Lake Superior Kiyi from 2003 to 2014.</p>
<p><img alt="plot of chunk KiyiJoy" src="https://i2.wp.com/derekogle.com/fishR/figures/KiyiJoy-1.png?w=456" data-recalc-dims="1"></p>
<p>Figure 2: Joyplot of the total lengths of Lake Superior Kiyi from 2003 to 2014.</p>
<p>In my opinion, the joyplot makes it easier to follow through time the strong year-classes that first appear in 2004 and 2010 and to see how fish in the strong year-classes grow and eventually merge in size with older fish. Joyplots look useful for displaying length (or age) data across many groups (years, locations, etc.). Some, however, like the specificity (less smoothing) of the histograms (see the last example in <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggjoy/vignettes/introduction.html" target="_blank">Wilke</a> for overlapping histograms).</p>
<hr>
<h3>R Code</h3>
<p>The data file used in this example has the following structure.</p>
<figure>
<pre><code data-lang="r"><span>str</span><span>(</span><span>lf</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## 'data.frame':	9151 obs. of  3 variables:
##  $ year: Factor w/ 12 levels &quot;2003&quot;,&quot;2004&quot;,..: 1 1 2 2 2 2 5 5 5 5 ...
##  $ mon : Factor w/ 3 levels &quot;Jul&quot;,&quot;Jun&quot;,&quot;May&quot;: 3 3 3 3 3 3 3 3 3 3 ...
##  $ tl  : int  183 259 200 249 215 234 119 188 157 189 ...</code></pre>
</figure>
<p>The histograms were constructed with the following code (requires the <code>ggplot2</code> package).</p>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>lf</span><span>,</span><span>aes</span><span>(</span><span>x</span><span>=</span><span>tl</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>expand</span><span>=</span><span>c</span><span>(</span><span>0.02</span><span>,</span><span>0</span><span>),</span><span>limits</span><span>=</span><span>c</span><span>(</span><span>40</span><span>,</span><span>310</span><span>),</span><span>
</span><span>breaks</span><span>=</span><span>seq</span><span>(</span><span>0</span><span>,</span><span>350</span><span>,</span><span>50</span><span>),</span><span>
</span><span>labels</span><span>=</span><span>c</span><span>(</span><span>&quot;&quot;</span><span>,</span><span>&quot;&quot;</span><span>,</span><span>100</span><span>,</span><span>&quot;&quot;</span><span>,</span><span>200</span><span>,</span><span>&quot;&quot;</span><span>,</span><span>300</span><span>,</span><span>&quot;&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_histogram</span><span>(</span><span>aes</span><span>(</span><span>y</span><span>=</span><span>..ncount..</span><span>),</span><span>binwidth</span><span>=</span><span>5</span><span>,</span><span>fill</span><span>=</span><span>&quot;gray40&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>year</span><span>,</span><span>nrow</span><span>=</span><span>4</span><span>,</span><span>dir</span><span>=</span><span>&quot;v&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span>=</span><span>&quot;Total Length (mm)&quot;</span><span>,</span><span>y</span><span>=</span><span>&quot;Relative Frequency&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.y</span><span>=</span><span>element_blank</span><span>())</span></code></pre>
</figure>
<p>The joyplot was constructed with the following code (requires the <code>ggplot2</code> and <code>ggjoy</code> packages). See the <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggjoy/vignettes/introduction.html" target="_blank">introductory vignette by Wilke</a> for how to further modify joyplots.</p>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>lf</span><span>,</span><span>aes</span><span>(</span><span>x</span><span>=</span><span>tl</span><span>,</span><span>y</span><span>=</span><span>year</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_joy</span><span>(</span><span>rel_min_height</span><span> </span><span>=</span><span> </span><span>0.01</span><span>)</span><span> </span><span>+</span><span> </span><span># removes tails
</span><span> </span><span>scale_y_discrete</span><span>(</span><span>expand</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.01</span><span>,</span><span> </span><span>0</span><span>))</span><span> </span><span>+</span><span> </span><span># removes cutoff top
</span><span> </span><span>labs</span><span>(</span><span>x</span><span>=</span><span>&quot;Total Length (mm)&quot;</span><span>,</span><span>y</span><span>=</span><span>&quot;Year&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_minimal</span><span>()</span></code></pre>
</figure>
<hr>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://derekogle.com/fishR/2017-07-28-JoyPlot"> fishR 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/3k-ZqzHFeAM" height="1"></a></div>