<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/ggplot2-2-2-0-coming-soon/"></div></div><div><a data-url="https://www.r-bloggers.com/ggplot2-2-2-0-coming-soon/" data-counturl="https://www.r-bloggers.com/ggplot2-2-2-0-coming-soon/" data-count="horizontal" data-text="ggplot2 2.2.0 coming soon!" 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="https://blog.rstudio.org/2016/09/30/ggplot2-2-2-0-coming-soon/"> RStudio Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’m planning to release ggplot2 2.2.0 in early November. In preparation, I’d like to announce that a release candidate is now available: version 2.1.0.9001. Please try it out, and file an <a target="_blank" href="https://github.com/hadley/ggplot2/issues">issue on GitHub</a> if you discover any problems. I hope we can find and fix any major issues before the official release.</p>
<p>Install the pre-release version with:</p>
<pre><code># install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;hadley/ggplot2&quot;)</code></pre>
<p>If you discover a major bug that breaks your plots, please <a target="_blank" href="https://github.com/hadley/ggplot2/issues">file a minimal reprex</a>, and then roll back to the released version with:</p>
<pre><code>install.packages(&quot;ggplot2&quot;)</code></pre>
<p>ggplot2 2.2.0 will be a relatively major release including:</p>
<ul>
<li>Subtitles and captions.</li>
<li>A large rewrite of the facetting system.</li>
<li>Improved theme options.</li>
<li>Better stacking</li>
<li><a target="_blank" href="https://github.com/hadley/ggplot2/blob/master/NEWS.md">Numerous bug fixes and minor improvements</a>.</li>
</ul>
<p>The majority of this work was carried out by <a target="_blank" href="https://github.com/thomasp85">Thomas Pederson</a>, who I was lucky to have as my “ggplot2 intern” this summer. Make sure to check out other visualisation packages: <a target="_blank" href="https://github.com/thomasp85/ggraph">ggraph</a>, <a target="_blank" href="https://github.com/thomasp85/ggforce">ggforce</a>, and <a target="_blank" href="https://github.com/thomasp85/tweenr">tweenr</a>.</p>
<div>
<h2>Subtitles and captions</h2>
<p>Thanks to <a target="_blank" href="https://rud.is/">Bob Rudis</a>, you can now add subtitles and captions:</p>
<pre><code>ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class)) +
  geom_smooth(se = FALSE, method = &quot;loess&quot;) +
  labs(
    title = &quot;Fuel efficiency generally decreases with engine size&quot;,
    subtitle = &quot;Two seaters (sports cars) are an exception because of their light weight&quot;,
    caption = &quot;Data from fueleconomy.gov&quot;
  )
<a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-3-1.png?ssl=1"><img alt="unnamed-chunk-3-1" class="wp-image-3283" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-3-1.png?w=456&ssl=1" data-recalc-dims="1"></a></code></pre>
<p>These are controlled by the theme settings <code>plot.subtitle</code> and <code>plot.caption</code>.</p>
<p>The plot title is now aligned to the left by default. To return to the previous centering, use <code>theme(plot.title = element_text(hjust = 0.5))</code>.</p>
</div>
<div>
<h2>Facets</h2>
<p>The facet and layout implementation has been moved to ggproto and received a large rewrite and refactoring. This will allow others to create their own facetting systems, as descrbied in the <em>Extending ggplot2</em> vignette. Along with the rewrite a number of features and improvements has been added, most notably:</p>
<ul>
<li>Functions in facetting formulas, thanks to <a target="_blank" href="https://github.com/DanRuderman">Dan Ruderman</a>.
<pre><code>ggplot(diamonds, aes(carat, price)) + 
  geom_hex(bins = 20) + 
  facet_wrap(~cut_number(depth, 6))
<a target="_blank" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-4-1.png?ssl=1"><img alt="unnamed-chunk-4-1" class="wp-image-3284" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-4-1.png?w=456&ssl=1" data-recalc-dims="1"></a></code></pre>
</li>
<li>Axes were dropped when the panels in <code>facet_wrap()</code> did not completely fill the rectangle. Now, an axis is drawn underneath the hanging panels:
<pre><code>ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  facet_wrap(~class)</code></pre>
<p><a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-5-1.png?ssl=1"><img alt="unnamed-chunk-5-1" class="wp-image-3285" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"></a>
<li>It is now possible to set the position of the axes through the <code>position</code> argument in the scale constructor:
<pre><code>ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  scale_x_continuous(position = &quot;top&quot;) + 
  scale_y_continuous(position = &quot;right&quot;)</code></pre>
<p><a target="_blank" href="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-6-1.png?ssl=1"><img alt="unnamed-chunk-6-1" class="wp-image-3286" src="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></a>
<li>You can display a secondary axis that is a one-to-one transformation of the primary axis with the <code>sec.axis</code> argument:
<pre><code>ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  scale_y_continuous(
    &quot;mpg (US)&quot;, 
    sec.axis = sec_axis(~ . * 1.20, name = &quot;mpg (UK)&quot;)
  )</code></pre>
<p><a target="_blank" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-7-1.png?ssl=1"><img alt="unnamed-chunk-7-1" class="wp-image-3287" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-7-1.png?w=456&ssl=1" data-recalc-dims="1"></a>
<li>Strips can be placed on any side, and the placement with respect to axes can be controlled with the <code>strip.placement</code> theme option.
<pre><code>ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  facet_wrap(~ drv, strip.position = &quot;bottom&quot;) + 
  theme(
    strip.placement = &quot;outside&quot;,
    strip.background = element_blank(),
    strip.text = element_text(face = &quot;bold&quot;)
  ) +
  xlab(NULL)</code></pre>
<p><a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-8-1.png?ssl=1"><img alt="unnamed-chunk-8-1" class="wp-image-3288" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></a>


<div>
<h2>Theming</h2>
<ul>
<li>Blank elements can now be overridden again so you get the expected behavior when setting e.g. <code>axis.line.x</code>.</li>
<li><code>element_line()</code> gets an <code>arrow</code> argument that lets you put arrows on axes.
<pre><code>arrow &lt;- arrow(length = unit(0.4, &quot;cm&quot;), type = &quot;closed&quot;)

ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  theme_minimal() + 
  theme(
    axis.line = element_line(arrow = arrow)
  )</code></pre>
<p><a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-9-1.png?ssl=1"><img alt="unnamed-chunk-9-1" class="wp-image-3289" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-9-1.png?w=456&ssl=1" data-recalc-dims="1"></a>
<li>Control of legend styling has been improved. The whole legend area can be aligned according to the plot area and a box can be drawn around all legends:
<pre><code>ggplot(mpg, aes(displ, hwy, shape = drv, colour = fl)) + 
  geom_point() + 
  theme(
    legend.justification = &quot;top&quot;, 
    legend.box.margin = margin(3, 3, 3, 3, &quot;mm&quot;), 
    legend.box.background = element_rect(colour = &quot;grey50&quot;)
  )</code></pre>
<p><a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-10-1.png?ssl=1"><img alt="unnamed-chunk-10-1" class="wp-image-3290" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-10-1.png?w=456&ssl=1" data-recalc-dims="1"></a>
<li><code>panel.margin</code> and <code>legend.margin</code> have been renamed to <code>panel.spacing</code> and <code>legend.spacing</code> respectively as this better indicates their roles. A new <code>legend.margin</code> has been actually controls the margin around each legend.</li>
<li>When computing the height of titles ggplot2, now inclues the height of the descenders (i.e. the bits <code>g</code> and <code>y</code> that hang underneath). This makes improves the margins around titles, particularly the y axis label. I have also very slightly increased the inner margins of axis titles, and removed the outer margins.</li>
<li>The default themes has been tweaked by <a target="_blank" href="http://www.obs-vlfr.fr/~irisson/">Jean-Olivier Irisson</a> making them better match <code>theme_grey()</code>.</li>
<li>Lastly, the <code>theme()</code> function now has named arguments so autocomplete and documentation suggestions are vastly improved.</li>


<div>
<h2>Stacking bars</h2>
<p><code>position_stack()</code> and <code>position_fill()</code> now stack values in the reverse order of the grouping, which makes the default stack order match the legend.</p>
<pre><code>avg_price &lt;- diamonds %>% 
  group_by(cut, color) %>% 
  summarise(price = mean(price)) %>% 
  ungroup() %>% 
  mutate(price_rel = price - mean(price))

ggplot(avg_price) + 
  geom_col(aes(x = cut, y = price, fill = color))</code></pre>
<p><a target="_blank" href="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-11-1.png?ssl=1"><img alt="unnamed-chunk-11-1" class="wp-image-3291" src="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-11-1.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>(Note also the new <code>geom_col()</code> which is short-hand for <code>geom_bar(stat = &quot;identity&quot;)</code>, contributed by Bob Rudis.)</p>
<p>Additionally, you can now stack negative values:</p>
<pre><code>ggplot(avg_price) + 
  geom_col(aes(x = cut, y = price_rel, fill = color))</code></pre>
<p><a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-12-1.png?ssl=1"><img alt="unnamed-chunk-12-1" class="wp-image-3292" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-12-1.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>The overall ordering cannot necessarily be matched in the presence of negative values, but the ordering on either side of the x-axis will match.</p>
<p>If you want to stack in the opposite order, try <a target="_blank" href="http://forcats.tidyverse.org/reference/fct_rev.html"><code>forcats::fct_rev()</code></a>:</p>
<pre><code>ggplot(avg_price) + 
  geom_col(aes(x = cut, y = price, fill = fct_rev(color)))
<a target="_blank" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-13-1.png?ssl=1"><img alt="unnamed-chunk-13-1" class="wp-image-3293" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/09/unnamed-chunk-13-1.png?w=456&ssl=1" data-recalc-dims="1"></a></code></pre>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rstudioblog.wordpress.com/3282/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rstudioblog.wordpress.com/3282/" 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><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/ggplot2-2-2-0-coming-soon/"></div></div><div><a data-url="https://www.r-bloggers.com/ggplot2-2-2-0-coming-soon/" data-counturl="https://www.r-bloggers.com/ggplot2-2-2-0-coming-soon/" data-count="vertical" data-text="ggplot2 2.2.0 coming soon!" 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="https://blog.rstudio.org/2016/09/30/ggplot2-2-2-0-coming-soon/"> RStudio 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 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/Mjm_2nIGEsw"></a></div></ul></div></ul></div>