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

Different behaviour of scales param na.value = NA in geom_tile() and geom_raster() #5523

Closed
dieghernan opened this issue Nov 15, 2023 · 2 comments

Comments

@dieghernan
Copy link

HI:

Following an issue on tidyterra (dieghernan/tidyterra#120, see also https://stackoverflow.com/questions/77474839/ggplot-of-raster-maps-horizontal-vertical-grid-cells-distortion/77486091#77486091) I think the root cause may be on how ggplot2 handles the param na.value = NA on geom_raster() (note that geom_spatraster() is leveraging on this ggplot2 function). See the different behaviour compared with geom_tile():

ex <- tibble::tribble(
  ~x, ~y, ~ExampleMap2,
  4415000L, 2865000L, 1L,
  4425000L, 2865000L, NA,
  4435000L, 2865000L, NA,
  4445000L, 2865000L, NA,
  4415000L, 2855000L, NA,
  4425000L, 2855000L, NA,
  4435000L, 2855000L, NA,
  4445000L, 2855000L, NA,
  4415000L, 2845000L, 1L,
  4425000L, 2845000L, NA,
  4435000L, 2845000L, NA,
  4445000L, 2845000L, NA,
  4415000L, 2835000L, NA,
  4425000L, 2835000L, NA,
  4435000L, 2835000L, NA,
  4445000L, 2835000L, NA,
  4415000L, 2825000L, 1L,
  4425000L, 2825000L, NA,
  4435000L, 2825000L, NA,
  4445000L, 2825000L, 1L
)

library(ggplot2)

p_raster <- ggplot(ex, aes(x, y)) +
  geom_raster(aes(fill = ExampleMap2)) +
  coord_equal()

# As expected
p_raster

# With na.value = NA extra cells are filled
p_raster +
  scale_fill_viridis_c(na.value = NA)
#> Warning: Removed 16 rows containing missing values (`geom_raster()`).

# Not happening with geom_tile
ggplot(ex, aes(x, y)) +
  geom_tile(aes(fill = ExampleMap2)) +
  coord_equal() +
  scale_fill_viridis_c(na.value = NA)

Created on 2023-11-15 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

Is this the same as #3025?
In any case, I think this particular example may also be solved by setting na.value = "transparent".
The fill is a non-missing aesthetic in geom_raster(), meaning that rows that have NA fills will be removed. Subsequently, there are too few cells to reconstruct the resolution of the data.

@dieghernan
Copy link
Author

Hi,

Yes, it seems to be the same case, thanks for pointing out. transparent would do the trick:

ex <- tibble::tribble(
  ~x, ~y, ~ExampleMap2,
  4415000L, 2865000L, 1L,
  4425000L, 2865000L, NA,
  4435000L, 2865000L, NA,
  4445000L, 2865000L, NA,
  4415000L, 2855000L, NA,
  4425000L, 2855000L, NA,
  4435000L, 2855000L, NA,
  4445000L, 2855000L, NA,
  4415000L, 2845000L, 1L,
  4425000L, 2845000L, NA,
  4435000L, 2845000L, NA,
  4445000L, 2845000L, NA,
  4415000L, 2835000L, NA,
  4425000L, 2835000L, NA,
  4435000L, 2835000L, NA,
  4445000L, 2835000L, NA,
  4415000L, 2825000L, 1L,
  4425000L, 2825000L, NA,
  4435000L, 2825000L, NA,
  4445000L, 2825000L, 1L
)

library(ggplot2)

p_raster <- ggplot(ex, aes(x, y)) +
  geom_raster(aes(fill = ExampleMap2)) +
  coord_equal()

# As expected
p_raster

# Filling extra cells
p_raster +
  scale_fill_viridis_c(na.value = NA)
#> Warning: Removed 16 rows containing missing values (`geom_raster()`).

# Not happening with transparent
p_raster +
  scale_fill_viridis_c(na.value = "transparent")

Created on 2023-11-15 with reprex v2.0.2

@dieghernan dieghernan changed the title Different behaviour of scales param na.value = NA in geom_tile() and `geom_raster() Different behaviour of scales param na.value = NA in geom_tile() and geom_raster() Nov 15, 2023
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

No branches or pull requests

2 participants