Skip to content

Commit a11f786

Browse files
committed
Support width and height aesthetics in geom_tile
Thanks to @malcolmbarrett. Closes #2510
1 parent 13d050c commit a11f786

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ up correct aspect ratio, and draws a graticule.
244244
to pass pre-computed values without manual ordering (@izahn, #2028).
245245
It also now knows it has `ymin` and `ymax` aesthetics (#1939).
246246

247+
* `geom_tile()` now once again interprets `width` and `height` correctly
248+
(@malcolmbarrett, #2510)
249+
247250
* `position_dodge()` gains an `preserve` argument that allows you to control
248251
whether the `total` width at each `x` value is preserved (the current
249252
default), or ensure that the width of a `single` element is preserved

R/geom-tile.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#' w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)
3131
#' )
3232
#' ggplot(df, aes(x, y)) +
33-
#' geom_tile(aes(fill = z))
33+
#' geom_tile(aes(fill = z), colour = "grey50")
3434
#' ggplot(df, aes(x, y)) +
3535
#' geom_tile(aes(fill = z, width = w), colour = "grey50")
3636
#' ggplot(df, aes(xmin = x - w / 2, xmax = x + w / 2, ymin = y, ymax = y + 1)) +
@@ -81,7 +81,7 @@ geom_tile <- function(mapping = NULL, data = NULL,
8181
#' @export
8282
#' @include geom-rect.r
8383
GeomTile <- ggproto("GeomTile", GeomRect,
84-
extra_params = c("na.rm", "width", "height"),
84+
extra_params = c("na.rm"),
8585

8686
setup_data = function(data, params) {
8787
data$width <- data$width %||% params$width %||% resolution(data$x, FALSE)
@@ -94,7 +94,7 @@ GeomTile <- ggproto("GeomTile", GeomRect,
9494
},
9595

9696
default_aes = aes(fill = "grey20", colour = NA, size = 0.1, linetype = 1,
97-
alpha = NA),
97+
alpha = NA, width = NA, height = NA),
9898

9999
required_aes = c("x", "y"),
100100

man/geom_tile.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-geom-tile.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,18 @@ test_that("accepts width and height params", {
1111
expect_equal(out2$xmin, c(0.75, 1.75))
1212
expect_equal(out2$xmax, c(1.25, 2.25))
1313
})
14+
15+
test_that("accepts width and height aesthetics", {
16+
df <- data.frame(x = 0, y = 0, width = c(2, 4), height = c(2, 4))
17+
18+
p <- ggplot(df, aes(x, y, width = width, height = height)) +
19+
geom_tile(fill = NA, colour = "black", size = 1)
20+
out <- layer_data(p)
21+
22+
boundary <- as.data.frame(tibble::tribble(
23+
~xmin, ~xmax, ~ymin, ~ymax,
24+
-1, 1, -1, 1,
25+
-2, 2, -2, 2
26+
))
27+
expect_equal(out[c("xmin", "xmax", "ymin", "ymax")], boundary)
28+
})

0 commit comments

Comments
 (0)