# \donttest{
filepath <- system.file("extdata/volcano2.tif", package = "tidyterra")
library(terra)
volcano2_rast <- rast(filepath)
# Palette
plot(volcano2_rast, col = grass.colors(100, palette = "haxby"))
library(ggplot2)
ggplot() +
geom_spatraster(data = volcano2_rast) +
scale_fill_grass_c(palette = "terrain")
# Use with no default limits
ggplot() +
geom_spatraster(data = volcano2_rast) +
scale_fill_grass_c(palette = "terrain", use_grass_range = FALSE)
# Full map with true tints
f_asia <- system.file("extdata/asia.tif", package = "tidyterra")
asia <- rast(f_asia)
ggplot() +
geom_spatraster(data = asia) +
scale_fill_grass_c(
palette = "srtm_plus",
labels = scales::label_number(),
breaks = c(-10000, 0, 5000, 8000),
guide = guide_colorbar(reverse = FALSE)
) +
labs(fill = "elevation (m)") +
theme(
legend.position = "bottom",
legend.title.position = "top",
legend.key.width = rel(3),
legend.ticks = element_line(colour = "black", linewidth = 0.3),
legend.direction = "horizontal"
)
# Binned
ggplot() +
geom_spatraster(data = volcano2_rast) +
scale_fill_grass_b(breaks = seq(70, 200, 25), palette = "sepia")
# With discrete values
factor <- volcano2_rast %>%
mutate(cats = cut(elevation,
breaks = c(100, 120, 130, 150, 170, 200),
labels = c(
"Very Low", "Low", "Average", "High",
"Very High"
)
))
ggplot() +
geom_spatraster(data = factor, aes(fill = cats)) +
scale_fill_grass_d(palette = "soilmoisture")
# }
# Display all the GRASS palettes
data("grass_db")
pals_all <- unique(grass_db$pal)
# In batches
pals <- pals_all[c(1:25)]
# Helper fun for plotting
ncols <- 128
rowcol <- grDevices::n2mfrow(length(pals))
opar <- par(no.readonly = TRUE)
par(mfrow = rowcol, mar = rep(1, 4))
for (i in pals) {
image(
x = seq(1, ncols), y = 1, z = as.matrix(seq(1, ncols)),
col = grass.colors(ncols, i), main = i,
ylab = "", xaxt = "n", yaxt = "n", bty = "n"
)
}
par(opar)
# Second batch
pals <- pals_all[-c(1:25)]
ncols <- 128
rowcol <- grDevices::n2mfrow(length(pals))
opar <- par(no.readonly = TRUE)
par(mfrow = rowcol, mar = rep(1, 4))
for (i in pals) {
image(
x = seq(1, ncols), y = 1, z = as.matrix(seq(1, ncols)),
col = grass.colors(ncols, i), main = i,
ylab = "", xaxt = "n", yaxt = "n", bty = "n"
)
}
par(opar)
Run the code above in your browser using DataLab