dieghernan / tidyterra

tidyverse and ggplot2 methods for terra spatial objects
https://dieghernan.github.io/tidyterra/
Other
181 stars 8 forks source link

ggplot support for SpatGraticule #155

Open Maschette opened 6 days ago

Maschette commented 6 days ago

Hi, This may be covered by another function but I couldn't see it in your list of data types.

Is there a way to plot SpatGraticule with ggplot2?

thanks, Dale

dieghernan commented 6 days ago

Currently I don't identify many methods for SpatGraticule objects, on terra so my workaround here would be converting it to SpatVector and then using geom_spatvector(), based on https://github.com/rspatial/terra/blob/43681fd1af25775ee1565a4fd1bb5b4830b1c985/R/graticule.R#L211-L212

library(terra)
#> terra 1.7.83
library(tidyterra)
#> 
#> Adjuntando el paquete: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggplot2)

terra::lin
#> Error: 'lin' is not an exported object from 'namespace:terra'

lux <- terra::vect(system.file("ex/lux.shp", package = "terra"))
grat <- graticule(
  lon = seq(5.5, 6.75, 0.25),
  lat = seq(49.25, 50.25, 0.25),
  crs = crs(lux)
)

# Convert to SpatVector (lines)
v <- vect()
v@ptr <- grat@ptr

v
#>  class       : SpatVector 
#>  geometry    : lines 
#>  dimensions  : 11, 3  (geometries, attributes)
#>  extent      : 5.5, 6.75, 49.25, 50.25  (xmin, xmax, ymin, ymax)
#>  coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#>  names       :         h   lon   lat
#>  type        : <logical> <num> <num>
#>  values      :     FALSE   5.5   NaN
#>                    FALSE  5.75   NaN
#>                    FALSE     6   NaN

ggplot(lux) +
  geom_spatvector(fill = "orange") +
  geom_spatvector(data = v, color = "blue", linetype = "dotted")


ggplot(lux) +
  geom_spatvector(fill = "orange") +
  geom_spatvector(data = v, color = "blue", linetype = "dotted") +
  coord_sf(crs = 3035) +
  ggtitle("With custom projection")

Created on 2024-11-28 with reprex v2.1.1

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.2 (2024-10-31 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Spanish_Spain.utf8 #> ctype Spanish_Spain.utf8 #> tz Europe/Madrid #> date 2024-11-28 #> pandoc 3.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> class 7.3-22 2023-05-03 [2] CRAN (R 4.4.2) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.4.0) #> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.1) #> codetools 0.2-20 2024-03-31 [2] CRAN (R 4.4.2) #> colorspace 2.1-1 2024-07-26 [1] CRAN (R 4.4.1) #> curl 6.0.1 2024-11-14 [1] CRAN (R 4.4.2) #> DBI 1.2.3 2024-06-02 [1] CRAN (R 4.4.0) #> digest 0.6.37 2024-08-19 [1] CRAN (R 4.4.1) #> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.4.0) #> e1071 1.7-16 2024-09-16 [1] CRAN (R 4.4.2) #> evaluate 1.0.1 2024-10-10 [1] CRAN (R 4.4.2) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.4.0) #> farver 2.1.2 2024-05-13 [1] CRAN (R 4.4.0) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.0) #> fs 1.6.5 2024-10-30 [1] CRAN (R 4.4.2) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.4.0) #> ggplot2 * 3.5.1 2024-04-23 [1] CRAN (R 4.4.0) #> glue 1.8.0 2024-09-30 [1] CRAN (R 4.4.2) #> gtable 0.3.6 2024-10-25 [1] CRAN (R 4.4.2) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0) #> KernSmooth 2.23-24 2024-05-17 [1] CRAN (R 4.4.0) #> knitr 1.49 2024-11-08 [1] CRAN (R 4.4.2) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0) #> munsell 0.5.1 2024-04-01 [1] CRAN (R 4.4.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.4.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.4.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.4.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.4.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0) #> Rcpp 1.0.13-1 2024-11-02 [1] CRAN (R 4.4.2) #> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.4.1) #> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.0) #> rmarkdown 2.29 2024-11-04 [1] CRAN (R 4.4.2) #> rstudioapi 0.17.1 2024-10-22 [1] CRAN (R 4.4.2) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0) #> sf 1.0-19 2024-11-05 [1] CRAN (R 4.4.2) #> terra * 1.7-83 2024-10-14 [1] CRAN (R 4.4.2) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.4.0) #> tidyr 1.3.1 2024-01-24 [1] CRAN (R 4.4.0) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.4.0) #> tidyterra * 0.6.1 2024-06-08 [1] CRAN (R 4.4.0) #> units 0.8-5 2023-11-28 [1] CRAN (R 4.4.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.4.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0) #> withr 3.0.2 2024-10-28 [1] CRAN (R 4.4.2) #> xfun 0.49 2024-10-31 [1] CRAN (R 4.4.2) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.4.0) #> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.4.1) #> #> [1] C:/Users/diego/AppData/Local/R/win-library/4.4 #> [2] C:/Program Files/R/R-4.4.2/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Maybe add vect.SpatGraticule method to terra @rhijmans ?

rhijmans commented 6 days ago

I have added a vect<SpatGraticule> method to terra 1.8-3. Thank you for suggesting it.