Ridgelines

Day 2 of 30DayMapChallenge. Lines
R
raster
datavisualization
30DayMapChallenge
Author

Michaël

Published

2025-11-02

Modified

2025-11-02

A photo of a hiking trail below a steep ridge in the clouds

Hiking Path – CC BY-NC-SA by HD

Day 2 of 30DayMapChallenge: « Lines » (previously).

Representing a digital elevation model as ridgelines…

Config

library(elevatr)
library(rnaturalearth)
# remotes::install_github("ropensci/rnaturalearthhires")
library(terra)
library(ggplot2)
library(ggridges)

Data

ch <- ne_countries(scale = 10, country = "switzerland")

ele <- get_elev_raster(ch, z = 9)
names(ele) <- "z"

Map

ele |> 
  mask(ch) |> 
  aggregate(fact = 20) |> 
  as.data.frame(xy = TRUE) |> 
  ggplot() +
  geom_density_ridges(aes(x = x, y = y, group = y, height = z),
                      stat = "identity",
                      alpha = 0.3,
                      color = "red",
                      fill = "white",
                      scale = 10) +
  labs(title = "Switzerland",
       caption = paste("Elevation data from
       https://registry.opendata.aws/terrain-tiles
                      https://r.iresmi.net/", Sys.Date())) +
  theme_void() + 
  theme(plot.background = element_rect(fill = "grey"),
        title = element_text(color = "red"),
        plot.caption = element_text(color = "#777"),
        plot.margin = margin(2, 2, 2, 5, unit = "mm"))
A map of Switzerland altitude
Figure 1: Swiss ridgelines