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

Horizontal / vertical grid cells distortion #120

Closed
elgabbas opened this issue Nov 10, 2023 · 3 comments
Closed

Horizontal / vertical grid cells distortion #120

elgabbas opened this issue Nov 10, 2023 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities 🏼 spatraster Related with SpatRaster

Comments

@elgabbas
Copy link

elgabbas commented Nov 10, 2023

Hello,

I am plotting multiple projected [EPSG:3035] spatraster objects, all having the same extent. ExampleMap.zip

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")) %>%   
      ggplot2::ggsave(filename = "ExampleMap1.jpeg", width = 25, height = 20, units = "cm", dpi = 600)

ExampleMap1

# horizontally distorted  
      (ggplot2::ggplot() +
      tidyterra::geom_spatraster(data = terra::rast(ExampleMap[[2]])) +
      paletteer::scale_fill_paletteer_c(na.value = NA, "viridis::plasma")) %>% 
      ggplot2::ggsave(filename = "ExampleMap2.jpeg", width = 25, height = 20, units = "cm", dpi = 600)

image

    # This seems to be okay
     (ggplot2::ggplot() +
      tidyterra::geom_spatraster(data = terra::rast(ExampleMap[[3]])) +
      paletteer::scale_fill_paletteer_c(na.value = NA, "viridis::plasma")) %>% 
      ggplot2::ggsave(filename = "ExampleMap3.jpeg", width = 25, height = 20, units = "cm", dpi = 600)

ExampleMap3

Is it possible to ensure that the plotted maps do not show this distortion (i.e., always shown as a rectangle).

Thanks

@dieghernan
Copy link
Owner

See tidyverse/ggplot2#5523 and tidyverse/ggplot2#3025 (also https://stackoverflow.com/questions/77474839/ggplot-of-raster-maps-horizontal-vertical-grid-cells-distortion/77486091#77486091).

It seems that it more a plotting issue (rather than distorting) related with how geom_raster() handles NA when the number of non-NA cells is low. Instead, use transparent:

# Get the data

tmp <- "ExampleMap.zip"
download.file(
  "https://github.com/dieghernan/tidyterra/files/13322139/ExampleMap.zip",
  tmp
  
)
dataset <- unzip(tmp, list = TRUE)[1]
unzip(tmp)


load("ExampleMap.RData")

library(terra)
#> terra 1.7.55
library(raster) # For the reprex
#> Loading required package: sp
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:raster':
#> 
#>     select
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggplot2)
library(paletteer)

ExampleMap1 <- rast(ExampleMap[[1]])


# Reproduce

ggplot() +
  geom_spatraster(data = ExampleMap1, maxcell = Inf) +
  paletteer::scale_fill_paletteer_c(na.value = NA, "viridis::plasma")

# Use transparent
ggplot() +
  geom_spatraster(data = ExampleMap1, maxcell = Inf) +
  paletteer::scale_fill_paletteer_c(na.value = "transparent", "viridis::plasma")

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

I would let this issue open until I documented this situation and also include it on the FAQs

@dieghernan dieghernan added documentation Improvements or additions to documentation 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities 🏼 spatraster Related with SpatRaster labels Nov 15, 2023
@dieghernan dieghernan self-assigned this Nov 15, 2023
@elgabbas
Copy link
Author

Thanks Diego.. This solves the issue with the provided example. I will implement it on other maps. Cheers.

@dieghernan
Copy link
Owner

Closed after #122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities 🏼 spatraster Related with SpatRaster
Projects
None yet
Development

No branches or pull requests

2 participants