<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/announcing-ggforce-accelerating-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/announcing-ggforce-accelerating-ggplot2/" data-counturl="https://www.r-bloggers.com/announcing-ggforce-accelerating-ggplot2/" data-count="horizontal" data-text="Announcing ggforce: Accelerating ggplot2" 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://www.data-imaginist.com/2016/Announcing-ggforce/"> Data Imaginist - R posts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am very pleased to announce the first release of the <code>ggforce</code> package.<br>
<code>ggforce</code> is a general <code>ggplot2</code> extension package in the same vein as <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=ggalt"><code>ggalt</code></a> with no overarching goal<br>
other than to provide additional functionality to the <code>ggplot2</code> universe. The<br>
inception and birth of ggforce was documented in a recent<br>
<a target="_blank" rel="nofollow" href="http://www.data-imaginist.com/2016/Becoming-the-intern/">post</a> and the road to CRAN has<br>
been long and winding, with my personal goal of the package going through<br>
multiple iterations. At the beginning it was meant as a place where everyone<br>
could submit extensions to. As I began working on<br>
<a target="_blank" rel="nofollow" href="https://github.com/thomasp85/ggraph"><code>ggraph</code></a> it became a dumping ground for<br>
functionality that was broader than network visualization, and as I began<br>
working on <code>ggplot2</code> it became a testing ground for new facetting functions.<br>
Now, as it is ready for CRAN, it does include some requests and code from other<br>
developers, thus paying partial homage to the founding goal of the package.</p>
<p>This post will not go into detail with every functionality that ggforce adds to<br>
<code>ggplot2</code>. <code>ggforce</code> includes a<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/ggforce/vignettes/Visual_Guide.html">vignette</a><br>
where every feature is described along with motivation and code examples.</p>
<h2>Features</h2>
<p>Below follows a description of all the major features (small utility functions<br>
are not included), grouped by the grammar they extend:</p>
<h3>Geoms</h3>
<ul>
<li><code>geom_arc()</code> allows you to plot circle segments defined by center, radius, and<br>
start- and end-angle.</li>
<li><code>geom_arc_bar()</code> is as above, except it draws a <em>thick</em> arc defined by an<br>
inner and outer radius. If the inner radius is zero it becomes a wedge.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>pie</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>state</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'eaten'</span><span>,</span><span> </span><span>'eaten but said you didn\'t'</span><span>,</span><span> </span><span>'cat took it'</span><span>,</span><span> </span><span>'for tonight'</span><span>,</span><span> </span><span>'will decompose slowly'</span><span>),</span><span>
</span><span>focus</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.2</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>),</span><span>
</span><span>start</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>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>),</span><span>
</span><span>end</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>2</span><span>*</span><span>pi</span><span>),</span><span>
</span><span>amount</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>4</span><span>,</span><span>3</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>1.5</span><span>,</span><span> </span><span>6</span><span>),</span><span>
</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>
</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>pie</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_arc_bar</span><span>(</span><span>aes</span><span>(</span><span>x</span><span>0</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>y</span><span>0</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>r</span><span>0</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>r</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>amount</span><span> </span><span>=</span><span> </span><span>amount</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>state</span><span>,</span><span> </span><span>explode</span><span> </span><span>=</span><span> </span><span>focus</span><span>),</span><span> </span><span>stat</span><span> </span><span>=</span><span> </span><span>'pie'</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_fill_brewer</span><span>(</span><span>''</span><span>,</span><span> </span><span>palette</span><span> </span><span>=</span><span> </span><span>'Set1'</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_fixed</span><span>()</span></code></pre>
</figure>
<p><img alt="center" src="https://i0.wp.com/www.data-imaginist.com/assets/images/2016-11-22-Announcing-ggforce/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<ul>
<li><code>geom_circle()</code> makes it possible to draw circles based on coordinate space,<br>
that is, the radius is scaled to the coordinate system rather than absolute.</li>
<li><code>geom_link()</code> is like <code>geom_segment()</code> except it expands the line into<br>
multiple segments making it possible to draw gradients over the line.</li>
<li><code>geom_bezier()</code> allows you to draw cubic and quadratic beziers.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>beziers</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>6</span><span>,</span><span> </span><span>6</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>0</span><span>),</span><span>
</span><span>type</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>'cubic'</span><span>,</span><span> </span><span>'quadratic'</span><span>),</span><span> </span><span>c</span><span>(</span><span>3</span><span>,</span><span> </span><span>4</span><span>)),</span><span>
</span><span>point</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'end'</span><span>,</span><span> </span><span>'control'</span><span>,</span><span> </span><span>'end'</span><span>,</span><span> </span><span>'end'</span><span>,</span><span> </span><span>'control'</span><span>,</span><span> </span><span>'control'</span><span>,</span><span> </span><span>'end'</span><span>)</span><span>
</span><span>)</span><span>
</span><span>help_lines</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>6</span><span>),</span><span>
</span><span>xend</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>6</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span>
</span><span>yend</span><span> </span><span>=</span><span> </span><span>2</span><span>
</span><span>)</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span> </span><span>geom_segment</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>xend</span><span> </span><span>=</span><span> </span><span>xend</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>yend</span><span> </span><span>=</span><span> </span><span>yend</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>help_lines</span><span>,</span><span> </span><span>arrow</span><span> </span><span>=</span><span> </span><span>arrow</span><span>(</span><span>length</span><span> </span><span>=</span><span> </span><span>unit</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0.5</span><span>,</span><span> </span><span>0.5</span><span>),</span><span> </span><span>'cm'</span><span>)),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>'grey'</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_bezier</span><span>(</span><span>aes</span><span>(</span><span>x</span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>type</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>type</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>beziers</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>point</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>beziers</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i0.wp.com/www.data-imaginist.com/assets/images/2016-11-22-Announcing-ggforce/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<ul>
<li><code>geom_bspline()</code> makes it possible to draw b-splines.</li>
<li><code>geom_sina()</code> is a novel alternative to <code>geom_violin()</code>/<code>geom_boxplot()</code>/<code>geom_jitter()</code><br>
that was submitted to ggforce by the developers of<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sinaplot">sinaplot</a></li>
</ul>
<figure>
<pre><code data-lang="r"><span>df</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>&quot;Distribution&quot;</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>c</span><span>(</span><span>rep</span><span>(</span><span>&quot;Unimodal&quot;</span><span>,</span><span> </span><span>500</span><span>),</span><span>
</span><span>rep</span><span>(</span><span>&quot;Bimodal&quot;</span><span>,</span><span> </span><span>250</span><span>),</span><span>
</span><span>rep</span><span>(</span><span>&quot;Trimodal&quot;</span><span>,</span><span> </span><span>600</span><span>)),</span><span> </span><span>c</span><span>(</span><span>'Unimodal'</span><span>,</span><span> </span><span>'Bimodal'</span><span>,</span><span> </span><span>'Trimodal'</span><span>)),</span><span>
</span><span>&quot;Value&quot;</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>rnorm</span><span>(</span><span>500</span><span>,</span><span> </span><span>6</span><span>,</span><span> </span><span>1</span><span>),</span><span>
</span><span>rnorm</span><span>(</span><span>200</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>.7</span><span>),</span><span> </span><span>rnorm</span><span>(</span><span>50</span><span>,</span><span> </span><span>7</span><span>,</span><span> </span><span>0.4</span><span>),</span><span>
</span><span>rnorm</span><span>(</span><span>200</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>0.7</span><span>),</span><span> </span><span>rnorm</span><span>(</span><span>300</span><span>,</span><span> </span><span>5.5</span><span>,</span><span> </span><span>0.4</span><span>),</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>,</span><span> </span><span>8</span><span>,</span><span> </span><span>0.4</span><span>))</span><span>
</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>Distribution</span><span>,</span><span> </span><span>Value</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_sina</span><span>(</span><span>aes</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>Distribution</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i2.wp.com/www.data-imaginist.com/assets/images/2016-11-22-Announcing-ggforce/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<h3>Scales</h3>
<ul>
<li><code>scale_[x|y]_unit()</code> provides support for the units class from the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=units">units</a> package. It provides<br>
automatical labelling of axis with units as well as changing the units of the<br>
scale without touching the data.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>units</span><span>)</span><span>
</span><span>miles</span><span> </span><span>&lt;-</span><span> </span><span>make_unit</span><span>(</span><span>'miles'</span><span>)</span><span>
</span><span>gallon</span><span> </span><span>&lt;-</span><span> </span><span>make_unit</span><span>(</span><span>'gallon'</span><span>)</span><span>
</span><span>horsepower</span><span> </span><span>&lt;-</span><span> </span><span>make_unit</span><span>(</span><span>'horsepower'</span><span>)</span><span>
</span><span>mtcars</span><span>$</span><span>consumption</span><span> </span><span>&lt;-</span><span> </span><span>mtcars</span><span>$</span><span>mpg</span><span> </span><span>*</span><span> </span><span>(</span><span>miles</span><span>/</span><span>gallon</span><span>)</span><span>
</span><span>mtcars</span><span>$</span><span>power</span><span> </span><span>&lt;-</span><span> </span><span>mtcars</span><span>$</span><span>hp</span><span> </span><span>*</span><span> </span><span>horsepower</span><span>
</span><span>ggplot</span><span>(</span><span>mtcars</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>power</span><span>,</span><span> </span><span>consumption</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_unit</span><span>(</span><span>unit</span><span> </span><span>=</span><span> </span><span>'km/l'</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i2.wp.com/www.data-imaginist.com/assets/images/2016-11-22-Announcing-ggforce/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<h3>Facets</h3>
<ul>
<li><code>facet_[grid|wrap]_paginate()</code> allows you to split grid and wrap facetting out<br>
into multiple pages.</li>
<li><code>facet_zoom()</code> is a third zooming alternative that provides a context<br>
overview.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>iris</span><span>,</span><span> </span><span>aes</span><span>(</span><span>Petal.Length</span><span>,</span><span> </span><span>Petal.Width</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>Species</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_zoom</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Species</span><span> </span><span>==</span><span> </span><span>&quot;versicolor&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i2.wp.com/www.data-imaginist.com/assets/images/2016-11-22-Announcing-ggforce/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<h3>Transformations</h3>
<p>While transformations are part of the <code>scales</code> package rather than <code>ggplot2</code><br>
their use is integral to <code>ggplot2</code>, and <code>ggforce</code> thus fills some gaps in this<br>
department as well.</p>
<ul>
<li><code>power_trans()</code> lets you create any power transformation (<code>scales</code> only<br>
provides <code>sqrt_trans()</code>).</li>
<li><code>radial_trans()</code> lets you translate between coordinates specified by radius<br>
and angle and coordinates specified by x and y.</li>
<li><code>trans_reverser()</code> reverses any monotonous transformation function, making it<br>
possible to e.g. have a reversed log transformation (<code>scales</code> only provides<br>
<code>reverse_trans()</code> for reversing the identity transformation).</li>
</ul>
<figure>
<pre><code data-lang="r"><span>p</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>power_trans</span><span>(</span><span>3</span><span>)</span><span>
</span><span>p</span><span>3</span><span>r</span><span> </span><span>&lt;-</span><span> </span><span>trans_reverser</span><span>(</span><span>p</span><span>3</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>mtcars</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>mpg</span><span>,</span><span> </span><span>cyl</span><span>))</span><span> </span><span>+</span><span> </span><span>scale_y_continuous</span><span>(</span><span>trans</span><span> </span><span>=</span><span> </span><span>p</span><span>3</span><span>r</span><span>)</span></code></pre>
</figure>
<p><img alt="center" src="https://i1.wp.com/www.data-imaginist.com/assets/images/2016-11-22-Announcing-ggforce/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<h2>Future</h2>
<p>I have many cool ideas planned for future releases, and I hope that users will<br>
approach me if they have some great ideas as well. If you have a burning wish<br>
for something in the <code>ggplot2</code> ecosystem I welcome any issues and PRs on the<br>
<a target="_blank" rel="nofollow" href="https://github.com/thomasp85/ggforce"><code>ggforce</code> github repository</a>. The<br>
<code>ggplot2</code> ecosystem is thriving though so please visit<br>
<a target="_blank" rel="nofollow" href="http://www.ggplot2-exts.org/">http://www.ggplot2-exts.org</a> and see if your idea doesn’t already exist in<br>
another package.</p>
<p>Happy plotting!</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/announcing-ggforce-accelerating-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/announcing-ggforce-accelerating-ggplot2/" data-counturl="https://www.r-bloggers.com/announcing-ggforce-accelerating-ggplot2/" data-count="vertical" data-text="Announcing ggforce: Accelerating ggplot2" 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://www.data-imaginist.com/2016/Announcing-ggforce/"> Data Imaginist - R posts</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/O4E_Obh_udU"></a></div>