# \donttest{
filepath <- system.file("extdata/volcano2.tif", package = "tidyterra")
library(terra)
volcano2_rast <- rast(filepath)
# Palette
plot(volcano2_rast, col = hypso.colors(100, palette = "wiki-2.0_hypso"))
# Palette with uneven colors
plot(volcano2_rast, col = hypso.colors2(100, palette = "wiki-2.0_hypso"))
library(ggplot2)
ggplot() +
geom_spatraster(data = volcano2_rast) +
scale_fill_hypso_c(palette = "colombia_hypso")
# 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_hypso_tint_c(
palette = "etopo1",
labels = scales::label_number(),
breaks = c(-10000, 0, 5000, 8000),
guide = guide_colorbar(reverse = TRUE)
) +
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_hypso_b(breaks = seq(70, 200, 25), palette = "wiki-2.0_hypso")
# With breaks
ggplot() +
geom_spatraster(data = volcano2_rast) +
scale_fill_hypso_b(
breaks = seq(75, 200, 25),
palette = "wiki-2.0_hypso"
)
# 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_hypso_d(na.value = "gray10", palette = "dem_poster")
# Tint version
ggplot() +
geom_spatraster(data = factor, aes(fill = cats)) +
scale_fill_hypso_tint_d(na.value = "gray10", palette = "dem_poster")
# }
# Display all the cpl_city palettes
pals <- unique(hypsometric_tints_db$pal)
# 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 = hypso.colors(ncols, i), main = i,
ylab = "", xaxt = "n", yaxt = "n", bty = "n"
)
}
par(opar)
# Display all the cpl_city palettes on version 2
pals <- unique(hypsometric_tints_db$pal)
# 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 = hypso.colors2(ncols, i), main = i,
ylab = "", xaxt = "n", yaxt = "n", bty = "n"
)
}
par(opar)
Run the code above in your browser using DataLab