Exploring {hillshader} for shaded relief maps with {ggplot2}, {terra} and {tidyterra}
Maps
Geocomputation
{elevatr}
{hillshader}
Author
Aditya Dahiya
Published
February 2, 2025
Exploring the {hillshader} package
The hillshader package (Roudier 2024) is an R tool designed to create shaded relief maps using ray-tracing techniques.t serves as a wrapper around the rayshader(Morgan-Wall 2024) and raster(Hijmans 2024) packages, facilitating the generation of hillshade relief maps and their export to spatial files.
The primary function, hillshader(), allows for the creation of hillshade maps as RasterLayer objects. Users can customize the shading process by specifying different shader functions, such as ray_shade and ambient_shade, and adjust parameters like sun angle and altitude to achieve desired visual effects. Additionally, the package offers functions like add_shadow_2d, matrix_to_raster, and write_raster to enhance integration with rayshader pipelines and GIS workflows.
Code
# Data Import and Wrangling Toolslibrary(sf) # Handling simple features in Rlibrary(terra) # Handling rasters in Rlibrary(tidyterra) # Rasters with ggplot2library(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text in ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Composing Plots# Package to explorelibrary(rayshader) # 2D / 3D map visualizationslibrary(raster) # Handling rasterslibrary(hillshader) # Shaded reliefs in R# Making tables in Rlibrary(gt) # Beautiful Tablesbts =36# Base Text Sizesysfonts::font_add_google("Roboto Condensed", "body_font")sysfonts::font_add_google("Oswald", "title_font")showtext::showtext_auto()theme_set(theme_minimal(base_size = bts,base_family ="body_font" ) +theme(text =element_text(colour ="grey30",lineheight =0.3,margin =margin(0,0,0,0, "pt") ),plot.title =element_text(hjust =0.5 ),plot.subtitle =element_text(hjust =0.5 ) ))# Some basic caption stuff# A base Colourbg_col <-"white"seecolor::print_color(bg_col)# Colour for highlighted texttext_hil <-"grey30"seecolor::print_color(text_hil)# Colour for the texttext_col <-"grey20"seecolor::print_color(text_col)# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""xtwitter_username <-"@adityadahiyaias"social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username} </span>")social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")plot_caption <-paste0("**Tools**: {hillshader} *#rstats* "," | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)
Another example for a smaller area - Sikkim (India)
The analysis uses the hillshader package to generate five different hillshade maps of Sikkim, India, by varying the sunangle and sunaltitude parameters. The base map is created by extracting elevation data using elevatr::get_elev_raster() and masking it with the Sikkim state boundary from a shapefile loaded with sf::read_sf(). The function plot_sikkim() applies hillshader() with ray-traced shading techniques (ray_shade and ambient_shade) and visualizes the results using ggplot2::geom_spatraster() for a grayscale effect. Different combinations of sunlight direction (sunangle) and height (sunaltitude) alter the relief perception. The six maps, including an elevation reference, are arranged with patchwork::wrap_plots(), demonstrating how terrain visualization changes under varying lighting conditions.
This visualization showcases the impact of varying sun angles and altitudes on hillshade maps of Sikkim, India, using the {hillshader} package. The top-left map represents the original elevation data, while the remaining five maps illustrate different shading effects based on changes in sunangle (direction of sunlight) and sunaltitude (height of the sun above the horizon). By adjusting these parameters, the perception of terrain depth and structure changes, highlighting how light sources influence the visualization of topography.