|
| 1 | +--- |
| 2 | +title: "ggplot2 2.0.0" |
| 3 | +--- |
| 4 | + |
| 5 | +```{r, include = FALSE} |
| 6 | +knitr::opts_chunk$set( |
| 7 | + comment = "#>", |
| 8 | + collapse = TRUE, |
| 9 | + fig.show = "hold", |
| 10 | + out.width = "50%", |
| 11 | + fig.width = 5, |
| 12 | + fig.asp = 2/3, |
| 13 | + fig.retina = NULL |
| 14 | +) |
| 15 | +library(ggplot2) |
| 16 | +library(dplyr) |
| 17 | +``` |
| 18 | + |
| 19 | +I'm very pleased to announce the release of ggplot2 2.0.0. I know I promised [that there wouldn't be any more updates](http://blog.rstudio.org/2015/01/09/ggplot2-updates/), but while working on the 2nd edition of the ggplot2 book, I just couldn't stop myself from fixing some long standing problems. |
| 20 | + |
| 21 | +On the scale of ggplot2 releases, this one is huge with over one hundred fixes and improvements. This might break some of your existing code (although I've tried to minimise breakage as much as possible), but I hope the new features make up for any short term hassle. This blog post documents the most important changes: |
| 22 | + |
| 23 | +* ggplot2 now has an official extension mechanism. |
| 24 | + |
| 25 | +* There are a handful of new geoms, and updates to existing geoms. |
| 26 | + |
| 27 | +* The default appearance has been thoroughly tweaked so most plots should |
| 28 | + look better. |
| 29 | + |
| 30 | +* Facets have a much richer set of labelling options. |
| 31 | + |
| 32 | +* The documentation has been overhauled to be more helpful, and require |
| 33 | + less integration across multiple pages. |
| 34 | + |
| 35 | +* A number of older and less used features have been deprecated. |
| 36 | + |
| 37 | +These are described in more detail below. See the [release notes](https://github.com/hadley/ggplot2/releases/tag/v2.0.0) for a complete list of all changes. |
| 38 | + |
| 39 | +## Extensibility |
| 40 | + |
| 41 | +Perhaps the bigggest news in this release is that ggplot2 now has an official extension mechanism. This means that others can now easily create their on stats, geoms and positions, and provide them in other packages. This should allow the ggplot2 community to flourish, even as less development work happens in ggplot2 itself. See [`vignette("extending-ggplot2")`](https://cran.r-project.org/web/packages/ggplot2/vignettes/extending-ggplot2.html) for details. |
| 42 | + |
| 43 | +Coupled with this change, ggplot2 no longer uses proto or reference classes. Instead, we now use ggproto, a new OO system designed specifically for ggplot2. Unlike proto and RC, ggproto supports clean cross-package inheritance, which is necessary for extensibility. Creating a new OO system isn't usually the right solution, but I'm pretty sure it was necessary here. Read more about it in the vignette. |
| 44 | + |
| 45 | +## New and updated geoms |
| 46 | + |
| 47 | +* ggplot no longer throws an error if you your plot has no layers. Instead it |
| 48 | + automatically adds `geom_blank()`: |
| 49 | + |
| 50 | + ```{r} |
| 51 | + ggplot(mpg, aes(cyl, hwy)) |
| 52 | + ``` |
| 53 | + |
| 54 | +* `geom_count()` (a new alias for the old `stat_sum()`) counts the number of |
| 55 | + points at unique locations on a scatterplot, and maps the size of the point |
| 56 | + to the count: |
| 57 | + |
| 58 | + ```{r} |
| 59 | + ggplot(mpg, aes(cty, hwy)) + |
| 60 | + geom_point() |
| 61 | + ggplot(mpg, aes(cty, hwy)) + |
| 62 | + geom_count() |
| 63 | + ``` |
| 64 | +
|
| 65 | +* `geom_curve()` draws curved lines in the same way that `geom_segment()` |
| 66 | + draws straight lines: |
| 67 | + |
| 68 | + ```{r} |
| 69 | + df <- expand.grid(x = 1:2, y = 1:2) |
| 70 | + ggplot(df, aes(x, y, xend = x + 0.5, yend = y + 0.5)) + |
| 71 | + geom_curve(aes(colour = "curve")) + |
| 72 | + geom_segment(aes(colour = "segment")) |
| 73 | + ``` |
| 74 | +
|
| 75 | +* `geom_bar()` now behaves differently from `geom_histogram()`. Instead of |
| 76 | + binning the data, it counts the number of unique observations at each |
| 77 | + location: |
| 78 | + |
| 79 | + ```{r} |
| 80 | + ggplot(mpg, aes(cyl)) + |
| 81 | + geom_bar() |
| 82 | + |
| 83 | + ggplot(mpg, aes(cyl)) + |
| 84 | + geom_histogram(binwidth = 1) |
| 85 | + ``` |
| 86 | + |
| 87 | + If you got into the (bad) habit of using `geom_histogram()` to create bar |
| 88 | + charts, or `geom_bar()` to create histograms, you'll need to switch. |
| 89 | +
|
| 90 | +* Layers are now much stricter about their arguments - you will get an error |
| 91 | + if you've supplied an argument that isn't an aesthetic or a parameter. |
| 92 | + This breaks the handful of geoms/stats that used `...` to pass |
| 93 | + additional arguments on to the underlying computation. Now |
| 94 | + `geom_smooth()`/`stat_smooth()` and `geom_quantile()`/`stat_quantile()` |
| 95 | + use `method.args` instead; and `stat_summary()`, `stat_summary_hex()`, |
| 96 | + and `stat_summary2d()` use `fun.args`. This is likely to cause some |
| 97 | + short-term pain but in the long-term it will make it much easier to spot |
| 98 | + spelling mistakes and other errors. |
| 99 | +
|
| 100 | +* `geom_text()` has been overhauled to make labelling your data a little |
| 101 | + easier. You can use `nudge_x` and `nudge_y` arguments to offset labels from |
| 102 | + their corresponding points. `check_overlap = TRUE` provides a simple way |
| 103 | + to avoid overplotting of labels: labels that would otherwise overlap are |
| 104 | + omitted. |
| 105 | +
|
| 106 | + ```{r} |
| 107 | + ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) + |
| 108 | + geom_point() + |
| 109 | + geom_text(nudge_y = 0.5, check_overlap = TRUE) |
| 110 | + ``` |
| 111 | + |
| 112 | + (Labelling points well is still a huge pain, but at least these new |
| 113 | + features make life a lit better.) |
| 114 | +
|
| 115 | +* `geom_label()` works like `geom_text()` but draws a rounded rectangle |
| 116 | + underneath each label: |
| 117 | + |
| 118 | + ```{r, fig.asp = 1} |
| 119 | + grid <- expand.grid( |
| 120 | + x = seq(-pi, pi, length = 50), |
| 121 | + y = seq(-pi, pi, length = 50) |
| 122 | + ) %>% mutate(r = x ^ 2 + y ^ 2, z = cos(r ^ 2) * exp(-r / 6)) |
| 123 | + |
| 124 | + ggplot(grid, aes(x, y)) + |
| 125 | + geom_raster(aes(fill = z)) + |
| 126 | + geom_label(data = data.frame(x = 0, y = 0), label = "Center") + |
| 127 | + theme(legend.position = "none") + |
| 128 | + coord_fixed() |
| 129 | + ``` |
| 130 | +
|
| 131 | +* `aes_()` replaces `aes_q()`, and works like the SE functions in dplyr |
| 132 | + and my other recent packages. It supports formulas, so the most concise |
| 133 | + SE version of `aes(carat, price)` is now `aes_(~carat, ~price)`. You may |
| 134 | + want to use this form in packages, as it will avoid spurious `R CMD check` |
| 135 | + warnings about undefined global variables. |
| 136 | + |
| 137 | + ```{r, eval = FALSE} |
| 138 | + ggplot(mpg, aes_(~displ, ~cty)) + |
| 139 | + geom_point() |
| 140 | + # Same as |
| 141 | + ggplot(mpg, aes(displ, cty)) + |
| 142 | + geom_point() |
| 143 | + ``` |
| 144 | +
|
| 145 | +## Appearance |
| 146 | +
|
| 147 | +I've made a number of small tweaks to the default appearance: |
| 148 | +
|
| 149 | +* The default `theme_grey()` background colour has been changed from "grey90" |
| 150 | + to "grey92": this makes the background a little less visually prominent. |
| 151 | + |
| 152 | +* Labels and titles have been tweaked for readability. Axis labels are darker, |
| 153 | + and legend titles get the same visual treatment as axis labels. |
| 154 | + |
| 155 | +* The default font size dropped from 12 to 11. You might be surprised that |
| 156 | + I've made the default text size smaller as it was already hard for |
| 157 | + many people to read. It turns out there was a bug in RStudio ([fixed in |
| 158 | + 0.99.724](https://www.rstudio.com/products/rstudio/download/preview/)), |
| 159 | + that shrunk the text of all grid based graphics. Once that was resolved |
| 160 | + the defaults seemed too big to my eyes. |
| 161 | +
|
| 162 | +* `scale_size()` now maps values to _area_, not radius. Use `scale_radius()` |
| 163 | + if you want the old behaviour (not recommended, except perhaps for lines). |
| 164 | + Continue to use `scale_size_area()` if you want 0 values to have 0 area. |
| 165 | +
|
| 166 | +* Bar and rectangle legends no longer get a diagonal line. Instead, the |
| 167 | + border has been tweaked to make it visible, and more closely match the |
| 168 | + size of line drawn on the plot. |
| 169 | + |
| 170 | + ```{r} |
| 171 | + ggplot(mpg, aes(factor(cyl), fill = drv)) + |
| 172 | + geom_bar(colour = "black", size = 1) + |
| 173 | + coord_flip() |
| 174 | + ``` |
| 175 | +
|
| 176 | +* `geom_point()` now uses shape 19 instead of 16. This looks much better on |
| 177 | + the default Linux graphics device. (It's very slightly smaller than the old |
| 178 | + point, but it shouldn't affect any graphics significantly). You can now |
| 179 | + control the width of the outline on shapes 21-25 with the `stroke` |
| 180 | + parameter. |
| 181 | + |
| 182 | +* The default legend will now allocate multiple rows (if vertical) or |
| 183 | + columns (if horizontal) in order to make a legend that is more likely to |
| 184 | + fit on the screen. You can override with the `nrow`/`ncol` arguments |
| 185 | + to `guide_legend()` |
| 186 | +
|
| 187 | + ```{r} |
| 188 | + p <- ggplot(mpg, aes(displ,hwy, colour = manufacturer)) + |
| 189 | + geom_point() + |
| 190 | + theme(legend.position = "bottom") |
| 191 | + p |
| 192 | + # Revert back to previous behaviour |
| 193 | + p + guides(colour = guide_legend(nrow = 1)) |
| 194 | + ``` |
| 195 | +
|
| 196 | +* Two new themes were contributed by |
| 197 | + [Jean-Olivier Irisson](http://github.com/jiho): `theme_void()` is completely |
| 198 | + empty and `theme_dark()` has a dark background designed to make colours pop |
| 199 | + out. |
| 200 | +
|
| 201 | +## Facet labels |
| 202 | +
|
| 203 | +Thanks to the work of [Lionel Henry](https://github.com/lionel-), facet labels have received three major improvements: |
| 204 | +
|
| 205 | +1. You can switch the position of facet labels so they're next to the axes. |
| 206 | +
|
| 207 | +1. `facet_wrap()` now supports custom labellers. |
| 208 | +
|
| 209 | +1. You can create combined labels when facetting by multiple variables. |
| 210 | +
|
| 211 | +### Switching the labels |
| 212 | +
|
| 213 | +The new `switch` argument allows you to switch the labels to display near the axes: |
| 214 | +
|
| 215 | +```{r} |
| 216 | +data <- transform(mtcars, |
| 217 | + am = factor(am, levels = 0:1, c("Automatic", "Manual")), |
| 218 | + gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five")) |
| 219 | +) |
| 220 | +
|
| 221 | +ggplot(data, aes(mpg, disp)) + |
| 222 | + geom_point() + |
| 223 | + facet_grid(am ~ gear, switch = "both") |
| 224 | +``` |
| 225 | + |
| 226 | +This is especially useful when the labels directly characterise the axes. In that situation, switching the labels can make the plot clearer and more readable. You may also want to use a neutral label background by setting `strip.background` to `element_blank()`: |
| 227 | + |
| 228 | +```{r} |
| 229 | +data <- mtcars %>% |
| 230 | + mutate( |
| 231 | + Logarithmic = log(mpg), |
| 232 | + Inverse = 1 / mpg, |
| 233 | + Cubic = mpg ^ 3, |
| 234 | + Original = mpg |
| 235 | +) %>% tidyr::gather(transformation, mpg2, Logarithmic:Original) |
| 236 | +
|
| 237 | +ggplot(data, aes(mpg2, disp)) + |
| 238 | + geom_point() + |
| 239 | + facet_wrap(~transformation, scales = "free", switch = "x") + |
| 240 | + theme(strip.background = element_blank()) |
| 241 | +``` |
| 242 | + |
| 243 | +### Wrap labeller |
| 244 | + |
| 245 | +A longstanding issue in ggplot was that `facet_wrap()` did not support |
| 246 | +custom labellers. Labellers are small functions that make it easy to |
| 247 | +customise the labels. You can now supply labellers to both wrap and |
| 248 | +grid facets: |
| 249 | + |
| 250 | +```{r} |
| 251 | +ggplot(data, aes(mpg2, disp)) + |
| 252 | + geom_point() + |
| 253 | + facet_wrap(~transformation, scales = "free", labeller = "label_both") |
| 254 | +``` |
| 255 | + |
| 256 | +### Composite margins |
| 257 | + |
| 258 | +Labellers have now better support for composite margins when you facet over multiple variable with `+`. All labellers gain a `multi_line` argument to control whether labels should be displayed as a single line or over multiple lines, one for each factor. |
| 259 | + |
| 260 | +The labellers still work the same way except for `label_bquote()`. That labeller makes it easy to write mathematical expression involving the values of facetted factors. Historically, `label_bquote()` could only specify a single expression for all margins and factor. The factor value was referred to via the backquoted placeholder `.(x)`. Now that it supports expressions combining multiple factors, you must backquote the variable names themselves. In addition, you can provide different expressions for each margin: |
| 261 | + |
| 262 | +```{r} |
| 263 | +my_labeller <- label_bquote( |
| 264 | + rows = .(am) / alpha, |
| 265 | + cols = .(vs) ^ .(cyl) |
| 266 | +) |
| 267 | +
|
| 268 | +ggplot(mtcars, aes(wt, mpg)) + |
| 269 | + geom_point() + |
| 270 | + facet_grid(am ~ vs + cyl, labeller = my_labeller) |
| 271 | +``` |
| 272 | + |
| 273 | +## Documentation |
| 274 | + |
| 275 | +I've given the documentation a thorough overhaul: |
| 276 | + |
| 277 | +* Tighly linked geoms and stats (e.g. `geom_boxplot()` and `stat_boxplot()`) |
| 278 | + are now documented in the same file so you can see all the arguments in one |
| 279 | + place. Similarly, variations on a theme (like `geom_path()`, `geom_line()`, and |
| 280 | + `geom_step()`) are documented together. |
| 281 | + |
| 282 | +* I've tried to reduce the use of `...` so that you can see all the |
| 283 | + documentation in one place rather than having to follow links around. |
| 284 | + In some cases this has involved adding additional arguments to geoms |
| 285 | + to make it more clear what you can do. |
| 286 | + |
| 287 | +* Thanks to [Bob Rudis](https://github.com/hrbrmstr), the use of `qplot()` |
| 288 | + in examples has been grealy reduced. This is inline with the 2nd edition |
| 289 | + of the ggplot2 book, which eliminates `qplot()` in favour of `ggplot()`. |
| 290 | + |
| 291 | +## Deprecated features |
| 292 | + |
| 293 | +* The `order` aesthetic is officially deprecated. It never really worked, and |
| 294 | + was poorly documented. |
| 295 | + |
| 296 | +* The `stat` and `position` arguments to `qplot()` have been deprecated. |
| 297 | + `qplot()` is designed for quick plots - if you need to specify position |
| 298 | + or stat, use `ggplot()` instead. |
| 299 | + |
| 300 | +* The theme setting `axis.ticks.margin` has been deprecated: now use the margin |
| 301 | + property of `axis.ticks`. |
| 302 | + |
| 303 | +* `stat_abline()`, `stat_hline()` and `stat_vline()` have been removed: |
| 304 | + these were never suitable for use other than with their corresponding |
| 305 | + geoms and were not documented. |
| 306 | + |
| 307 | +* `show_guide` has been renamed to `show.legend`: this more accurately |
| 308 | + reflects what it does (controls appearance of layer in legend), and uses the |
| 309 | + same convention as other ggplot2 arguments (i.e. a `.` between names). |
| 310 | + (Yes, I know that's inconsistent with function names (which use `_`) but it's |
| 311 | + too late to change now.) |
| 312 | + |
| 313 | +A number of geoms have been renamed to be more consistent. The previous names will continue to work for the forseeable future, but you should switch to the new names for new work. |
| 314 | + |
| 315 | +* `stat_binhex()` and `stat_bin2d()` have been renamed to `stat_bin_hex()` |
| 316 | + and `stat_bin_2d()`. `stat_summary2d()` has been renamed to |
| 317 | + `stat_summary_2d()`, `geom_density2d()`/`stat_density2d()` has been renamed |
| 318 | + to `geom_density_2d()`/`stat_density_2d()`. |
| 319 | + |
| 320 | +* `stat_spoke()` is now `geom_spoke()` since I realised it's a |
| 321 | + reparameterisation of `geom_segment()`. |
| 322 | + |
| 323 | +* `stat_bindot()` has been removed because it's so tightly coupled to |
| 324 | + `geom_dotplot()`. If you happened to use `stat_bindot()`, just change to |
| 325 | + `geom_dotplot()`. |
| 326 | + |
| 327 | +All defunct functions have been removed. |
0 commit comments