# 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)