# \donttest{
# Avg temperature on spring in Castille and Leon (Spain)
file_path <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
library(terra)
temp_rast <- rast(file_path)
library(ggplot2)
# Display a single layer
names(temp_rast)
ggplot() +
geom_spatraster(data = temp_rast, aes(fill = tavg_04)) +
# You can use coord_sf
coord_sf(crs = 3857) +
scale_fill_grass_c(palette = "celsius")
# Display facets
ggplot() +
geom_spatraster(data = temp_rast) +
facet_wrap(~lyr, ncol = 2) +
scale_fill_grass_b(palette = "celsius", breaks = seq(0, 20, 2.5))
# Non spatial rasters
no_crs <- rast(crs = NA, extent = c(0, 100, 0, 100), nlyr = 1)
values(no_crs) <- seq_len(ncell(no_crs))
ggplot() +
geom_spatraster(data = no_crs)
# Downsample
ggplot() +
geom_spatraster(data = no_crs, maxcell = 25)
# }
# \donttest{
# Using stat_spatraster
# Default
ggplot() +
stat_spatraster(data = temp_rast) +
facet_wrap(~lyr)
# Using points
ggplot() +
stat_spatraster(
data = temp_rast,
aes(color = after_stat(value)),
geom = "point", maxcell = 250
) +
scale_colour_viridis_c(na.value = "transparent") +
facet_wrap(~lyr)
# Using points and labels
r_single <- temp_rast %>% select(1)
ggplot() +
stat_spatraster(
data = r_single,
aes(color = after_stat(value)),
geom = "point",
maxcell = 2000
) +
stat_spatraster(
data = r_single,
aes(label = after_stat(round(value, 2))),
geom = "label",
alpha = 0.85,
maxcell = 20
) +
scale_colour_viridis_c(na.value = "transparent")
# }
Run the code above in your browser using DataLab