-
Notifications
You must be signed in to change notification settings - Fork 6
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
Comments
Hi, Your reprex has an issue, when converting from 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 infosessioninfo::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
#>
#> ────────────────────────────────────────────────────────────────────────────── |
Thank you very much for your reply. The Tidyterra package has helped solve many problems. Thank you for everything you have done. |
您好!我已收到你的来信,一般我会在每天23:00阅读邮件,如有重要事情请直接电话联系--15357902809。非常感谢来电!张梦杰
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
! Plotting only layer "layer.1" of class <factor> Warning message: Mixed layer classes found in
tidyterra::geom_spat*().`
The text was updated successfully, but these errors were encountered: