Skip to content

Commit ef33dc7

Browse files
committed
Merge branch 'master' of github.com:hadley/ggplot2
2 parents 94fa853 + 864d64f commit ef33dc7

File tree

8 files changed

+109
-40
lines changed

8 files changed

+109
-40
lines changed

NEWS.md

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -210,33 +210,69 @@ All defunct functions have been removed.
210210
211211
### Labelling
212212
213-
* `facet_wrap()` gains a `labeller` option (@lionel-, #25).
214-
215-
* The labeller API has been updated to offer more control when facetting over
216-
multiple factors (e.g. with formulae such as `~cyl + am`). Previously, a
217-
labeller function would take `variable` and `value` arguments and return a
218-
character vector.
219-
220-
Now, they take a data frame of character vectors and return a list. The input
221-
data frame has one column per factor facetted over. Each column in the
222-
returned list becomes one line in the strip label. See documentation for more
223-
details.
224-
225-
* Labellers offer the `multi_line` argument to control whether to display one
226-
or multiple lines.
227-
228-
* Referring to `x` in backquoted expressions with `label_bquote()` is
229-
deprecated. You can now refer to the variable names instead. (@lionel-)
230-
231-
* `label_bquote()` and `labeller()` now take `rows` and `cols` arguments. They
232-
allow rows and columns labels to have specific plotmath expressions or
233-
labellers.
234-
235-
* New labeller `label_context()` which behaviour differently based on the
236-
on the number of factors facetted over. With a single factor, it displays
237-
only the values, just as before. But with multiple factors (e.g. with
238-
`~cyl + am`), the labels are dispatched to `label_both()` to display both the
239-
variables and the values and make the plot clearer. (@lionel-)
213+
The facet labelling system was updated with many new features and a
214+
more flexible interface (@lionel-). It now works consistently across
215+
grid and wrap facets. The most important user visible changes are:
216+
217+
* `facet_wrap()` gains a `labeller` option (#25).
218+
219+
* `facet_grid()` and `facet_wrap()` gain a `switch` argument to
220+
display the facet titles near the axes. When switched, the labels
221+
become axes subtitles. `switch` can be set to "x", "y" or "both"
222+
(the latter only for grids) to control which margin is switched.
223+
224+
The labellers (such as `label_value()` or `label_both()`) also get
225+
some new features:
226+
227+
* They now offer the `multi_line` argument to control whether to
228+
display composite facets (those specified as `~var1 + var2`) on one
229+
or multiple lines.
230+
231+
* In `label_bquote()` you now refer directly to the names of
232+
variables. With this change, you can create math expressions that
233+
depend on more than one variable. This math expression can be
234+
specified either for the rows or the columns and you can also
235+
provide different expressions to each margin.
236+
237+
As a consequence of these changes, referring to `x` in backquoted
238+
expressions is deprecated.
239+
240+
* Similarly to `label_bquote()`, `labeller()` now take `.rows` and
241+
`.cols` arguments. In addition, it also takes `.default`.
242+
`labeller()` is useful to customise how particular variables are
243+
labelled. The three additional arguments specify how to label the
244+
variables are not specifically mentioned, respectively for rows,
245+
columns or both. This makes it especially easy to set up a
246+
project-wide labeller dispatcher that can be reused across all your
247+
plots. See the documentation for an example.
248+
249+
* The new labeller `label_context()` adapts to the number of factors
250+
facetted over. With a single factor, it displays only the values,
251+
just as before. But with multiple factors in a composite margin
252+
(e.g. with `~cyl + am`), the labels are passed over to
253+
`label_both()`. This way the variables names are displayed with the
254+
values to help identifying them.
255+
256+
On the programming side, the labeller API has been rewritten in order
257+
to offer more control when facetting over multiple factors (e.g. with
258+
formulae such as `~cyl + am`). This also means that if you have
259+
written custom labellers, you will need to update them for this
260+
version of ggplot.
261+
262+
* Previously, a labeller function would take `variable` and `value`
263+
arguments and return a character vector. Now, they take a data frame
264+
of character vectors and return a list. The input data frame has one
265+
column per factor facetted over and each column in the returned list
266+
becomes one line in the strip label. See documentation for more
267+
details.
268+
269+
* The labels received by a labeller now contain metadata: their margin
270+
(in the "type" attribute) and whether they come from a wrap or a
271+
grid facet (in the "facet" attribute).
272+
273+
* Note that the new `as_labeller()` function operator provides an easy
274+
way to transform an existing function to a labeller function. The
275+
existing function just needs to take and return a character vector.
240276
241277
## Documentation
242278
@@ -321,11 +357,6 @@ All defunct functions have been removed.
321357
* `facet_wrap()` and `facet_grid()` now allow you to use non-standard
322358
variable names by surrounding them with backticks (#1067).
323359

324-
* `facet_grid()` and `facet_wrap()` gain a `switch` argument that
325-
allows the facet titles to be displayed near the axes. They then act
326-
as axes subtitles. Can be set to "x", "y" or "both" (the latter only
327-
for grids) to control which label strips are switched. (@lionel-)
328-
329360
* `facet_wrap()` more carefully checks its `nrow` and `ncol` arguments
330361
to ensure that they're specified correctly (@richierocks, #962)
331362

R/facet-grid-.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed
221221
stop("Must specify at least one variable to facet by", call. = FALSE)
222222
}
223223

224+
# Check for deprecated labellers
225+
labeller <- check_labeller(labeller)
226+
224227
facet(
225228
rows = rows, cols = cols, margins = margins, shrink = shrink,
226229
free = free, space_free = space_free, labeller = labeller,

R/facet-labels.r

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,21 @@ adjust_angle <- function(angle) {
555555
angle + 180
556556
}
557557
}
558+
559+
# Check for old school labeller
560+
check_labeller <- function(labeller) {
561+
labeller <- match.fun(labeller)
562+
is_deprecated <- all(c("variable", "value") %in% names(formals(labeller)))
563+
564+
if (is_deprecated) {
565+
old_labeller <- labeller
566+
labeller <- function(labels) {
567+
Map(old_labeller, names(labels), labels)
568+
}
569+
warning("The labeller API has been updated. Labellers taking `variable`",
570+
"and `value` arguments are now deprecated. See labellers documentation.",
571+
call. = FALSE)
572+
}
573+
574+
labeller
575+
}

R/facet-wrap.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
9393
ncol <- sanitise_dim(ncol)
9494
}
9595

96+
# Check for deprecated labellers
97+
labeller <- check_labeller(labeller)
98+
9699
facet(
97100
facets = as.quoted(facets), free = free, shrink = shrink,
98101
as.table = as.table, switch = switch,

tests/testthat/test-facet-labels.r

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,17 @@ test_that("as_labeller() deals with non-labellers", {
133133
p2 <- p + facet_wrap(~am, labeller = labeller(am = function(x) paste0(x, "-foo")))
134134
expect_equal(get_labels_matrix(p2), cbind(c("0-foo", "1-foo")))
135135
})
136+
137+
test_that("old school labellers still work", {
138+
my_labeller <- function(variable, value) {
139+
paste0("var = ", as.character(value))
140+
}
141+
142+
expect_warning(p <-
143+
ggplot(mtcars, aes(disp, drat)) +
144+
geom_point() +
145+
facet_grid(~cyl, labeller = my_labeller))
146+
147+
expected_labels <- cbind(paste("var =", c(4, 6, 8)))
148+
expect_identical(get_labels_matrix(p, "cols"), expected_labels)
149+
})

visual_test/lines.r

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,26 @@ nzmap + geom_abline(intercept = 130, slope = -1) + coord_map()
7474
save_vtest("geom_abline: intercept=130, slope=-1 projection=mercator")
7575

7676

77-
nzmap + geom_hline(yintercept = -45) + coord_map(project = "cylindrical")
77+
nzmap + geom_hline(yintercept = -45) + coord_map(projection = "cylindrical")
7878
save_vtest("geom_hline: intercept=-45, projection=cylindrical")
7979

80-
nzmap + geom_vline(xintercept = 172) + coord_map(project = "cylindrical")
80+
nzmap + geom_vline(xintercept = 172) + coord_map(projection = "cylindrical")
8181
save_vtest("geom_vline: intercept=172, projection=cylindrical")
8282

83-
nzmap + geom_abline(intercept = 130, slope = -1) + coord_map(project = "cylindrical")
83+
nzmap + geom_abline(intercept = 130, slope = -1) + coord_map(projection = "cylindrical")
8484
save_vtest("geom_abline: intercept=130, slope=-1, projection=cylindrical")
8585

8686

8787
nzmap + geom_hline(yintercept = -45) +
88-
coord_map(project = 'azequalarea', orientation = c(-36.92, 174.6, 0))
88+
coord_map(projection = 'azequalarea', orientation = c(-36.92, 174.6, 0))
8989
save_vtest("geom_hline: intercept=-45, projection=azequalarea")
9090

9191
nzmap + geom_vline(xintercept = 172) +
92-
coord_map(project = 'azequalarea', orientation = c(-36.92, 174.6, 0))
92+
coord_map(projection = 'azequalarea', orientation = c(-36.92, 174.6, 0))
9393
save_vtest("geom_vline: intercept=172, projection=azequalara")
9494

9595
nzmap + geom_abline(intercept = 130, slope = -1) +
96-
coord_map(project = 'azequalarea', orientation = c(-36.92, 174.6, 0))
96+
coord_map(projection = 'azequalarea', orientation = c(-36.92, 174.6, 0))
9797
save_vtest("geom_abline: intercept=130, slope=-1, projection=azequalarea")
9898

9999
end_vcontext()

visual_test/minor-breaks.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ save_vtest("manual minor breaks with coord_polar")
1111

1212
set.seed(342)
1313
df <- data.frame(
14-
date = seq(as.Date("2012-2-29"), len = 100, by = "1 day")[sample(100, 50)],
14+
date = seq(as.Date("2012-2-29"), length.out = 100, by = "1 day")[sample(100, 50)],
1515
price = runif(50)
1616
)
1717
df <- df[order(df$date), ]

visual_test/violin.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ggplot(dat, aes(x = as.numeric(1), y = y)) + geom_violin()
4242
save_vtest("continuous x axis, single group (center should be at 1.0)")
4343

4444

45-
ggplot(dat, aes(x=x, y=y)) + geom_violin(quantiles=c(0.25,0.5,0.75))
45+
ggplot(dat, aes(x=x, y=y)) + geom_violin(draw_quantiles=c(0.25,0.5,0.75))
4646
save_vtest("quantiles")
4747

4848
dat2 <- data.frame(x = LETTERS[1:3], y = rnorm(90), g = letters[5:6])

0 commit comments

Comments
 (0)