Skip to content

Commit 3a7ae74

Browse files
authored
facet_grid(space = "free") can work with coord_fixed() (#5977)
* allow coord aspect ratio when space is free * add test * add news bullet
1 parent c38606f commit 3a7ae74

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
(@teunbrand, #5945).
155155
* (internal) The summary function of `stat_summary()` and `stat_summary_bin()`
156156
is setup once in total instead of once per group (@teunbrand, #5971)
157+
* `facet_grid(space = "free")` can now be combined with `coord_fixed()`
158+
(@teunbrand, #4584).
157159

158160
# ggplot2 3.5.1
159161

R/facet-.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,22 @@ Facet <- ggproto("Facet", NULL,
139139
free <- params$free %||% list(x = FALSE, y = FALSE)
140140
space <- params$space_free %||% list(x = FALSE, y = FALSE)
141141

142-
if ((free$x || free$y) && !coord$is_free()) {
143-
cli::cli_abort(
144-
"{.fn {snake_class(self)}} can't use free scales with \\
145-
{.fn {snake_class(coord)}}."
146-
)
147-
}
148-
149142
aspect_ratio <- theme$aspect.ratio
150143
if (!is.null(aspect_ratio) && (space$x || space$y)) {
151144
cli::cli_abort("Free scales cannot be mixed with a fixed aspect ratio.")
152145
}
153146

147+
if (!coord$is_free()) {
148+
if (space$x && space$y) {
149+
aspect_ratio <- aspect_ratio %||% coord$ratio
150+
} else if (free$x || free$y) {
151+
cli::cli_abort(
152+
"{.fn {snake_class(self)}} can't use free scales with \\
153+
{.fn {snake_class(coord)}}."
154+
)
155+
}
156+
}
157+
154158
table <- self$init_gtable(
155159
panels, layout, theme, ranges, params,
156160
aspect_ratio = aspect_ratio %||% coord$aspect(ranges[[1]])
@@ -219,7 +223,7 @@ Facet <- ggproto("Facet", NULL,
219223
if (space$y) {
220224
idx <- layout$PANEL[layout$COL == 1]
221225
heights <- vapply(idx, function(i) diff(ranges[[i]]$y.range), numeric(1))
222-
heights <- unit(heights, "null")
226+
heights <- unit(heights * abs(aspect_ratio %||% 1), "null")
223227
}
224228

225229
# Build gtable

tests/testthat/test-facet-layout.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ test_that("facet_grid throws errors at bad layout specs", {
253253
expect_snapshot_error(ggplotGrob(p))
254254
})
255255

256+
test_that("facet_grid can respect coord aspect with free scales/space", {
257+
df <- expand.grid(x = letters[1:6], y = LETTERS[1:3])
258+
p <- ggplot(df, aes(x, y)) +
259+
geom_tile() +
260+
facet_grid(
261+
rows = vars(y == "C"),
262+
cols = vars(x %in% c("e", "f")),
263+
scales = "free", space = "free"
264+
) +
265+
coord_fixed(3, expand = FALSE)
266+
gt <- ggplotGrob(p)
267+
width <- gt$widths[panel_cols(gt)$l]
268+
height <- gt$heights[panel_rows(gt)$t]
269+
expect_equal(as.numeric(width), c(4, 2))
270+
expect_equal(as.numeric(height), c(6, 3))
271+
})
272+
256273
test_that("facet_wrap and facet_grid throws errors when using reserved words", {
257274
mtcars2 <- mtcars
258275
mtcars2$PANEL <- mtcars2$cyl

0 commit comments

Comments
 (0)