Created
November 13, 2022 08:27
-
-
Save dieghernan/2ea67063a08a674514bc55e74fb30bf8 to your computer and use it in GitHub Desktop.
5 minutes elevation map with hillshade with R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://dieghernan.github.io/202210_tidyterra-hillshade/ | |
library(tidyterra) | |
library(geodata) | |
library(tidyverse) | |
library(scales) | |
r <- elevation_30s(country = "BOL", path = tempdir()) | |
names(r) <- "alt" | |
slope <- terrain(r, "slope", unit = "radians") | |
aspect <- terrain(r, "aspect", unit = "radians") | |
hill <- shade(slope, aspect, 30, 270) | |
# normalize names | |
names(hill) <- "shades" | |
# Hillshading, but we need a palette | |
pal_greys <- hcl.colors(1000, "Grays") | |
index <- hill %>% | |
mutate(index_col = rescale(shades, to = c(1, length(pal_greys)))) %>% | |
mutate(index_col = round(index_col)) %>% | |
pull(index_col) | |
# Get cols | |
vector_cols <- pal_greys[index] | |
ggplot() + | |
geom_spatraster(data = hill, fill=vector_cols, maxcell = Inf) + | |
geom_spatraster(data=r, maxcell = Inf) + | |
scale_fill_hypso_tint_c(limits = as.vector(minmax(r)), | |
palette = "dem_poster", | |
alpha =0.4, | |
labels = scales::label_comma(), | |
breaks = c(seq(0,1000, 250), seq(2000, 6000, 1000))) + | |
guides(fill=guide_legend(title = "elevation", reverse = TRUE)) + | |
labs(title = "Bolivia") + | |
theme_void() + | |
theme(plot.title = element_text(hjust = 0.5, size = 30)) | |
ggsave("bol.png", bg="white", width = 8, height = 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create