Creating a geodata poster with R

I had previously seen a few nice examples of rendering a collection of extracts of larger data sets using R. Perhaps something similar could be done with the largest lakes in Svartedalen, Sweden? The name roughly translates to “Black valley”. I remember scuba diving in one of the lakes, perhaps 25 years ago. Someone had parked a car at the bottom of the lake, about 5 m below the surface. With so many lakes to choose from, I will probably never find it again.

Why would you ever need that many lakes? How do people around there get anything done?

Leaving that discussion for later, the main steps for creating the poster are:

  • Define the area of interest, create a polygon in QGIS
  • Download the most recent OSM data set for Sweden from GeoFabrik
  • Import the data in R
  • Extract the desired data
  • Calculate area for each lake and sort them in a descending order
  • Create plot with the 25 largest lakes in the area
  • Export the plot to file

The script:


# Set directory
setwd("C:/Eget/R")

# Load libraries
library(dplyr)
library(ggplot2)
library(sf)
library(stringr)

# Read geodata
svartedalen_polygon <- st_read("C:/Users/johnr/Desktop/Geodata/Svartedalen.gpkg")
lakes <- st_read("C:/Users/johnr/Desktop/Geodata/OpenStreetMap/OSM 2025-10-13/gis_osm_water_a_free_1.shp")

# Filter lakes
lakes <- lakes[lakes$fclass == "water", ]

# Remove lakes outside of Svartedalen
lakes_in_svartedalen <- st_intersection(lakes, svartedalen_polygon)

# Transform to EPSG 3006 and calculate area
lakes_in_svartedalen <- st_transform(lakes_in_svartedalen, 3006)
lakes_in_svartedalen$area_km2 <- round(as.numeric(st_area(lakes_in_svartedalen)) / 1000000, 3)

# Select top 25 lakes
top_25_lakes <- lakes_in_svartedalen[order(-lakes_in_svartedalen$area_km2), ][1:25, ]

# Add rank
top_25_lakes$rank <- 1:25

# Place lakes
for(i in 1:nrow(top_25_lakes)) {
  # Hämta centroid
  centroid <- st_centroid(st_geometry(top_25_lakes[i,]))
  centroid_coords <- st_coordinates(centroid)
  
  # Move geometry
  top_25_lakes$geometry[i] <- st_geometry(top_25_lakes[i,]) - centroid_coords
}

# Add label
top_25_lakes$label <- paste0(top_25_lakes$rank, ". ", top_25_lakes$name, "\n", 
                             top_25_lakes$area_km2, " km²")

top_25_lakes$label <- paste0(top_25_lakes$rank, ". ", 
                             str_wrap(top_25_lakes$name, width = 20), 
                             "\n", top_25_lakes$area_km2, " km²")
top_25_lakes$label <- factor(top_25_lakes$label, levels = top_25_lakes$label)

# Plot
p <- ggplot(top_25_lakes) +
  geom_sf(fill = "#4A90E2", color = "#2E5C8A", linewidth = 0.6) +
  facet_wrap(~label, ncol = 5) +
  theme_void() +
  theme(
    strip.text = element_text(size = 8, face = "bold", color = "white", 
                              margin = margin(8, 5, 8, 5), lineheight = 1.1),
    strip.background = element_rect(fill = "#34495e", color = NA),
    panel.background = element_rect(fill = "#f8f9fa", color = "#dee2e6", linewidth = 0.5),
    panel.spacing = unit(0.8, "lines"),
    plot.background = element_rect(fill = "white", color = NA),
    plot.title = element_text(hjust = 0.5, size = 18, face = "bold", 
                              color = "#2c3e50", margin = margin(b = 20)),
    plot.margin = margin(25, 25, 25, 25)
  ) +
  coord_sf(expand = FALSE) +
  ggtitle("The 25 largest lakes in Svartedalen")

print(p)

ggsave("svartedalen_lakes.png", plot = p, 
       width = 24, height = 16, dpi = 300, bg = "white")

The result:

Acceptable result for the 1.0 version, but there is room for more aesthetics, so there will be a future article on this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please reload

Please Wait