I’m very pleased to announce ggplot2 2.2.0. It includes four major new features:
As well as numerous bug fixes and minor improvements, as described in the release notes.
The majority of this work was carried out by Thomas Pederson, who I was lucky to have as my “ggplot2 intern” this summer. Make sure to check out his other visualisation packages: ggraph, ggforce, and tweenr.
Install ggplot2 with:
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
vignette("extending-ggplot2"). Along with the rewrite a number of features and improvements has been added, most notably:
ou can now use functions in facetting formulas, thanks to Dan Ruderman.
Axes are now drawn under the panels in
facet_wrap() when the rentangle is not completely filled.
You can set the position of the axes with the
ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous(position = "top") + scale_y_continuous(position = "right")
You can display a secondary axis that is a one-to-one transformation of the primary axis with
ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_y_continuous( "mpg (US)", sec.axis = sec_axis(~ . * 1.20, name = "mpg (UK)") )
Strips can be placed on any side, and the placement with respect to axes can be controlled with the
strip.placement theme option.
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) )
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:
ggplot(mpg, aes(displ, hwy, shape = drv, colour = fl)) + geom_point() + theme( legend.justification = "top", legend.box = "horizontal", legend.box.margin = margin(3, 3, 3, 3, "mm"), legend.margin = margin(), 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 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 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.
theme() function now has named arguments so autocomplete and documentation suggestions are vastly improved.
If you want to stack in the opposite order, try
Additionally, you can now stack negative values:
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.
Labels can also be stacked, but the default position is suboptimal:
You can improve the position with the
vjust parameter. A
vjust of 0.5 will center the labels inside the corresponding area: