From 7be9f1eaee98cc7ee14a0fd79683cce603276db4 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 4 Jan 2024 10:49:40 +0100 Subject: [PATCH 1/4] fallback mechanism --- R/geom-raster.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/geom-raster.R b/R/geom-raster.R index 2cd591d879..2d4ecd85d2 100644 --- a/R/geom-raster.R +++ b/R/geom-raster.R @@ -88,9 +88,14 @@ GeomRaster <- ggproto("GeomRaster", Geom, draw_panel = function(self, data, panel_params, coord, interpolate = FALSE, hjust = 0.5, vjust = 0.5) { if (!inherits(coord, "CoordCartesian")) { - cli::cli_abort(c( - "{.fn {snake_class(self)}} only works with {.fn coord_cartesian}." + cli::cli_inform(c( + "{.fn {snake_class(self)}} only works with {.fn coord_cartesian}.", + i = "Falling back to drawing as {.fn {snake_class(GeomRect)}}." )) + data$linewidth <- 0.3 # preventing anti-aliasing artefacts + data$colour <- data$fill + grob <- GeomRect$draw_panel(data, panel_params, coord) + return(grob) } # Convert vector of data to raster From 14ed0d8af883f2621e2ba8aff2c86f67805d788d Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 4 Jan 2024 10:49:53 +0100 Subject: [PATCH 2/4] add tests --- tests/testthat/_snaps/geom-raster.md | 7 -- .../_snaps/geom-raster/rectangle-fallback.svg | 78 +++++++++++++++++++ tests/testthat/test-geom-raster.R | 10 ++- 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/_snaps/geom-raster/rectangle-fallback.svg diff --git a/tests/testthat/_snaps/geom-raster.md b/tests/testthat/_snaps/geom-raster.md index 16da7d9d54..4deac92872 100644 --- a/tests/testthat/_snaps/geom-raster.md +++ b/tests/testthat/_snaps/geom-raster.md @@ -14,13 +14,6 @@ `vjust` must be a number, not the string "a". ---- - - Problem while converting geom to grob. - i Error occurred in the 1st layer. - Caused by error in `draw_panel()`: - ! `geom_raster()` only works with `coord_cartesian()`. - # geom_raster() fails with pattern fills Problem while converting geom to grob. diff --git a/tests/testthat/_snaps/geom-raster/rectangle-fallback.svg b/tests/testthat/_snaps/geom-raster/rectangle-fallback.svg new file mode 100644 index 0000000000..efb96b5c87 --- /dev/null +++ b/tests/testthat/_snaps/geom-raster/rectangle-fallback.svg @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1.0 +1.5 +2.0 +0.5/2.5 + + + +0.5 +1.0 +1.5 +2.0 +2.5 + + + + + +x +y + +fill + + + + + + + + +A +B +C +D +rectangle fallback + + diff --git a/tests/testthat/test-geom-raster.R b/tests/testthat/test-geom-raster.R index 2dfa1106e3..2a3ed66a31 100644 --- a/tests/testthat/test-geom-raster.R +++ b/tests/testthat/test-geom-raster.R @@ -6,7 +6,7 @@ test_that("geom_raster() checks input and coordinate system", { df <- data_frame(x = rep(c(-1, 1), each = 3), y = rep(-1:1, 2), z = 1:6) p <- ggplot(df, aes(x, y, fill = z)) + geom_raster() + coord_polar() - expect_snapshot_error(ggplotGrob(p)) + expect_message(ggplotGrob(p), "only works with") }) test_that("geom_raster() fails with pattern fills", { @@ -66,6 +66,14 @@ test_that("geom_raster draws correctly", { geom_point(colour = "red") ) + # In non-linear coordinates + df <- data.frame(x = c(1, 2, 1, 2), y = c(1, 1, 2, 2), fill = LETTERS[1:4]) + suppressMessages( + expect_doppelganger("rectangle fallback", + ggplot(df, aes(x, y, fill = fill)) + geom_raster() + coord_polar() + ) + ) + # Categorical fill, irregular swatches --------------------------------------- df <- expand.grid(x = 1:10, y = 1:10) From 40189fd4ad780c4d4bb7f61eb1c90db8f0573312 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 4 Jan 2024 10:50:07 +0100 Subject: [PATCH 3/4] add news bullet --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 60c7877119..d3e6520b33 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ + +* `geom_raster()` now falls back to rendering as `geom_rect()` when coordinates + are not Cartesian (#5503). + # ggplot2 (development version) This is a minor release that turned out quite beefy. It is focused on From b64a1e8b2ad139c2c061211e241b9f9be5b73146 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 20 May 2024 11:12:05 +0200 Subject: [PATCH 4/4] put bullet in appropriate place --- NEWS.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index d06c8c41a7..b08c7c13a2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,7 @@ +# ggplot2 (development version) * `geom_raster()` now falls back to rendering as `geom_rect()` when coordinates are not Cartesian (#5503). - -# ggplot2 (development version) - * `stat_ecdf()` now has an optional `weight` aesthetic (@teunbrand, #5058). * Position scales combined with `coord_sf()` can now use functions in the `breaks` argument. In addition, `n.breaks` works as intended and