dieghernan / tidyterra

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

facet_grid #152

Open danbebber opened 2 months ago

danbebber commented 2 months ago

I've posted this on Stack Overflow: tidyterra allows use of facet_wrap(~lyr) to plot multiple spatial raster layers in one figure.

library(terra)
library(ggplot2)
library(tidyterra)
x <- rast(array(data = rnorm(120,0,1), dim=c(5,5,4)))
names(x) <- c("A_1", "B_1", "A_2", "B_2")
ggplot() +
  geom_spatraster(data = x) +
  facet_wrap(~lyr)

Is there any way of implementing facet_grid in tidyterra so that, in the case above for example, the rows would be labelled 1 and 2, and columns would be labelled A and B? This would require a spatraster object to have more than one attribute (the name of the layer) associated with it, I guess. I don't know if a data.frame can be associated with a spatraster object in which each row refers to a layer?

dieghernan commented 2 months ago

Maybe this? fortify to a tibble with tidyterra method, and then pivot, split labels and plot as a regular data frame.

library(terra)
#> terra 1.7.78
library(ggplot2)
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter
x <- rast(array(data = rnorm(120, 0, 1), dim = c(5, 5, 4)))
names(x) <- c("A_1", "B_1", "A_2", "B_2")
ggplot() +
  geom_spatraster(data = x) +
  facet_wrap(~lyr, ncol = 2) +
  coord_sf(crs = pull_crs(x)) +
  labs(x = "", y = "")


# Fortify method converting to data.frame
x_frt <- fortify(x) %>%
  pivot_longer(-c(x:y), names_to = "lyr", values_to = "value") %>%
  mutate(
    col_n = stringr::str_split_i(lyr, "_", 1),
    row_n = stringr::str_split_i(lyr, "_", 2)
  )

x_frt
#> # A tibble: 100 × 6
#>        x     y lyr    value col_n row_n
#>    <dbl> <dbl> <chr>  <dbl> <chr> <chr>
#>  1   0.5   4.5 A_1   -0.318 A     1    
#>  2   0.5   4.5 B_1    1.02  B     1    
#>  3   0.5   4.5 A_2   -0.612 A     2    
#>  4   0.5   4.5 B_2    0.989 B     2    
#>  5   1.5   4.5 A_1   -2.68  A     1    
#>  6   1.5   4.5 B_1    1.83  B     1    
#>  7   1.5   4.5 A_2   -0.792 A     2    
#>  8   1.5   4.5 B_2    0.132 B     2    
#>  9   2.5   4.5 A_1    0.274 A     1    
#> 10   2.5   4.5 B_1    0.128 B     1    
#> # ℹ 90 more rows

ggplot(x_frt, aes(x, y, z = value, fill = value)) +
  geom_raster() +
  facet_grid(vars(row_n), vars(col_n)) +
  coord_sf(crs = pull_crs(x)) +
  labs(x = "", y = "")

Created on 2024-07-30 with reprex v2.1.1

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.1 (2024-06-14) #> 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-07-30 #> pandoc 3.1.1 @ /usr/lib/rstudio-server/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.1) #> classInt 0.4-10 2023-09-05 [1] RSPM (R 4.4.0) #> cli 3.6.3 2024-06-21 [1] RSPM (R 4.4.0) #> codetools 0.2-20 2024-03-31 [2] CRAN (R 4.4.1) #> colorspace 2.1-1 2024-07-26 [1] RSPM (R 4.4.0) #> curl 5.2.1 2024-03-01 [1] RSPM (R 4.4.0) #> data.table 1.15.4 2024-03-30 [1] RSPM (R 4.4.0) #> DBI 1.2.3 2024-06-02 [1] RSPM (R 4.4.0) #> digest 0.6.36 2024-06-23 [1] RSPM (R 4.4.0) #> dplyr 1.1.4 2023-11-17 [1] RSPM (R 4.4.0) #> e1071 1.7-14 2023-12-06 [1] RSPM (R 4.4.0) #> evaluate 0.24.0 2024-06-10 [1] RSPM (R 4.4.0) #> fansi 1.0.6 2023-12-08 [1] RSPM (R 4.4.0) #> farver 2.1.2 2024-05-13 [1] RSPM (R 4.4.0) #> fastmap 1.2.0 2024-05-15 [1] RSPM (R 4.4.0) #> fs 1.6.4 2024-04-25 [1] RSPM (R 4.4.0) #> generics 0.1.3 2022-07-05 [1] RSPM (R 4.4.0) #> ggplot2 * 3.5.1 2024-04-23 [1] RSPM (R 4.4.0) #> glue 1.7.0 2024-01-09 [1] RSPM (R 4.4.0) #> gtable 0.3.5 2024-04-22 [1] RSPM (R 4.4.0) #> highr 0.11 2024-05-26 [1] RSPM (R 4.4.0) #> htmltools 0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0) #> KernSmooth 2.23-24 2024-05-17 [2] CRAN (R 4.4.1) #> knitr 1.48 2024-07-07 [1] RSPM (R 4.4.0) #> labeling 0.4.3 2023-08-29 [1] RSPM (R 4.4.0) #> lifecycle 1.0.4 2023-11-07 [1] RSPM (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] RSPM (R 4.4.0) #> munsell 0.5.1 2024-04-01 [1] RSPM (R 4.4.0) #> pillar 1.9.0 2023-03-22 [1] RSPM (R 4.4.0) #> pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.4.0) #> proxy 0.4-27 2022-06-09 [1] RSPM (R 4.4.0) #> purrr 1.0.2 2023-08-10 [1] RSPM (R 4.4.0) #> R6 2.5.1 2021-08-19 [1] RSPM (R 4.4.0) #> Rcpp 1.0.13 2024-07-17 [1] RSPM (R 4.4.0) #> reprex 2.1.1 2024-07-06 [1] RSPM (R 4.4.0) #> rlang 1.1.4 2024-06-04 [1] RSPM (R 4.4.0) #> rmarkdown 2.27 2024-05-17 [1] RSPM (R 4.4.0) #> rstudioapi 0.16.0 2024-03-24 [1] RSPM (R 4.4.0) #> scales 1.3.0 2023-11-28 [1] RSPM (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.4.0) #> sf 1.0-16 2024-03-24 [1] RSPM (R 4.4.0) #> stringi 1.8.4 2024-05-06 [1] RSPM (R 4.4.0) #> stringr 1.5.1 2023-11-14 [1] RSPM (R 4.4.0) #> terra * 1.7-78 2024-05-22 [1] RSPM (R 4.4.0) #> tibble 3.2.1 2023-03-20 [1] RSPM (R 4.4.0) #> tidyr 1.3.1 2024-01-24 [1] RSPM (R 4.4.0) #> tidyselect 1.2.1 2024-03-11 [1] RSPM (R 4.4.0) #> tidyterra * 0.6.1 2024-06-08 [1] RSPM (R 4.4.0) #> units 0.8-5 2023-11-28 [1] RSPM (R 4.4.0) #> utf8 1.2.4 2023-10-22 [1] RSPM (R 4.4.0) #> vctrs 0.6.5 2023-12-01 [1] RSPM (R 4.4.0) #> withr 3.0.0 2024-01-16 [1] RSPM (R 4.4.0) #> xfun 0.46 2024-07-18 [1] RSPM (R 4.4.0) #> xml2 1.3.6 2023-12-04 [1] RSPM (R 4.4.0) #> yaml 2.3.10 2024-07-26 [1] RSPM (R 4.4.0) #> #> [1] /cloud/lib/x86_64-pc-linux-gnu-library/4.4 #> [2] /opt/R/4.4.1/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```