Skip to contents

This 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, run sf::st_make_valid() to fix invalid geometries before processing.

Value

A list with three components:

  • shp: sf with group colors joined back to adm2 polygons

  • labels: sf with label coordinates (x, y) for each group

  • gg: ggplot object of the colored map with labels

Examples

# Not run:
# res <- plot_admin_map_distinct(
#   shp = shp_output_old$spat_vec$adm2,
#   group_col = "adm1",
#   id_col = "adm2",
#   out_png = here::here("03_outputs/plots/burundi_health_dist_old2.png")
# )
# print(res$gg)