Creates a choropleth (colored) map by joining external data to territorial unit boundaries. Works with any Italian territorial unit type (comuni, province, regioni, ripartizioni). Useful for visualizing statistics, census data, or any other territorial-level indicators.

map_territorial_choropleth(
  territorial_sf,
  data,
  join_by = NULL,
  value_col,
  name_col = NULL,
  palette = "YlOrRd",
  legend_title = value_col,
  popup_fields = NULL
)

Arguments

territorial_sf

An sf object with territorial unit boundaries

data

A data.frame or data.table with values to visualize

join_by

Character string. Column name to join on (must exist in both territorial_sf and data). If NULL (default), auto-detects based on territorial type (PRO_COM, COD_REG, COD_UTS, COD_RIP).

value_col

Character string. Column name in data containing values to visualize (e.g., population, income, etc.)

name_col

Character string. Column name to use for labels. If NULL (default), auto-detects based on territorial type.

palette

Character string. Color palette for choropleth. Options: "YlOrRd" (default), "YlGnBu", "RdYlGn", "Spectral", or any RColorBrewer palette.

legend_title

Character string. Title for the legend. Defaults to value_col.

popup_fields

Character vector. Additional fields to show in popup. If NULL, auto-selected based on territorial type.

Value

A leaflet map object

Examples

if (FALSE) { # \dontrun{
# Choropleth for comuni
comuni <- readRDS("data/comuni_map.rds")
pop_data <- data.frame(
  PRO_COM = sample(comuni$PRO_COM, 100),
  population = runif(100, 1000, 100000)
)
map_territorial_choropleth(
  comuni,
  data = pop_data,
  value_col = "population",
  legend_title = "Population"
)

# Choropleth for regioni
regioni <- sf::st_read("regioni.geojson")
gdp_data <- data.frame(
  COD_REG = 1:20,
  gdp_per_capita = rnorm(20, 25000, 5000)
)
map_territorial_choropleth(
  regioni,
  data = gdp_data,
  value_col = "gdp_per_capita",
  legend_title = "GDP per capita (EUR)"
)

# Choropleth for province with custom join
province <- sf::st_read("province.geojson")
employment_data <- data.frame(
  COD_UTS = unique(province$COD_UTS),
  unemployment_rate = runif(length(unique(province$COD_UTS)), 5, 15)
)
map_territorial_choropleth(
  province,
  data = employment_data,
  join_by = "COD_UTS",
  value_col = "unemployment_rate",
  legend_title = "Unemployment Rate (%)"
)
} # }