oscarperpinan / rastervis

The raster package defines classes and methods for spatial raster data access and manipulation. The rasterVis package complements raster providing a set of methods for enhanced visualization and interaction.
oscarperpinan.github.io/rastervis
GNU General Public License v3.0
85 stars 26 forks source link

`gplot` does not work with terra SpatRaster objects #78

Closed Pakillo closed 2 years ago

Pakillo commented 2 years ago

Hi @oscarperpinan , thanks for the package, and for updating it to work with terra.

I seem unable to use gplot with terra rasters, though... See example code from gplot below.

The problem may be in terra::sampleRegular, which does not seem to exist anymore, and probably should be replaced by terra::spatSample (https://rspatial.github.io/terra/reference/sample.html)

Thanks

library(raster)
#> Loading required package: sp
library(terra)
#> terra version 1.4.22
library(rasterVis)
#> Loading required package: lattice

r <- raster(system.file("external/test.grd", package="raster"))

s1 <- stack(r, r*2)
names(s1) <- c('meuse', 'meuse x 2')

library(ggplot2)

theme_set(theme_bw())

## With raster
gplot(s1) + geom_tile(aes(fill = value)) +
  facet_wrap(~ variable) +
  scale_fill_gradient(low = 'white', high = 'blue') +
  coord_equal()

## With terra
s2 <- rast(s1)
gplot(s2) + geom_tile(aes(fill = value)) +
  facet_wrap(~ variable) +
  scale_fill_gradient(low = 'white', high = 'blue') +
  coord_equal()
#> Error: 'sampleRegular' is not an exported object from 'namespace:terra'

Created on 2021-11-28 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ────────────────────────────────────────────────────────────── #> hash: woman firefighter: light skin tone, right-facing fist: dark skin tone, fork and knife #> #> setting value #> version R version 4.1.1 (2021-08-10) #> os Ubuntu 20.04.3 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_GB:en #> collate en_GB.UTF-8 #> ctype en_GB.UTF-8 #> tz Europe/Madrid #> date 2021-11-28 #> pandoc 2.11.4 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> assertthat 0.2.1 2019-03-21 [1] RSPM (R 4.1.0) #> cli 3.1.0 2021-10-27 [1] RSPM (R 4.1.0) #> codetools 0.2-18 2020-11-04 [2] CRAN (R 4.1.1) #> colorspace 2.0-2 2021-06-24 [1] RSPM (R 4.1.0) #> crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.1) #> curl 4.3.2 2021-06-23 [1] RSPM (R 4.1.0) #> DBI 1.1.1 2021-01-15 [1] RSPM (R 4.1.0) #> digest 0.6.28 2021-09-23 [1] RSPM (R 4.1.0) #> dplyr 1.0.7 2021-06-18 [1] RSPM (R 4.1.0) #> ellipsis 0.3.2 2021-04-29 [1] RSPM (R 4.1.0) #> evaluate 0.14 2019-05-28 [1] RSPM (R 4.1.0) #> fansi 0.5.0 2021-05-25 [1] RSPM (R 4.1.0) #> farver 2.1.0 2021-02-28 [1] RSPM (R 4.1.0) #> fastmap 1.1.0 2021-01-25 [1] RSPM (R 4.1.0) #> fs 1.5.0 2020-07-31 [1] RSPM (R 4.1.0) #> generics 0.1.1 2021-10-25 [1] RSPM (R 4.1.0) #> ggplot2 * 3.3.5 2021-06-25 [1] RSPM (R 4.1.0) #> glue 1.5.0 2021-11-07 [1] RSPM (R 4.1.0) #> gtable 0.3.0 2019-03-25 [1] RSPM (R 4.1.0) #> hexbin 1.28.2 2021-01-08 [1] RSPM (R 4.1.0) #> highr 0.9 2021-04-16 [1] RSPM (R 4.1.0) #> htmltools 0.5.2 2021-08-25 [1] RSPM (R 4.1.0) #> httr 1.4.2 2020-07-20 [1] RSPM (R 4.1.0) #> jpeg 0.1-9 2021-07-24 [1] RSPM (R 4.1.0) #> knitr 1.36 2021-09-29 [1] RSPM (R 4.1.0) #> labeling 0.4.2 2020-10-20 [1] RSPM (R 4.1.0) #> lattice * 0.20-44 2021-05-02 [2] CRAN (R 4.1.1) #> latticeExtra 0.6-29 2019-12-19 [1] RSPM (R 4.1.0) #> lifecycle 1.0.1 2021-09-24 [1] RSPM (R 4.1.0) #> magrittr 2.0.1 2020-11-17 [1] RSPM (R 4.1.0) #> mime 0.12 2021-09-28 [1] RSPM (R 4.1.0) #> munsell 0.5.0 2018-06-12 [1] RSPM (R 4.1.0) #> pillar 1.6.4 2021-10-18 [1] RSPM (R 4.1.0) #> pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.1.0) #> png 0.1-7 2013-12-03 [1] RSPM (R 4.1.0) #> purrr 0.3.4 2020-04-17 [1] RSPM (R 4.1.0) #> R6 2.5.1 2021-08-19 [1] RSPM (R 4.1.0) #> raster * 3.5-2 2021-10-11 [1] RSPM (R 4.1.0) #> rasterVis * 0.51.1 2021-11-20 [1] CRAN (R 4.1.1) #> RColorBrewer 1.1-2 2014-12-07 [1] RSPM (R 4.1.0) #> Rcpp 1.0.7 2021-07-07 [1] RSPM (R 4.1.0) #> reprex 2.0.1 2021-08-05 [1] RSPM (R 4.1.0) #> rgdal 1.5-27 2021-09-16 [1] RSPM (R 4.1.0) #> rlang 0.4.12 2021-10-18 [1] RSPM (R 4.1.0) #> rmarkdown 2.11 2021-09-14 [1] RSPM (R 4.1.0) #> rstudioapi 0.13 2020-11-12 [1] RSPM (R 4.1.0) #> scales 1.1.1 2020-05-11 [1] RSPM (R 4.1.0) #> sessioninfo 1.2.1 2021-11-02 [1] RSPM (R 4.1.0) #> sp * 1.4-6 2021-11-14 [1] RSPM (R 4.1.0) #> stringi 1.7.5 2021-10-04 [1] CRAN (R 4.1.1) #> stringr 1.4.0 2019-02-10 [1] RSPM (R 4.1.0) #> terra * 1.4-22 2021-11-24 [1] RSPM (R 4.1.0) #> tibble 3.1.6 2021-11-07 [1] RSPM (R 4.1.0) #> tidyselect 1.1.1 2021-04-30 [1] RSPM (R 4.1.0) #> utf8 1.2.2 2021-07-24 [1] RSPM (R 4.1.0) #> vctrs 0.3.8 2021-04-29 [1] RSPM (R 4.1.0) #> viridisLite 0.4.0 2021-04-13 [1] RSPM (R 4.1.0) #> withr 2.4.2 2021-04-18 [1] RSPM (R 4.1.0) #> xfun 0.28 2021-11-04 [1] RSPM (R 4.1.0) #> xml2 1.3.2 2020-04-23 [1] RSPM (R 4.1.0) #> yaml 2.2.1 2020-02-01 [1] RSPM (R 4.1.0) #> zoo 1.8-9 2021-03-09 [1] RSPM (R 4.1.0) #> #> [1] /home/frs/R/x86_64-pc-linux-gnu-library/4.0 #> [2] /opt/R/4.1.1/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
oscarperpinan commented 2 years ago

Thanks for the issue. There is a redundant and incorrect line in this function.

Pakillo commented 2 years ago

Many thanks for the quick fix!

Cheers