Skip to content

Issues related to crs and default_crs processing after ggplot2 4.0 Release #179

@Breeze-Hu

Description

@Breeze-Hu

Hi!

I'm in the process of using tidyterra for raster file plotting, and I've noticed a problem in the development version of ggplot2 that I didn't have before (the code that went there first is not working). The details are as follows:

example data

library(terra)
#> terra 1.8.54
library(ggplot2)
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter

data <- rast('./temdata.nc') 

defcrs <- paste(
  "+proj=lcc", 
  "+lat_1=25", 
  "+lat_2=47", 
  "+lon_0=105", 
  "+datum=WGS84", 
  "+units=m",
  sep = " "
)

ggplot()+
  geom_spatraster(data = data) +
  coord_sf(crs = defcrs) # This works.

# When I needed to adjust the range so that I used the following method. This reports an error:
ggplot()+
  geom_spatraster(data = data) +
  coord_sf(
    crs = defcrs,
    default_crs = sf::st_crs(4326),
    xlim = c(90, 122),
    ylim = c(24, 37),
    label_axes = list(bottom = "E", left = "N", right = "N")
  ) 
#> `geom_raster()` only works with linear coordinate systems, not `coord_sf()`.
#> ℹ Falling back to drawing as `geom_rect()`.
#> Error in `geom_spatraster()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 1st layer.
#> Caused by error:
#> ! vector memory limit of 18.0 Gb reached, see mem.maxVSize()

Created on 2025-06-26 with reprex v2.1.1

ggplot2 tidyverse/ggplot2#6382 made some changes to the geom_raster function that I suspect are related.

> sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tidyterra_0.7.2    ggplot2_3.5.2.9000 terra_1.8-54      

loaded via a namespace (and not attached):
 [1] tidyr_1.3.1        RColorBrewer_1.1-3 R6_2.6.1           codetools_0.2-20  
 [5] tidyselect_1.2.1   farver_2.1.2       magrittr_2.0.3     gtable_0.3.6      
 [9] glue_1.8.0         tibble_3.3.0       dichromat_2.0-0.1  pkgconfig_2.0.3   
[13] generics_0.1.4     dplyr_1.1.4        lifecycle_1.0.4    cli_3.6.5         
[17] scales_1.4.0       vctrs_0.6.5        grid_4.5.1         withr_3.0.2       
[21] compiler_4.5.1     purrr_1.0.4        rstudioapi_0.17.1  tools_4.5.1       
[25] pillar_1.10.2      Rcpp_1.0.14        rlang_1.1.6       

After checking, this version of ggplot at tidyverse/ggplot2@3914e13 is able to draw images properly, although there are warning messages (lcc: Invalid latitude; lcc: Invalid latitude...)

Best wishes,
Hu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions