Skip to content

Commit

Permalink
geom_raster() fallback for non-Cartesian coords (#5627)
Browse files Browse the repository at this point in the history
* fallback mechanism

* add tests

* add news bullet

* put bullet in appropriate place
  • Loading branch information
teunbrand committed May 20, 2024
1 parent 1df93c4 commit 1654df1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 7 additions & 2 deletions R/geom-raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions tests/testthat/_snaps/geom-raster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
78 changes: 78 additions & 0 deletions tests/testthat/_snaps/geom-raster/rectangle-fallback.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion tests/testthat/test-geom-raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1654df1

Please sign in to comment.