Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ggplot support for SpatGraticule #155

Closed
Maschette opened this issue Nov 28, 2024 · 2 comments · Fixed by #158
Closed

ggplot support for SpatGraticule #155

Maschette opened this issue Nov 28, 2024 · 2 comments · Fixed by #158
Labels
feature Ideas for new features 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities

Comments

@Maschette
Copy link

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
Copy link
Owner

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
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
Copy link

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

@dieghernan dieghernan added 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities feature Ideas for new features labels Dec 6, 2024
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 5, 2025
# version 1.8-5

## bug fixes

- `Spatsample(method='stratified', ext=e)` returned the wrong sampling
  coordinates [#1628](rspatial/terra#1628)
  by Barnabas Harris

- `spatSample(method='stratified')` could fail with small sample sizes
  [#1503](rspatial/terra#1503) by karluf

- transparency (alpha) did not work with RGB
  plotting. [#1642](rspatial/terra#1642) by
  Timothée Giraud

- rasterization failed on very large rasters
  [#1636](rspatial/terra#1636) by Mary
  Fisher, [#1463](rspatial/terra#1463) by
  Nic Spono and [#1281](rspatial/terra#1281)
  by Sebastian Dunnett

- `tmpFiles` only looked in the default temp files folder
  [#1630](rspatial/terra#1630) by
  smckenzie1986

- `where.min` did not work well if there were negative values
  [#1634](rspatial/terra#1634) by Michael
  Sumner

- `plet<SpatRaster>` now works for RGB rasters and rasters with a
  color table [#1596](rspatial/terra#1596)
  by Agustin Lobo

- `vect<MULTIPOINT WKT>` did not work properly
  [#1376](rspatial/terra#1376) by
  silasprincipe

- `compareGeom<SpatVector>` did not work
  [#1654](rspatial/terra#1654) by Jason
  Flower

- `buffer<SpatVector>` is now more accurate buffers for lonlat
  polygons [#1616](rspatial/terra#1616) by
  Roberto Amaral-Santos

- `terra:interpNear` used square windows, not circles, beyond 100
  points [#1509](rspatial/terra#1509) by
  Jean-Luc Dupouey

- `vect` read INT64 fields as integers, sometimes leading to
  overflows. [#1666](rspatial/terra#1666) by
  bengannon-fc

- `plot` showed a legend title even if none was requestd if title
  parameters were specified
  . [#1664](rspatial/terra#1664) by Márcia
  Barbosa



## enhancements

n- improved documentation of `writeVector` overwrite when using
  layers. [#1573](rspatial/terra#1573) by
  Todd West

- improved treatment of (supposedly) flipped rasters by Timothée
  Giraud [#1627](rspatial/terra#1627) and
  fchianucci [#1646](rspatial/terra#1646)

- added `map.pal("random")`
  [#1631](rspatial/terra#1631) by Agustin
  Lobo

- expressions can now be used in legend titles
  [#1626](rspatial/terra#1626) by Noah
  Goodkind

- `app` and `tapp` now emit a warning when factors are coerced to
  numeric [#1566](rspatial/terra#1566) by
  shuysman

- `plet<SpatRaster>` now has argument "stretch" for RGB rasters
  [#1596](rspatial/terra#1596) by Agustin

- `%%` and `%/%` now behave the same for SpatRaster as for (base R)
  numbers [#1661](rspatial/terra#1661) by
  Klaus Huebert

## new

- `patches` with option `valus=TRUE` can now distinguish regions based
  on their cell values (instead of only NA vs not-NA)
  [#495](rspatial/terra#495) by Jakub
  Nowosad and [#1632](rspatial/terra#1632)
  by Agustin Lobo

- `rowSums`, `rowMeans`, `colSums` and `colMeans` for SpatRaster

- `metags` for SpatRasterDataset
  [#1624](rspatial/terra#1624) by Andrea
  Manica

- `metags` for layers (bands) of SpatRaster are now saved to and read
  from GTiff files
  [#1071](rspatial/terra#1071) by Mike
  Koontz

- `global` has new effcient functions "anyNA" and "anynotNA"
  [#1540](rspatial/terra#1540) by Kevin J
  Wolz

- `wrap`, `saveRDS` and `serialize` for
  SpatExtent. [#1430](rspatial/terra#1430)
  by BastienFR

- `vect<SpatGraticule>` method suggested in relation to [tidyterra
  #155](dieghernan/tidyterra#155) by Diego
  Hernangómez

- `toMemory<SpatRaster>` and `<SpatRasterDataset>` methods
  [#1660](rspatial/terra#1660) by Derek Friend
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature Ideas for new features 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants