
Color admin groups so touching neighbors never share a color
Source:R/plot_admin_map_distinct.R
plot_admin_map_distinct.RdThis function takes an adm2-level shapefile, dissolves it into a higher
administrative level (e.g., provinces at adm1), constructs a neighbor graph,
assigns colors to the aggregated units ensuring that no adjacent units share
the same color, and then maps those colors back to the adm2 polygons.
Labels are placed at the centroid of each higher-level unit, and a
ggplot2 map is produced. The plot can optionally be saved as a PNG.
Usage
plot_admin_map_distinct(
shp,
group_col,
id_col,
out_png = NULL,
palette = c("#D95F02", "#1B9E77", "#7570B3", "#E7298A", "#66A61E", "#E6AB02"),
fix_valid = FALSE
)Arguments
- shp
sf. Polygon layer at adm2 level containing a column for the higher-level grouping (adm1) and unique adm2 identifiers.
- group_col
character. Column name for higher-level admin grouping (e.g., adm1).
- id_col
character. Column name for unique adm2 identifiers.
- out_png
character|NULL. File path to save PNG output. If
NULL, no file is saved.- palette
character. Vector of hex colors to use for coloring groups. Will be recycled if there are more groups than colors.
- fix_valid
logical. If
TRUE, runsf::st_make_valid()to fix invalid geometries before processing.