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

How to use tidyterra for factor stacked raster data? #148

Closed
mengjiezhang4ds opened this issue Apr 24, 2024 · 3 comments
Closed

How to use tidyterra for factor stacked raster data? #148

mengjiezhang4ds opened this issue Apr 24, 2024 · 3 comments

Comments

@mengjiezhang4ds
Copy link

I randomly generated two raster data and stacked them to convert them into factor values, but when I used tidyterra to plot, only the first raster data was displayed. Why is this? How can I modify it to display all the data?

library(raster)
r1 <- raster(matrix(rep(1:3, 4), nrow = 4))
r2 <- raster(matrix(rep(1:4, 3), nrow = 4))
r <- stack(r1, r2)
r <- stack(lapply(as.list(r), as.factor))
library(ggplot2)
library(terra)
library(tidyterra)
ggplot() +
  tidyterra::geom_spatraster(data = rast(r)) +
  facet_wrap(~lyr) +
  scale_fill_whitebox_d(
    palette = "muted"
  ) +
  theme_light()

! Plotting only layer "layer.1" of class <factor> Warning message: Mixed layer classes found intidyterra::geom_spat*().
`

@dieghernan
Copy link
Owner

Hi,

Your reprex has an issue, when converting from RasterStack to SpatRaster only layer.1 retains the factor class and layer.2 is parsed as numeric. Converting it back to factor make it works:

library(raster)
#> Loading required package: sp
r1 <- raster(matrix(rep(1:3, 4), nrow = 4))
r2 <- raster(matrix(rep(1:4, 3), nrow = 4))
r <- stack(r1, r2)
r <- stack(lapply(as.list(r), as.factor))

is.factor(r$layer.1)
#> [1] TRUE
is.factor(r$layer.2)
#> [1] TRUE

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

as_terra <- terra::rast(r)

is.factor(as_terra$layer.1)
#> [1] TRUE
is.factor(as_terra$layer.2)
#> [1] FALSE
head(as_tibble(as_terra), 3)
#> # A tibble: 3 × 2
#>   layer.1 layer.2
#>   <fct>     <int>
#> 1 1             1
#> 2 2             1
#> 3 3             1

# layer.2 is not a factor after converting to SpatRaster
as_terra$layer.2 <- as.factor(as_terra$layer.2)

is.factor(as_terra$layer.2)
#> [1] TRUE


ggplot() +
  tidyterra::geom_spatraster(data = as_terra) +
  facet_wrap(~lyr) +
  scale_fill_whitebox_d(
    palette = "muted"
  ) +
  theme_light()

Created on 2024-04-24 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29)
#>  os       Ubuntu 20.04.6 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  C.UTF-8
#>  ctype    C.UTF-8
#>  tz       UTC
#>  date     2024-04-24
#>  pandoc   3.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [1] RSPM (R 4.3.0)
#>  codetools     0.2-19  2023-02-01 [2] CRAN (R 4.3.3)
#>  colorspace    2.1-0   2023-01-23 [1] RSPM (R 4.3.0)
#>  curl          5.2.1   2024-03-01 [1] RSPM (R 4.3.0)
#>  data.table    1.15.4  2024-03-30 [1] RSPM (R 4.3.0)
#>  digest        0.6.35  2024-03-11 [1] RSPM (R 4.3.0)
#>  dplyr         1.1.4   2023-11-17 [1] RSPM (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [1] RSPM (R 4.3.0)
#>  fansi         1.0.6   2023-12-08 [1] RSPM (R 4.3.0)
#>  farver        2.1.1   2022-07-06 [1] RSPM (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] RSPM (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] RSPM (R 4.3.0)
#>  generics      0.1.3   2022-07-05 [1] RSPM (R 4.3.0)
#>  ggplot2     * 3.5.0   2024-02-23 [1] RSPM (R 4.3.0)
#>  glue          1.7.0   2024-01-09 [1] RSPM (R 4.3.0)
#>  gtable        0.3.5   2024-04-22 [1] RSPM (R 4.3.0)
#>  highr         0.10    2022-12-22 [1] RSPM (R 4.3.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] RSPM (R 4.3.0)
#>  knitr         1.46    2024-04-06 [1] RSPM (R 4.3.0)
#>  labeling      0.4.3   2023-08-29 [1] RSPM (R 4.3.0)
#>  lattice       0.22-5  2023-10-24 [2] CRAN (R 4.3.3)
#>  lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.3.0)
#>  munsell       0.5.1   2024-04-01 [1] RSPM (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] RSPM (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.3.0)
#>  purrr         1.0.2   2023-08-10 [1] RSPM (R 4.3.0)
#>  R.cache       0.16.0  2022-07-21 [1] RSPM (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] RSPM (R 4.3.0)
#>  R.oo          1.26.0  2024-01-24 [1] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [1] RSPM (R 4.3.0)
#>  R6            2.5.1   2021-08-19 [1] RSPM (R 4.3.0)
#>  raster      * 3.6-26  2023-10-14 [1] RSPM (R 4.3.0)
#>  Rcpp          1.0.12  2024-01-09 [1] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [1] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [1] RSPM (R 4.3.0)
#>  rmarkdown     2.26    2024-03-05 [1] RSPM (R 4.3.0)
#>  rstudioapi    0.16.0  2024-03-24 [1] RSPM (R 4.3.0)
#>  scales        1.3.0   2023-11-28 [1] RSPM (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.3.0)
#>  sp          * 2.1-3   2024-01-30 [1] RSPM (R 4.3.0)
#>  styler        1.10.3  2024-04-07 [1] RSPM (R 4.3.0)
#>  terra       * 1.7-71  2024-01-31 [1] RSPM (R 4.3.0)
#>  tibble        3.2.1   2023-03-20 [1] RSPM (R 4.3.0)
#>  tidyr         1.3.1   2024-01-24 [1] RSPM (R 4.3.0)
#>  tidyselect    1.2.1   2024-03-11 [1] RSPM (R 4.3.0)
#>  tidyterra   * 0.6.0   2024-04-22 [1] RSPM (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [1] RSPM (R 4.3.0)
#>  vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [1] RSPM (R 4.3.0)
#>  xfun          0.43    2024-03-25 [1] RSPM (R 4.3.0)
#>  xml2          1.3.6   2023-12-04 [1] RSPM (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [1] RSPM (R 4.3.0)
#> 
#>  [1] /cloud/lib/x86_64-pc-linux-gnu-library/4.3
#>  [2] /opt/R/4.3.3/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@mengjiezhang4ds
Copy link
Author

Thank you very much for your reply. The Tidyterra package has helped solve many problems. Thank you for everything you have done.

@mengjiezhang4ds
Copy link
Author

mengjiezhang4ds commented May 23, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants