diff --git a/NEWS.md b/NEWS.md index 8a31cd598a..b08c7c13a2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `geom_raster()` now falls back to rendering as `geom_rect()` when coordinates + are not Cartesian (#5503). * `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 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 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)