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 18.104.22.16801. Please try it out, and file an issue on GitHub if you discover any problems. I hope we can find and fix any major issues before the official release.
Install the pre-release version with:
# install.packages("devtools") devtools::install_github("hadley/ggplot2")
If you discover a major bug that breaks your plots, please file a minimal reprex, and then roll back to the released version with:
ggplot2 2.2.0 will be a relatively major release including:
Subtitles and captions.
A large rewrite of the facetting system.
Improved theme options.
The majority of this work was carried out by Thomas Pedersen, who I was lucky to have as my “ggplot2 intern” this summer. Make sure to check out other visualisation packages: ggraph, ggforce, and tweenr.
Thanks to Bob Rudis, you can now add subtitles and captions:
ggplot(mpg, aes(displ, hwy)) + geom_point(aes(color = class)) + geom_smooth(se = FALSE, method = "loess") + labs( title = "Fuel efficiency generally decreases with engine size", subtitle = "Two seaters (sports cars) are an exception because of their light weight", caption = "Data from fueleconomy.gov" )
These are controlled by the theme settings
The plot title is now aligned to the left by default. To return to the previous centering, use
theme(plot.title = element_text(hjust = 0.5)).
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 Extending ggplot2 vignette. Along with the rewrite a number of features and improvements has been added, most notably:
ggplot(diamonds, aes(carat, price)) + geom_hex(bins = 20) + facet_wrap(~cut_number(depth, 6))
facet_wrap()did not completely fill the rectangle. Now, an axis is drawn underneath the hanging panels:
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~class)
positionargument in the scale constructor:
ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous(position = "top") + scale_y_continuous(position = "right")
ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_y_continuous( "mpg (US)", sec.axis = sec_axis(~ . * 1.20, name = "mpg (UK)") )
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~ drv, strip.position = "bottom") + theme( strip.placement = "outside", strip.background = element_blank(), strip.text = element_text(face = "bold") ) + xlab(NULL)
Blank elements can now be overridden again so you get the expected behavior when setting e.g.
element_line() gets an
arrow argument that lets you put arrows on axes.
arrow <- arrow(length = unit(0.4, "cm"), type = "closed") ggplot(mpg, aes(displ, hwy)) + geom_point() + theme_minimal() + theme( axis.line = element_line(arrow = arrow) )
ggplot(mpg, aes(displ, hwy, shape = drv, colour = fl)) + geom_point() + theme( legend.justification = "top", legend.box.margin = margin(3, 3, 3, 3, "mm"), legend.box.background = element_rect(colour = "grey50") )
legend.margin have been renamed to
legend.spacing respectively as this better indicates their roles. A new
legend.margin has been actually controls the margin around each legend.
When computing the height of titles ggplot2, now inclues the height of the descenders (i.e. the bits
y 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.
The default themes has been tweaked by Jean-Olivier Irisson making them better match
theme() function now has named arguments so autocomplete and documentation suggestions are vastly improved.
position_fill() now stack values in the reverse order of the grouping, which makes the default stack order match the legend.
avg_price <- 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))
(Note also the new
geom_col() which is short-hand for
geom_bar(stat = "identity"), contributed by Bob Rudis.)
Additionally, you can now stack negative values:
ggplot(avg_price) + geom_col(aes(x = cut, y = price_rel, fill = color))
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.
If you want to stack in the opposite order, try
ggplot(avg_price) + geom_col(aes(x = cut, y = price, fill = fct_rev(color)))
In this series, we walk through lesser-known tips and tricks to help you work more effectively and efficiently in R Markdown. This third post focuses on features that save you time and trouble.
The RStudio IDE includes a visual markdown editor that displays changes in real-time and provides support for technical writing. This post walks through several features of visual editing mode.