Replies: 2 comments
-
Hi @sraul1 I think this is a functionality that can't be implemented at this moment. However, you can try the following approaches, see if any of them suits your needs: library(ggplot2)
library(tidyterra)
#> ── Attaching packages ─────────────────────────────────────── tidyterra 0.2.0 ──
#>
#> Suppress this startup message by setting Sys.setenv(tidyterra.quiet = TRUE)
#> ✔ tibble 3.1.7 ✔ dplyr 1.0.9
#> ✔ tidyr 1.2.0
library(terra)
#> terra 1.5.34
#>
#> Attaching package: 'terra'
#> The following object is masked from 'package:tidyr':
#>
#> extract
pols <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) %>%
filter(name %in% c("Soria", "León"))
# Create a SpatRasterCollection
r <- rast(system.file("extdata/cyl_elev.tif", package = "tidyterra"))
pols <- project(pols, pull_crs(r))
r1 <- crop(r, pols[1, ]) %>% mask(pols[1, ])
r2 <- crop(r, pols[2, ]) %>% mask(pols[2, ])
src <- sprc(r1, r2)
plot(src[1]) plot(src[2]) # Solution with cowplot
gt <- purrr::map(
seq_len(length(src)),
function(x) {
ggplot() +
geom_spatraster(data = src[x]) +
# Set same limits to all the plots
scale_fill_terrain_c(limits = c(400, 2000)) +
labs(
title = pols[x, ]$name,
fill = "elevation"
)
}
)
cowplot::plot_grid(plotlist = gt) # Additional solution: use pixel values plus geom_raster
df_raster <- lapply(seq_len(length(src)), function(x) {
df <- as_tibble(src[x], xy = TRUE)
df$name <- pols$name[x]
return(df)
}) %>% bind_rows()
# Note that the axes are not displayed in degrees, but x,y coordinates
ggplot(df_raster) +
geom_raster(aes(x, y, fill = elevation_m)) +
facet_wrap(~name, scales = "free") +
scale_fill_terrain_c()
#> Warning: Raster pixels are placed at uneven horizontal intervals and will be
#> shifted. Consider using geom_tile() instead.
#> Warning: Raster pixels are placed at uneven vertical intervals and will be
#> shifted. Consider using geom_tile() instead.
#> Warning: Removed 2748 rows containing missing values (geom_raster). Created on 2022-09-12 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
0 replies
-
These will work! Was looking at what different raster resolutions were doing for a site and just wanted an easy way to view them all at once without repeated ggplot calls |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Not sure how feasible this would be, but it would be a nice addition if we were able to facet/plot all sub-datasets within a SpatRasterCollection (where each sub-dataset doesn't necessarily need to have the same resolution/extent).
Maybe this is already achievable and I just haven't come to the solution for it.
This a fantastic package, I love to use it. Syntax is exactly what I have been looking for in terms of spatial plotting within a tidyverse framework.
Beta Was this translation helpful? Give feedback.
All reactions