|
| 1 | +# ggplot2 2.2.0 coming soon! |
| 2 | + |
| 3 | +```{r setup, include=FALSE} |
| 4 | +library(ggplot2) |
| 5 | +library(dplyr) |
| 6 | +library(forcats) |
| 7 | +
|
| 8 | +knitr::opts_chunk$set( |
| 9 | + fig.asp = 1 / 1.6, |
| 10 | + out.width = "75%", |
| 11 | + collapse = TRUE, |
| 12 | + comment = "#>" |
| 13 | +) |
| 14 | +``` |
| 15 | + |
| 16 | +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 [issue on GitHub](https://github.com/hadley/ggplot2/issues) if you discover any problems. I hope we can find and fix any major issues before the official release. |
| 17 | + |
| 18 | +Install the pre-release version with: |
| 19 | + |
| 20 | +```{r, eval = FALSE} |
| 21 | +# install.packages("devtools") |
| 22 | +devtools::install_github("hadley/ggplot2") |
| 23 | +``` |
| 24 | + |
| 25 | +If you discover a major bug that breaks your plots, please [file a minimal reprex](https://github.com/hadley/ggplot2/issues), and then roll back to the released version with: |
| 26 | + |
| 27 | +```{r, eval = FALSE} |
| 28 | +install.packages("ggplot2") |
| 29 | +``` |
| 30 | + |
| 31 | +ggplot2 2.2.0 will be a relatively major release including: |
| 32 | + |
| 33 | +* Subtitles and captions. |
| 34 | +* A large rewrite of the facetting system. |
| 35 | +* Improved theme options. |
| 36 | +* Better stacking |
| 37 | +* [Numerous bug fixes and minor improvements][news.md]. |
| 38 | + |
| 39 | +The majority of this work was carried out by [Thomas Pederson](https://github.com/thomasp85), who I was lucky to have as my "ggplot2 intern" this summer. Make sure to check out other visualisation packages: [ggraph](https://github.com/thomasp85/ggraph), [ggforce](https://github.com/thomasp85/ggforce), and [tweenr](https://github.com/thomasp85/tweenr). |
| 40 | + |
| 41 | +## Subtitles and captions |
| 42 | + |
| 43 | +Thanks to [Bob Rudis](https://rud.is), you can now add subtitles and captions: |
| 44 | + |
| 45 | +```{r} |
| 46 | +ggplot(mpg, aes(displ, hwy)) + |
| 47 | + geom_point(aes(color = class)) + |
| 48 | + geom_smooth(se = FALSE, method = "loess") + |
| 49 | + labs( |
| 50 | + title = "Fuel efficiency generally decreases with engine size", |
| 51 | + subtitle = "Two seaters (sports cars) are an exception because of their light weight", |
| 52 | + caption = "Data from fueleconomy.gov" |
| 53 | + ) |
| 54 | +``` |
| 55 | + |
| 56 | +These are controlled by the theme settings `plot.subtitle` and `plot.caption`. |
| 57 | + |
| 58 | +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))`. |
| 59 | + |
| 60 | +## Facets |
| 61 | + |
| 62 | +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: |
| 63 | + |
| 64 | + * Functions in facetting formulas, thanks to |
| 65 | + [Dan Ruderman](https://github.com/DanRuderman). |
| 66 | + |
| 67 | + ```{r} |
| 68 | + ggplot(diamonds, aes(carat, price)) + |
| 69 | + geom_hex(bins = 20) + |
| 70 | + facet_wrap(~cut_number(depth, 9)) |
| 71 | + ``` |
| 72 | +
|
| 73 | + * Axes were dropped when the panels in `facet_wrap()` did not completely |
| 74 | + fill the rectangle. Now, an axis is drawn underneath the hanging panels: |
| 75 | + |
| 76 | + ```{r} |
| 77 | + ggplot(mpg, aes(displ, hwy)) + |
| 78 | + geom_point() + |
| 79 | + facet_wrap(~class) |
| 80 | + ``` |
| 81 | +
|
| 82 | + * It is now possible to set the position of the axes through the `position` |
| 83 | + argument in the scale constructor: |
| 84 | + |
| 85 | + ```{r} |
| 86 | + ggplot(mpg, aes(displ, hwy)) + |
| 87 | + geom_point() + |
| 88 | + scale_x_continuous(position = "top") + |
| 89 | + scale_y_continuous(position = "right") |
| 90 | + ``` |
| 91 | +
|
| 92 | + * It is now possible to display a secondary axis that is a linear |
| 93 | + transformation of the primary axis through the `sec.axis` argument: |
| 94 | + |
| 95 | + ```{r} |
| 96 | + ggplot(mpg, aes(displ, hwy)) + |
| 97 | + geom_point() + |
| 98 | + scale_y_continuous( |
| 99 | + "miles / galloon", |
| 100 | + sec.axis = sec_axis(~ 235 / ., name = "L / 100km") |
| 101 | + ) |
| 102 | + ``` |
| 103 | +
|
| 104 | + * Strips can be placed on any side, and the placement with respect to axes |
| 105 | + can be controlled with the `strip.placement` theme option. |
| 106 | + |
| 107 | + ```{r} |
| 108 | + ggplot(mpg, aes(displ, hwy)) + |
| 109 | + geom_point() + |
| 110 | + facet_wrap(~ drv, strip.position = "bottom") + |
| 111 | + theme( |
| 112 | + strip.placement = "outside", |
| 113 | + strip.background = element_blank(), |
| 114 | + strip.text = element_text(face = "bold") |
| 115 | + ) + |
| 116 | + xlab(NULL) |
| 117 | + ``` |
| 118 | +
|
| 119 | +## Theming |
| 120 | +
|
| 121 | +* Blank elements can now be overridden again so you get the expected |
| 122 | + behavior when setting e.g. `axis.line.x`. Furthermore, `element_line()` |
| 123 | + gets an `arrow` argument that lets you put arrows on axes. |
| 124 | + |
| 125 | + ```{r} |
| 126 | + arrow <- arrow(length = unit(0.4, "cm"), type = "closed") |
| 127 | + |
| 128 | + ggplot(mpg, aes(displ, hwy)) + |
| 129 | + geom_point() + |
| 130 | + theme_minimal() + |
| 131 | + theme( |
| 132 | + axis.line = element_line(arrow = arrow) |
| 133 | + ) |
| 134 | + ``` |
| 135 | +
|
| 136 | +* Control of legend styling has been improved. The whole legend area can be |
| 137 | + aligned according to the plot area and a box can be drawn around all legends: |
| 138 | + |
| 139 | + ```{r} |
| 140 | + ggplot(mpg, aes(displ, hwy, shape = drv, colour = class)) + |
| 141 | + geom_point() + |
| 142 | + theme( |
| 143 | + legend.justification = "top", |
| 144 | + legend.box.margin = margin(3, 3, 3, 3, "mm"), |
| 145 | + legend.box.background = element_rect(colour = "grey50") |
| 146 | + ) |
| 147 | + ``` |
| 148 | +
|
| 149 | +* `panel.margin` and `legend.margin` have been renamed to `panel.spacing` |
| 150 | + and `legend.spacing` respectively as this better indicates their roles. |
| 151 | + A new `legend.margin` has been actually controls the margin around each |
| 152 | + legend. |
| 153 | +
|
| 154 | +* When computing the height of titles ggplot2, now inclues the height of the |
| 155 | + descenders (i.e. the bits `g` and `y` that hang underneath). This makes |
| 156 | + improves the margins around titles, particularly the y axis label. I have |
| 157 | + also very slightly increased the inner margins of axis titles, and removed |
| 158 | + the outer margins. |
| 159 | +
|
| 160 | +* The default themes has been tweaked by |
| 161 | + [Jean-Olivier Irisson](http://www.obs-vlfr.fr/~irisson/) making them better |
| 162 | + match `theme_grey()`. |
| 163 | +
|
| 164 | +* Lastly, the `theme()` function now has named arguments so autocomplete |
| 165 | + and documentation suggestions are vastly improved. |
| 166 | +
|
| 167 | +## Stacking bars |
| 168 | + |
| 169 | +`position_stack()` and `position_fill()` now stacks values in the reverse |
| 170 | +order of the grouping, which makes the default stack order match the legend. |
| 171 | +
|
| 172 | +```{r} |
| 173 | +avg_price <- diamonds %>% |
| 174 | + group_by(cut, color) %>% |
| 175 | + summarise(price = mean(price)) %>% |
| 176 | + ungroup() %>% |
| 177 | + mutate(price_rel = price - mean(price)) |
| 178 | +
|
| 179 | +ggplot(avg_price) + |
| 180 | + geom_col(aes(x = cut, y = price, fill = color)) |
| 181 | +``` |
| 182 | + |
| 183 | +(Note also the new `geom_col()` which is short-hand for |
| 184 | +`geom_bar(stat = "identity")`, contributed by Bob Rudis.) |
| 185 | + |
| 186 | +Furthermore, you can now stack negative values: |
| 187 | + |
| 188 | +```{r} |
| 189 | +ggplot(avg_price) + |
| 190 | + geom_col(aes(x = cut, y = price_rel, fill = color)) |
| 191 | +``` |
| 192 | + |
| 193 | +The overall ordering cannot necessarily be matched in the presence of |
| 194 | +negative values, but the ordering on either side of the x-axis will match. |
| 195 | + |
| 196 | +If you want to stack in the opposite, order try |
| 197 | +[`forcats::fct_rev()`](http://forcats.tidyverse.org/reference/fct_rev.html): |
| 198 | + |
| 199 | +```{r} |
| 200 | +ggplot(avg_price) + |
| 201 | + geom_col(aes(x = cut, y = price, fill = fct_rev(color))) |
| 202 | +``` |
| 203 | + |
| 204 | +[news.md]: https://github.com/hadley/ggplot2/blob/master/NEWS.md |
0 commit comments