Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facet_grid(space = "free") can work with coord_fixed() #5977

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
(@teunbrand, #5945).
* (internal) The summary function of `stat_summary()` and `stat_summary_bin()`
is setup once in total instead of once per group (@teunbrand, #5971)
* `facet_grid(space = "free")` can now be combined with `coord_fixed()`
(@teunbrand, #4584).

# ggplot2 3.5.1

Expand Down
20 changes: 12 additions & 8 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down Expand Up @@ -219,7 +223,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
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-facet-layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,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
Expand Down
Loading