Skip to content

Commit 9b640c5

Browse files
authored
Don't reorder data during faceting. (#2694)
* Don't change data order in facet_grid() or facet_wrap(). Enables data vectors to be provided via aes() or as parameters. * add news item
1 parent e0797dc commit 9b640c5

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

NEWS.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# ggplot2 3.0.0.9000
22

3-
* `geom_hex()` now understands the `size` and `linetype` aesthetics
4-
(@mikmart, #2488).
3+
* `geom_hex()` now understands the `size` and `linetype` aesthetics
4+
(@mikmart, #2488).
55

6+
* Data is no longer internally reordered when faceting. This makes it safer to
7+
feed data columns into `aes()` or into parameters of geoms or stats. However,
8+
doing so remains discouraged (@clauswilke).
9+
10+
611
# ggplot2 3.0.0
712

813
## Breaking changes

R/facet-grid-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
288288

289289
data$PANEL <- layout$PANEL[match(keys$x, keys$y)]
290290
}
291-
data[order(data$PANEL), , drop = FALSE]
291+
data
292292
},
293293
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
294294
if ((params$free$x || params$free$y) && !coord$is_free()) {

R/facet-wrap.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
199199
keys <- plyr::join.keys(facet_vals, layout, by = names(vars))
200200

201201
data$PANEL <- layout$PANEL[match(keys$x, keys$y)]
202-
data[order(data$PANEL), ]
202+
data
203203
},
204204
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
205205
if ((params$free$x || params$free$y) && !coord$is_free()) {

tests/testthat/test-facet-map.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test_that("grid: missing facet columns are duplicated", {
3131

3232
loc_a <- panel_map_one(facet, df_a, plot_data = df)
3333
expect_equal(nrow(loc_a), 4)
34-
expect_equal(loc_a$PANEL, factor(1:4))
34+
expect_equal(loc_a$PANEL, factor(c(1, 3, 2, 4)))
3535

3636
loc_b <- panel_map_one(facet, df_b, plot_data = df)
3737
expect_equal(nrow(loc_b), 4)
@@ -47,8 +47,8 @@ test_that("wrap: missing facet columns are duplicated", {
4747

4848
loc_a <- panel_map_one(facet, df_a, plot_data = df)
4949
expect_equal(nrow(loc_a), 4)
50-
expect_equal(loc_a$PANEL, factor(1:4))
51-
expect_equal(loc_a$a, c(1, 1, 2, 2))
50+
expect_equal(loc_a$PANEL, factor(c(1, 3, 2, 4)))
51+
expect_equal(loc_a$a, c(1, 2, 1, 2))
5252

5353
loc_b <- panel_map_one(facet, df_b, plot_data = df)
5454
expect_equal(nrow(loc_b), 4)

0 commit comments

Comments
 (0)