I am plotting multiple projected raster objects (SpatRaster or raster stack; equal area projection; EPSG:3035), all having the same extent. Example map
However, the mapped grid cells are distorted, either horizontally or vertically (i.e. not square), or shown larger than they should be.
# vertically distorted
ggplot2::ggplot() +
tidyterra::geom_spatraster(data = terra::rast(ExampleMap[[1]])) +
paletteer::scale_fill_paletteer_c(na.value = NA, "viridis::plasma")
# horizontally distorted
ggplot2::ggplot() +
tidyterra::geom_spatraster(data = terra::rast(ExampleMap[[2]])) +
paletteer::scale_fill_paletteer_c(na.value = NA, "viridis::plasma")
Here is a full script for plotting one layer using different geoms:
require(raster)
require(stara)
require(ggplot2)
ExampleMap2 <- ExampleMap[[1]] %>% trim()
Using geom_stars
ggplot2::ggplot() +
stars::geom_stars(data = stars::st_as_stars(ExampleMap2)) +
ggplot2::coord_equal() +
ggplot2::theme_void() +
ggplot2::scale_fill_continuous(na.value = NA) +
guides(fill = guide_legend(NULL)) +
theme(aspect.ratio = 1) +
labs(title = "stars::geom_stars")
Using tidyterra::geom_spatraster
ggplot2::ggplot() +
tidyterra::geom_spatraster(data = terra::rast(ExampleMap2)) +
ggplot2::theme_void() +
ggplot2::scale_fill_continuous(na.value = NA) +
guides(fill = guide_legend(NULL)) +
theme(aspect.ratio = 1) +
labs(title = "tidyterra::geom_spatraster")
Using ggplot2::geom_raster
DT <- as.data.frame(ExampleMap2, xy = TRUE)
ggplot2::ggplot() +
ggplot2::geom_raster(aes(x = x, y = y, fill = as.numeric(as.character(ExampleMap1))), data = DT) +
ggplot2::theme_void() +
ggplot2::scale_fill_continuous(na.value = NA) +
guides(fill = guide_legend(NULL)) +
coord_equal() +
theme(aspect.ratio = 1) +
labs(title = "ggplot2::geom_raster")
Using ggplot2::geom_tile [this seems to work as expected]
ggplot2::ggplot() +
ggplot2::geom_tile(aes(x = x, y = y, fill = as.numeric(as.character(ExampleMap1))), data = DT) +
ggplot2::theme_void() +
ggplot2::scale_fill_continuous(na.value = NA) +
guides(fill = guide_legend(NULL)) +
coord_equal() +
labs(title = "ggplot2::geom_tile")
What is the reason for this distortion and how to ensure that this does not happen using e.g. tidyterra::geom_spatraster or ggplot2::geom_raster?
Thanks






I modified my original answer, noted now under Old Answer
It seems to be related with how ggplot handles
NAongeom_raster()when there are few values left (see https://github.com/tidyverse/ggplot2/issues/5523 and https://github.com/tidyverse/ggplot2/issues/3025. Instead usena.value = "transparent"Created on 2023-11-15 with reprex v2.0.2
Old answer
I added several tests to
tidyterraand didn't find an issue with this. I think here the problem is not thattidyterrais distorting the cells, but how theggplot2palette is filling the plot, and related with the value ofna.valueof the fill scale.Maybe this can be an issue on
ggplot2?See how the
ggplot2is affected by this parameter, and a workaround using thegeom_tile()approach:The problem is the na.value param
Workaround