Hillshading and beautiful 3D maps with the Rayshader package in R

Pablo Jarrín-V.
3 min readNov 25, 2022
A sample of Rayshader’s capabilities to render high-quality maps.

The following set of snippets was possible thanks to Morgan-Wall, T. (2022)¹ and the beautiful maps provided by Flotsam on Twitter.

At the core of the render is the engine provided by the rayshader package. Installing may be bumpy, be patient, and make sure you run R and RStudio with full administrator credentials.

Essential libraries for creating the map render:

Setting up the color pallette, check MetBrewer for possibilities. Here are a few options, choose the line that suits you.

The following annotation is copied precisely from @researchremora ‘s snippet published some time ago on Twitter: “read shapefile from GADM for other countries, replace BRA with ISO3C country code for example, if you want Argentina, replace BRA with ARG 0 indicates the largest boundary, also levels 1, 2, and 3.”

Read the file: in this case, I am using Ecuador (…gadm36_ECU_0…) or its provinces (…gadm36_ECU_1…) as the shapefile to delimit the digital elevation model. Choose the line accordingly.

If you only have a single shapefile (e.g. …gadm36_ECU_0…) you can create your raster and associated matrices by directly invoking the assigned variable. Here I use a list of provinces shapefiles derived from the “ecuador1”, which is a data frame of several shapefiles. Use dplyr for making the list.

elevatr allows you to get the elevation raster from the defined boundaries of the selected shapefile. “z” is an important parameter that will determine RAM usage and overall raster spatial resolution. Check the elevatr webpage for details. Depending on “z” you may find critical RAM errors in R. Apply wisely and patiently.

Mask raster with shapefile:

Convert raster to matrix:

An option for memory restrictions is the resize_matrix function:

For setting up the dimensions of the rgl window, the following method is copied from Spencer Schien’s marvelous tutorial. Dynamically set window height and width based on object size:

Rayshader has several functions for several hillshading techniques, we should use “height_shade”, as it has worked for me for pallette and color selection. Rendering resolution is dependent on the rgl window size. I have found that the following is an acceptable setting.

The plot_3d will generate an interactive plot in the rgl window. Before running the last block, choose the preferred orientation of your 3d render.

A movie generated in Rayshader after applying the plot_3d package

This is the final step in generating a high-resolution render. It requires significant computation resources, so be patient and careful in your selection of parameters. An important component is the environmental light, check for .HDR sources in Polyheaven. The selection of the environmental light will determine significant aspects of your render (think of a photographic studio). Take notice of the large window size used, for obtaining appropriate resolution.

Your high-quality render may need some text. you can use the magick package and complementary packages to add text on the obtained high-res render. Check Spencer Schien’s tutorial for a useful and clear explanation of how to do it.

This render was generated with an .HDR file with strong shadows and high contrast.
A diffused studio light setting was used on this render, with soft shadows and low contrast.

[1]#Morgan-Wall T (2022). rayshader: Create Maps and Visualize Data in 2D and 3D. https://www.rayshader.com, https://github.com/tylermorganwall/rayshader, https://www.rayshader.com/.

--

--

Pablo Jarrín-V.

Ubi Dubium Ibi Libertas. Historias de la región amazónica, historias del mundo.