Skip to content

Fix for raster resolution #5412

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

Merged
merged 6 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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,7 +1,11 @@
# ggplot2 (development version)

* `resolution()` has a small tolerance, preventing spuriously small resolutions
due to rounding errors (@teunbrand, #2516).

* `stage()` now works correctly, even with aesthetics that do not have scales
(#5408)

* `labeller()` now handles unspecified entries from lookup tables
(@92amartins, #4599).

Expand Down
3 changes: 2 additions & 1 deletion R/geom-raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ GeomRaster <- ggproto("GeomRaster", Geom,
"{.fn {snake_class(self)}} only works with {.fn coord_cartesian}"
))
}
data <- coord$transform(data, panel_params)

# Convert vector of data to raster
x_pos <- as.integer((data$x - min(data$x)) / resolution(data$x, FALSE))
y_pos <- as.integer((data$y - min(data$y)) / resolution(data$y, FALSE))

data <- coord$transform(data, panel_params)

nrow <- max(y_pos) + 1
ncol <- max(x_pos) + 1

Expand Down
5 changes: 3 additions & 2 deletions R/utilities-resolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ resolution <- function(x, zero = TRUE) {
if (zero) {
x <- unique0(c(0, x))
}

min(diff(sort(x)))
d <- diff(sort(x))
tolerance <- sqrt(.Machine$double.eps)
min(d[d > tolerance])
}
60 changes: 60 additions & 0 deletions tests/testthat/_snaps/geom-raster/discrete-positions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/testthat/test-geom-raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,11 @@ test_that("geom_raster draws correctly", {
expect_doppelganger("irregular categorical",
ggplot(df, aes(x, y, fill = factor(col))) + geom_raster()
)

# Categorical axes -----------------------------------------------------------

df <- expand.grid(x = c("A", "B"), y = c("C", "D"))
expect_doppelganger("discrete positions",
ggplot(df, aes(x, y, fill = interaction(x, y))) + geom_raster()
)
})
3 changes: 3 additions & 0 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,7 @@ test_that("resolution() gives correct answers", {
expect_equal(resolution(mapped_discrete(c(4, 6))), 1L)
expect_equal(resolution(c(0, 0)), 1L)
expect_equal(resolution(c(0.5, 1.5), zero = TRUE), 0.5)

# resolution has a tolerance
expect_equal(resolution(c(1, 1 + 1000 * .Machine$double.eps, 2)), 1)
})