<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/version-2-2-2-released/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.ggtern.com/2018/01/20/version-2-2-2-released/"> ggtern: ternary diagrams in R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>ggtern version 2.2.2 has just been submitted to CRAN, and it includes a number of new features. This time around, I have adapted the hexbin geometry (and stat), and additionally, created an almost equivalent geometry which operates on a triangular mesh rather than a hexagonal mesh. There are some subtle differences which give some added functionality, and together these will provide an additional level of richness to ternary diagrams produced with ggtern, when the data-set is perhaps significantly large and points themselves start to lose their meaning from visual clutter.</p>
<h3>Ternary Hexbin</h3>
<p>Firstly, lets look a the ternary hexbin, which, as the name suggests has the capability to bin points in a regular hexagonal grid to produce a pseudo-surface. Now in the original ggplot version, this geometry is somewhat limiting since it only performs a ‘count’ on the number of points in each bin, however, it is not hard to imagine how performing a ‘mean’ or ‘standard deviation’, or other user-defined scalar function on a mapping provided by the user:</p>

<pre>set.seed(1)
n  = 5000
df = data.frame(x     = runif(n),
                y     = runif(n),
                z     = runif(n),
                value = runif(n))

ggtern(df,aes(x,y,z)) + 
  geom_hex_tern(bins=5,aes(value=value,fill=..count..))</pre>

<p><a rel="nofollow" href="https://i0.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_hex1.png" target="_blank"><img sizes="(max-width: 606px) 100vw, 606px" src="https://i0.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_hex1.png?w=450" alt="" class="wp-image-2521" data-recalc-dims="1"></a></p>
<p>Now because we can define user functions, we can do something a little more fancy. Here we will calculate the mean within each hexagon, and, also superimpose a text label over the top.</p>

<pre>ggtern(df,aes(x,y,z)) + 
  theme_bw() +
  geom_hex_tern(bins=5,fun=mean,aes(value=value,fill=..stat..)) + 
  stat_hex_tern(bins=5,fun=mean,
                geom='text',
                aes(value=value,label=sprintf(&quot;%.2f&quot;,..stat..)),
                size=3, color='white')</pre>

<p><a rel="nofollow" href="https://i1.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_hex2.png" target="_blank"><img sizes="(max-width: 606px) 100vw, 606px" src="https://i1.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_hex2.png?w=450" alt="" class="wp-image-2522" data-recalc-dims="1"></a></p>
<h3>Ternary Tribin</h3>
<p>The ternary tribin operates much the same, except that the binwidth no longer has meaning, instead, the density (number of panels) of the triangular mesh is controlled exclusively by the ‘bins’ argument. Using the same data above, lets create some equivalent plots:</p>

<pre>ggtern(df,aes(x,y,z)) + 
  geom_tri_tern(bins=5,aes(value=value,fill=..count..))</pre>

<p><a rel="nofollow" href="https://i1.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_tri1.png" target="_blank"><img sizes="(max-width: 606px) 100vw, 606px" src="https://i1.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_tri1.png?w=450" alt="" class="wp-image-2525" data-recalc-dims="1"></a></p>
<p>There is a subtle difference with the labelling in the stat_tri_tern usage below, we have introduced a ‘centroid’ parameter, and this is because the orientation of each polygon is not consistent (some point up, some point down) and so unlike the hexbin where the centroid is returned by default, with the construction of the polygons being handled by the geometry, for the tribin, this is all handled in the stat.</p>

<pre>ggtern(df,aes(x,y,z)) + 
  theme_bw() +
  geom_tri_tern(bins=5,fun=mean,aes(value=value,fill=..stat..)) + 
  stat_tri_tern(bins=5,fun=mean,
                geom='text',
                aes(value=value,label=sprintf(&quot;%.2f&quot;,..stat..)),
                size=3, color='white',centroid=TRUE)</pre>

<p><a rel="nofollow" href="https://i2.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_tri2.png" target="_blank"><img sizes="(max-width: 606px) 100vw, 606px" src="https://i2.wp.com/www.ggtern.com/wp-content/uploads/2018/01/geom_tri2.png?w=450" alt="" class="wp-image-2524" data-recalc-dims="1"></a></p>
<p>These new geometries have been on the cards for quite some time, several users have requested them. Many thanks Laurie and Steve from <a rel="nofollow" href="http://qedinsight.com/" target="_blank">QEDInsight</a> for partially supporting the development of this work, and forcing me to pull my finger out and get it done. Hopefully we will see these in some awesome publications this year at some time. Until this is accepted on CRAN, you will have to download from my <a rel="nofollow" href="https://bitbucket.org/nicholasehamilton/ggtern" target="_blank">bitbucket repo.</a></p>
<p>Cheers.</p>
<p>Hamo</p>
<p>The post <a rel="nofollow" href="http://www.ggtern.com/2018/01/20/version-2-2-2-released/" target="_blank">Version 2.2.2 Released</a> appeared first on <a rel="nofollow" href="http://www.ggtern.com/" target="_blank">ggtern: ternary diagrams in R</a>.</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="http://www.ggtern.com/2018/01/20/version-2-2-2-released/"> ggtern: ternary diagrams in 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 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/ma8uL6ts5vY" height="1"></a></div>