From e9e6bd57930d23767304b0790979cd8264e0a1ce Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 5 Jul 2024 14:14:33 +0200 Subject: [PATCH 1/3] allow coord aspect ratio when space is free --- R/facet-.R | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/R/facet-.R b/R/facet-.R index f985d84afc..d1526d375a 100644 --- a/R/facet-.R +++ b/R/facet-.R @@ -139,18 +139,22 @@ Facet <- ggproto("Facet", NULL, free <- params$free %||% list(x = FALSE, y = FALSE) space <- params$space_free %||% list(x = FALSE, y = FALSE) - if ((free$x || free$y) && !coord$is_free()) { - cli::cli_abort( - "{.fn {snake_class(self)}} can't use free scales with \\ - {.fn {snake_class(coord)}}." - ) - } - aspect_ratio <- theme$aspect.ratio if (!is.null(aspect_ratio) && (space$x || space$y)) { cli::cli_abort("Free scales cannot be mixed with a fixed aspect ratio.") } + if (!coord$is_free()) { + if (space$x && space$y) { + aspect_ratio <- aspect_ratio %||% coord$ratio + } else if (free$x || free$y) { + cli::cli_abort( + "{.fn {snake_class(self)}} can't use free scales with \\ + {.fn {snake_class(coord)}}." + ) + } + } + table <- self$init_gtable( panels, layout, theme, ranges, params, aspect_ratio = aspect_ratio %||% coord$aspect(ranges[[1]]), @@ -220,7 +224,7 @@ Facet <- ggproto("Facet", NULL, if (space$y) { idx <- layout$PANEL[layout$COL == 1] heights <- vapply(idx, function(i) diff(ranges[[i]]$y.range), numeric(1)) - heights <- unit(heights, "null") + heights <- unit(heights * abs(aspect_ratio %||% 1), "null") } # Build gtable From b472cf5bcfd66d34d7e8f5b3e0d33b744d625008 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 5 Jul 2024 14:21:33 +0200 Subject: [PATCH 2/3] add test --- tests/testthat/test-facet-layout.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testthat/test-facet-layout.R b/tests/testthat/test-facet-layout.R index 70a4ed30e8..8bfe3fb809 100644 --- a/tests/testthat/test-facet-layout.R +++ b/tests/testthat/test-facet-layout.R @@ -232,6 +232,23 @@ test_that("facet_grid throws errors at bad layout specs", { expect_snapshot_error(ggplotGrob(p)) }) +test_that("facet_grid can respect coord aspect with free scales/space", { + df <- expand.grid(x = letters[1:6], y = LETTERS[1:3]) + p <- ggplot(df, aes(x, y)) + + geom_tile() + + facet_grid( + rows = vars(y == "C"), + cols = vars(x %in% c("e", "f")), + scales = "free", space = "free" + ) + + coord_fixed(3, expand = FALSE) + gt <- ggplotGrob(p) + width <- gt$widths[panel_cols(gt)$l] + height <- gt$heights[panel_rows(gt)$t] + expect_equal(as.numeric(width), c(4, 2)) + expect_equal(as.numeric(height), c(6, 3)) +}) + test_that("facet_wrap and facet_grid throws errors when using reserved words", { mtcars2 <- mtcars mtcars2$PANEL <- mtcars2$cyl From c72a1757fba9eb77c7d50c683d0a63c471a7c904 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 5 Jul 2024 14:22:27 +0200 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index d553c7f924..b74aa83983 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `facet_grid(space = "free")` can now be combined with `coord_fixed()` + (@teunbrand, #4584). * `stat_bin()` now accepts functions for argument `breaks` (@aijordan, #4561) * (internal) The plot's layout now has a coord parameter that is used to prevent setting up identical panel parameters (#5427)