Day 09: Urban-rural

Published

November 9, 2026

Dorling cartogram of Pays de la Loire: one circle per commune, its area proportional to its residents in 2022, pushed apart so none overlap, over the department borders. Circles are coloured by the census of the commune's highest population. Huge orange circles, at their record since 2010, cluster around Nantes, Angers and La Roche-sur-Yon and along the Atlantic. Small navy dots, communes that peaked in 1876 or earlier, are scattered across Mayenne and rural Sarthe. Le Mans and Laval are large grey-blue circles, past their 1975 peak.

# Start R at the project root
source("2026/_kit/base.R")
source("2026/_kit/atlas.R")

library(dplyr)

day <- 9
inks <- atlas_ink(day)

series <- "evolution-de-la-population-des-pays-de-la-loire-de-1876-a-2019-copie"

# Population only since shapes would make it heavy
# 8 communes appear under 2 spellings so keep one row per census
census <- ods_table("region", series, select = "insee,annee,population") |>
  transmute(
    insee,
    department = substr(insee, 1, 2),
    year = as.integer(substr(annee, 1, 4)),
    population = as.numeric(population)
  ) |>
  filter(!is.na(population)) |>
  distinct(insee, year, .keep_all = TRUE)

print(length(unique(census$year)))

# Census with the highest population and the earliest one if tied
years <- sort(unique(census$year))
between <- range(years[years > 1876 & years < 2010])
classes <- c("1876 ou avant", sprintf("De %d à %d", between[1], between[2]), "Depuis 2010")

peaks <- census |>
  group_by(insee, department) |>
  arrange(desc(population), year) |>
  slice(1) |>
  ungroup() |>
  mutate(
    class = case_when(
      year == 1876 ~ classes[1],
      year >= 2010 ~ classes[3],
      .default = classes[2]
    ),
    class = factor(class, levels = classes)
  )

# Residents at the latest census
latest <- census |>
  filter(year == max(year)) |>
  select(insee, residents = population)

peaks <- peaks |>
  left_join(latest, by = "insee") |>
  mutate(ratio = residents / population)

# Share of residents living in a commune at its record
record_share <- function(rows) {
  sum(rows$residents[rows$class == classes[3]], na.rm = TRUE) /
    sum(rows$residents, na.rm = TRUE)
}

by_department <- peaks |>
  group_by(department) |>
  group_modify(\(rows, key) tibble(share = record_share(rows))) |>
  ungroup()

region_share <- record_share(peaks)
communes_share <- mean(peaks$class == classes[3])

# Rural exodus for the subtitle
before_1914 <- mean(peaks$year <= 1911)
lost_half <- sum(peaks$ratio < 0.5, na.rm = TRUE)
print(c(before_1914, lost_half))

department_share <- \(code) by_department$share[by_department$department == code]

# Commune shapes from a single census
communes <- ods_get("region", series, where = "year(annee)=2022") |>
  distinct(insee, .keep_all = TRUE) |>
  select(insee) |>
  left_join(select(peaks, insee, class, residents, year, ratio), by = "insee")

region <- base_get("region")

# Dorling cartogram where circles sized by residents push apart until none overlap
dorling <- function(x, y, r, steps = 2000, pull = 0.01) {
  home_x <- x
  home_y <- y
  for (step in seq_len(steps)) {
    dx <- outer(x, x, "-")
    dy <- outer(y, y, "-")
    d <- pmax(sqrt(dx^2 + dy^2), 1)
    diag(d) <- Inf
    overlap <- pmax(outer(r, r, "+") - d, 0)
    if (max(overlap) < 20) break
    # Smaller circles move more
    push <- 0.5 * overlap * outer(r, r, \(a, b) b / (a + b)) / d
    # The pull home fades so circles can fully separate at the end
    fade <- pull * max(0, 1 - step / (0.6 * steps))
    x <- x + rowSums(push * dx) + fade * (home_x - x)
    y <- y + rowSums(push * dy) + fade * (home_y - y)
  }
  message("dorling stopped after ", step, " steps with ", round(max(overlap)), " m of overlap left")
  list(x = x, y = y)
}

# Circles cover 55 % of the region so a village of 1000 still shows
dots <- communes |>
  filter(residents > 0) |>
  st_set_agr("constant") |>
  st_point_on_surface()

region_area <- as.numeric(st_area(st_union(region)))
radius <- \(people) sqrt(0.55 * region_area * people / (pi * sum(dots$residents)))

# Layout cached since it takes a few minutes
layout_cache <- cache_path("day_09", paste0("dorling", cache_suffix(dots$insee, dots$residents), ".rds"))
if (fs::file_exists(layout_cache)) {
  moved <- readRDS(layout_cache)
} else {
  set.seed(9)
  start <- st_coordinates(dots) + runif(2 * nrow(dots), -1, 1)
  moved <- dorling(start[, 1], start[, 2], radius(dots$residents))
  saveRDS(moved, layout_cache)
}

circles <- dots |>
  st_drop_geometry() |>
  mutate(x = moved$x, y = moved$y) |>
  st_as_sf(coords = c("x", "y"), crs = atlas_crs) |>
  st_buffer(radius(dots$residents), nQuadSegs = 16) |>
  arrange(desc(residents))

# Size key in map units at the bottom right
key_people <- c(1000, 10000, 100000)
key <- tibble(
  people = key_people,
  r = radius(key_people),
  x = 492000,
  y = 6605000 + r,
  top = 6605000 + 2 * r,
  # Spread the labels so the 2 small ones do not touch
  label_y = top + c(-3500, 1500, 0),
  label = c("1 000", "10 000", "100 000")
)
key_circles <- st_as_sf(key, coords = c("x", "y"), crs = atlas_crs) |>
  st_buffer(key$r, nQuadSegs = 32)
colours <- c("#17233A", "#93A0B5", inks$accent)
names(colours) <- classes

map <- ggplot() +
  geom_sf(data = region, fill = NA, colour = alpha(inks$ink, 0.55), linewidth = 0.45) +
  geom_sf(
    data = circles,
    aes(fill = class),
    colour = inks$paper,
    linewidth = 0.12,
    key_glyph = "point"
  ) +
  geom_sf(data = key_circles, fill = NA, colour = inks$ink, linewidth = 0.3) +
  annotate("segment", x = 492000, xend = 509000, y = key$top, yend = key$label_y, colour = inks$ink, linewidth = 0.2) +
  annotate(
    "text",
    x = 510500,
    y = key$label_y,
    label = key$label,
    hjust = 0,
    family = "atlas_mono",
    size = 12 / .pt,
    colour = inks$ink
  ) +
  annotate(
    "text",
    x = 478000,
    y = max(key$top) + 9000,
    label = "Habitants en 2022",
    hjust = 0,
    family = "atlas_display",
    fontface = "bold",
    size = 14 / .pt,
    colour = inks$ink
  ) +
  scale_fill_manual(
    values = colours,
    name = "Population au plus haut",
    guide = guide_legend(override.aes = list(shape = 21, size = 6, colour = NA))
  ) +
  atlas_coord(x = atlas_region$x, y = atlas_region$y, ticks = FALSE) +
  atlas_theme(day, frame = FALSE) +
  # Colour legend at the top left of the map
  theme_sub_legend(
    position = "inside",
    position.inside = c(0.02, 0.98),
    justification.inside = c(0, 1),
    direction = "vertical",
    key.size = unit(20, "pt"),
    text = element_text(size = 13),
    title = element_text(size = 14, face = "bold")
  )

numbers <- c("Une", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf")

plate <- atlas_plate(
  map,
  day,
  title = sprintf(
    "%s habitants sur dix vivent dans\nune commune à son record de population",
    numbers[round(10 * region_share)]
  ),
  subtitle = sprintf(
    "Mais %.0f %% des communes ont culminé avant 1914, et %d ont perdu plus de la moitié de leurs habitants depuis leur record.",
    100 * before_1914,
    lost_half
  ),
  source = "Insee, recensements 1876-2022, via data.paysdelaloire.fr",
  layout = "bleed",
  title_size = 32
)

atlas_save(plate, day)
Back to top