Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geom_raster() fallback for non-Cartesian coords #5627

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
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
Loading