Skip to content

Do not use class information for raster resolution calculation #5638

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 1 commit into from
Jan 12, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR to the RC aims to fix a bug uncovered during reverse dependency checks.

When feeding continuous data to geom_raster() but using discrete scales confers the mapped_discrete class even upon the continuous data. We use the heuristic that mapped_discrete values have resolution 1 in #5247. This leads to problems in geom_raster():

library(ggplot2)

t <- c(0.1, 0.9, length.out = 2)
df <- expand.grid(c(0.1, 0.9), c(0.1, 0.9))
df$fill <- LETTERS[1:4]

# Should show 4 squares
ggplot(df, aes(Var1, Var2, fill = fill)) +
  geom_raster() +
  scale_x_discrete() +
  scale_y_discrete()

In this PR, the data of geom_raster() is unclass()'ed so that resolution correctly pick up on its continuous nature:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(df, aes(Var1, Var2, fill = fill)) +
  geom_raster() +
  scale_x_discrete() +
  scale_y_discrete()

I found out about this using case via the breakage of stars, where the following example would consume gigabytes of memory due to building an enormous raster. With this PR, this is now more in sync with the size of the original data.

library(stars)

x <- system.file("tif/L7_ETMs.tif", package = "stars") |>
  read_stars()

ggplot() + 
  geom_stars(data = x) +
  coord_equal() +
  facet_wrap(~band) +
  theme_void() +
  scale_fill_viridis_c() +
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0))

Created on 2024-01-12 with reprex v2.0.2

@teunbrand teunbrand added this to the ggplot2 3.5.0 milestone Jan 12, 2024
@teunbrand teunbrand requested a review from thomasp85 January 12, 2024 08:50
@teunbrand teunbrand changed the title Do not use class information for calculation raster resolution Do not use class information for raster resolution calculation Jan 12, 2024
Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants