Skip to content

Commit 1654df1

Browse files
authored
geom_raster() fallback for non-Cartesian coords (#5627)
* fallback mechanism * add tests * add news bullet * put bullet in appropriate place
1 parent 1df93c4 commit 1654df1

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* `geom_raster()` now falls back to rendering as `geom_rect()` when coordinates
4+
are not Cartesian (#5503).
35
* `stat_ecdf()` now has an optional `weight` aesthetic (@teunbrand, #5058).
46
* Position scales combined with `coord_sf()` can now use functions in the
57
`breaks` argument. In addition, `n.breaks` works as intended and

R/geom-raster.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ GeomRaster <- ggproto("GeomRaster", Geom,
8888
draw_panel = function(self, data, panel_params, coord, interpolate = FALSE,
8989
hjust = 0.5, vjust = 0.5) {
9090
if (!inherits(coord, "CoordCartesian")) {
91-
cli::cli_abort(c(
92-
"{.fn {snake_class(self)}} only works with {.fn coord_cartesian}."
91+
cli::cli_inform(c(
92+
"{.fn {snake_class(self)}} only works with {.fn coord_cartesian}.",
93+
i = "Falling back to drawing as {.fn {snake_class(GeomRect)}}."
9394
))
95+
data$linewidth <- 0.3 # preventing anti-aliasing artefacts
96+
data$colour <- data$fill
97+
grob <- GeomRect$draw_panel(data, panel_params, coord)
98+
return(grob)
9499
}
95100

96101
# Convert vector of data to raster

tests/testthat/_snaps/geom-raster.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414

1515
`vjust` must be a number, not the string "a".
1616

17-
---
18-
19-
Problem while converting geom to grob.
20-
i Error occurred in the 1st layer.
21-
Caused by error in `draw_panel()`:
22-
! `geom_raster()` only works with `coord_cartesian()`.
23-
2417
# geom_raster() fails with pattern fills
2518

2619
Problem while converting geom to grob.
Lines changed: 78 additions & 0 deletions
Loading

tests/testthat/test-geom-raster.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test_that("geom_raster() checks input and coordinate system", {
66

77
df <- data_frame(x = rep(c(-1, 1), each = 3), y = rep(-1:1, 2), z = 1:6)
88
p <- ggplot(df, aes(x, y, fill = z)) + geom_raster() + coord_polar()
9-
expect_snapshot_error(ggplotGrob(p))
9+
expect_message(ggplotGrob(p), "only works with")
1010
})
1111

1212
test_that("geom_raster() fails with pattern fills", {
@@ -66,6 +66,14 @@ test_that("geom_raster draws correctly", {
6666
geom_point(colour = "red")
6767
)
6868

69+
# In non-linear coordinates
70+
df <- data.frame(x = c(1, 2, 1, 2), y = c(1, 1, 2, 2), fill = LETTERS[1:4])
71+
suppressMessages(
72+
expect_doppelganger("rectangle fallback",
73+
ggplot(df, aes(x, y, fill = fill)) + geom_raster() + coord_polar()
74+
)
75+
)
76+
6977
# Categorical fill, irregular swatches ---------------------------------------
7078

7179
df <- expand.grid(x = 1:10, y = 1:10)

0 commit comments

Comments
 (0)