Skip to content

Commit f58e5ec

Browse files
authored
Fix resolution bug with empty panels (#6759)
* Fix bug * add test * Add news bullet
1 parent bc605b6 commit f58e5ec

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Suggests:
7070
rpart,
7171
sf (>= 0.7-3),
7272
svglite (>= 2.1.2),
73-
testthat (>= 3.1.5),
73+
testthat (>= 3.3.0),
7474
tibble,
7575
vdiffr (>= 1.0.6),
7676
xml2

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
(@lgaborini, #6724)
55
* Added new argument `geom_curve(shape)` that will be passed down to
66
`grid::curveGrob()` (@fmarotta, #5998).
7+
* Fixed a regression where default `width` was miscalculated when some panels
8+
are empty (@teunbrand, #6758)
79

810
# ggplot2 4.0.1
911

R/utilities.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ compute_data_size <- function(data, size, default = 0.9,
927927
panels <- arg_match0(panels, c("across", "by", "ignore"))
928928

929929
if (panels == "across") {
930-
res <- split(data[[var]], data$PANEL, drop = FALSE)
930+
res <- split(data[[var]], data$PANEL, drop = TRUE)
931931
res <- vapply(res, resolution, FUN.VALUE = numeric(1), ...)
932932
res <- min(res, na.rm = TRUE)
933933
} else if (panels == "by") {

tests/testthat/test-utilities.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,13 @@ test_that("fallback_palette finds palettes", {
197197
pal <- fallback_palette(sc)
198198
expect_true(is_discrete_pal(pal))
199199
})
200+
201+
test_that("compute_data_size handles gnarly cases", {
202+
# Test missing levels
203+
df <- data_frame0(
204+
x = seq(0, 20, by = 2),
205+
PANEL = factor(rep("B", 11), levels = c("A", "B"))
206+
)
207+
new <- compute_data_size(df, size = NULL, target = "width", default = 1)
208+
expect_all_equal(new$width, 2)
209+
})

0 commit comments

Comments
 (0)