<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/"></div></div><div><a data-url="https://www.r-bloggers.com/ggplot2-2-2-0/" data-counturl="https://www.r-bloggers.com/ggplot2-2-2-0/" data-count="horizontal" data-text="ggplot2 2.2.0" 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/11/14/ggplot2-2-2-0/"> RStudio Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’m very pleased to announce ggplot2 2.2.0. It includes four major new features:</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>
</ul>
<p>It also includes as numerous bug fixes and minor improvements, as described in the <a target="_blank" rel="nofollow" href="http://github.com/hadley/ggplot2/releases/tag/v2.2.0">release notes</a>.</p>
<p>The majority of this work was carried out by <a target="_blank" rel="nofollow" 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 his other visualisation packages: <a target="_blank" rel="nofollow" href="https://github.com/thomasp85/ggraph">ggraph</a>, <a target="_blank" rel="nofollow" href="https://github.com/thomasp85/ggforce">ggforce</a>, and <a target="_blank" rel="nofollow" href="https://github.com/thomasp85/tweenr">tweenr</a>.</p>
<p>Install ggplot2 with:</p>
<pre><code>install.packages(&quot;ggplot2&quot;)</code></pre>
<div>
<h2>Subtitles and captions</h2>
<p>Thanks to <a target="_blank" rel="nofollow" href="https://rud.is/">Bob Rudis</a>, you can now add subtitles and captions to your plots:</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;
  )</code></pre>
<p> </p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/subtitle-11.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="subtitle-1" class="wp-image-3689" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/subtitle-11.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a></p>
<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 centered alignment, 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 <code>vignette(&quot;extending-ggplot2&quot;)</code>. Along with the rewrite a number of features and improvements has been added, most notably:</p>
<ul>
<li>ou can now use functions in facetting formulas, thanks to <a target="_blank" rel="nofollow" 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))</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-1-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="facet-1-1" class="wp-image-3679" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-1-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a>
<li>Axes are now drawn under the panels in <code>facet_wrap()</code> when the rentangle is not completely filled.
<pre><code>ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  facet_wrap(~class)</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-2-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="facet-2-1" class="wp-image-3680" src="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-2-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a>
<li>You can set the position of the axes with the <code>position</code> argument.
<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" rel="nofollow" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-3-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="facet-3-1" class="wp-image-3681" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-3-1.png?resize=300%2C188&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 <code>sec.axis</code>.
<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> 
<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" rel="nofollow" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-5-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="facet-5-1" class="wp-image-3683" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/facet-5-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a>


<div>
<h2>Theming</h2>
<ul>
<li>The <code>theme()</code> function now has named arguments so autocomplete and documentation suggestions are vastly improved.</li>
<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" rel="nofollow" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/theme-1-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="theme-1-1" class="wp-image-3690" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/theme-1-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a>
<li>Control of legend styling has been improved. The whole legend area can be aligned with 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 = &quot;horizontal&quot;,
    legend.box.margin = margin(3, 3, 3, 3, &quot;mm&quot;), 
    legend.margin = margin(),
    legend.box.background = element_rect(colour = &quot;grey50&quot;)
  )</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/theme-2-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="theme-2-1" class="wp-image-3691" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/theme-2-1.png?resize=300%2C188&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> 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 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" rel="nofollow" href="http://www.obs-vlfr.fr/~irisson/">Jean-Olivier Irisson</a> making them better match <code>theme_grey()</code>.</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" rel="nofollow" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-1-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="stack-1-1" class="wp-image-3684" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-1-1.png?resize=300%2C188&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>If you want to stack in the opposite order, try <a target="_blank" rel="nofollow" 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)))</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-2-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="stack-2-1" class="wp-image-3685" src="https://i0.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-2-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a></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" rel="nofollow" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-3-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="stack-3-1" class="wp-image-3686" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-3-1.png?resize=300%2C188&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>Labels can also be stacked, but the default position is suboptimal:</p>
<pre><code>series &lt;- data.frame(
  time = c(rep(1, 4),rep(2, 4), rep(3, 4), rep(4, 4)),
  type = rep(c('a', 'b', 'c', 'd'), 4),
  value = rpois(16, 10)
)

ggplot(series, aes(time, value, group = type)) +
  geom_area(aes(fill = type)) +
  geom_text(aes(label = type), position = &quot;stack&quot;)</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-4-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="stack-4-1" class="wp-image-3687" src="https://i1.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-4-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a></p>
<p>You can improve the position with the <code>vjust</code> parameter. A <code>vjust</code> of 0.5 will center the labels inside the corresponding area:</p>
<pre><code>ggplot(series, aes(time, value, group = type)) +
  geom_area(aes(fill = type)) +
  geom_text(aes(label = type), position = position_stack(vjust = 0.5))</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-5-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" alt="stack-5-1" class="wp-image-3688" src="https://i2.wp.com/rstudioblog.files.wordpress.com/2016/11/stack-5-1.png?resize=300%2C188&ssl=1" data-recalc-dims="1"></a></p>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rstudioblog.wordpress.com/3677/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rstudioblog.wordpress.com/3677/" 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/"></div></div><div><a data-url="https://www.r-bloggers.com/ggplot2-2-2-0/" data-counturl="https://www.r-bloggers.com/ggplot2-2-2-0/" data-count="vertical" data-text="ggplot2 2.2.0" 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/11/14/ggplot2-2-2-0/"> 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/FIAXUZ3sLpE"></a></div></ul></div></ul></div>