Skip to content

Commit ae02c68

Browse files
authored
Resolution of geom_tile() per panel (#5764)
* compute resolution per panel * add news bullet * add test
1 parent e72d1a1 commit ae02c68

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
* `theme()` gets new `spacing` and `margins` arguments that all other spacings
4343
and (non-text) margins inherit from (@teunbrand, #5622).
4444
* `geom_ribbon()` can have varying `fill` or `alpha` in linear coordinate
45-
systems (@teunbrand, #4690)
45+
systems (@teunbrand, #4690).
46+
* `geom_tile()` computes default widths and heights per panel instead of
47+
per layer (@teunbrand, #5740).
4648

4749
# ggplot2 3.5.1
4850

R/geom-tile.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ GeomTile <- ggproto("GeomTile", GeomRect,
109109
extra_params = c("na.rm"),
110110

111111
setup_data = function(data, params) {
112-
data$width <- data$width %||% params$width %||% resolution(data$x, FALSE, TRUE)
113-
data$height <- data$height %||% params$height %||% resolution(data$y, FALSE, TRUE)
112+
113+
data$width <- data$width %||% params$width %||%
114+
stats::ave(data$x, data$PANEL, FUN = function(x) resolution(x, FALSE, TRUE))
115+
data$height <- data$height %||% params$height %||%
116+
stats::ave(data$y, data$PANEL, FUN = function(y) resolution(y, FALSE, TRUE))
114117

115118
transform(data,
116119
xmin = x - width / 2, xmax = x + width / 2, width = NULL,

tests/testthat/test-geom-tile.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,18 @@ test_that("accepts linejoin parameter", {
3434
gp2 <- layer_grob(ggplot(df, aes(x, y)) + geom_tile(linejoin = "round"))[[1]]$gp
3535
expect_equal(gp2$linejoin, "round")
3636
})
37+
38+
test_that("width and height are inferred per panel", {
39+
df <- data_frame0(
40+
x = c(1, 2, 3, 10, 20, 30),
41+
y = c(10, 10.5, 11, 100, 200, 300),
42+
f = rep(c("A", "B"), each = 3)
43+
)
44+
45+
ld <- layer_data(
46+
ggplot(df, aes(x, y)) + geom_tile() + facet_wrap(~f, scales = "free")
47+
)
48+
49+
expect_equal(ld$xmax - ld$xmin, c(1, 1, 1, 10, 10, 10))
50+
expect_equal(ld$ymax - ld$ymin, c(0.5, 0.5, 0.5, 100, 100, 100))
51+
})

0 commit comments

Comments
 (0)