Skip to contents

group_graph() turns a partition of the units of a unit_graph() into the graph of its groups, with their areas and the boundaries they share, so group_units() can build the next level with the same engine: enumeration areas into supervision areas, those into work zones. Each level nests in the one below by construction, and the geometry is not read again.

Usage

group_graph(graph, group, within = NULL)

Arguments

graph

A unit_graph().

group

The group of each unit, an atomic vector with no NA.

within

The part each group lies in at the new level, as a vector with one value per unit, the same for all units of a group, or NULL (default) to keep the parts of graph. Parts coarser than those of graph, such as the cercle over communes, join groups of different parts of graph, which needs the graph built with between = TRUE.

Value

A unit_graph with one unit per group, in order of first appearance, holding the same tables as unit_graph(). Its units table adds group, the label of each group. Sizes are not part of a graph. Sum them per group in the same order, for example with tapply(size, factor(group, unique(group)), sum).

Details

Two groups are adjacent when any of their units are, and their shared boundary is the sum over those pairs, with the part on visible lines kept. Pairs of units across a barrier become crossings between groups, with the highest rank, so barriers hold at every level. A group keeps its units' patch when they all share one.

With coarser parts, the pairs of units of different parts of graph that lie in the same new part become edges. Their boundary follows an admin line, and counts as not visible.

See also

group_units(), unit_graph() and its between.

Other grouping and cutting functions: dissolve_units(), group_units(), order_units(), split_units(), unit_graph()

Examples

cells <- sf::st_make_grid(
  sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 800, ymax = 400)),
  cellsize = 100
)
x <- sf::st_sf(geometry = sf::st_set_crs(cells, 32630))
xy <- sf::st_coordinates(sf::st_centroid(x))
x$commune <- ifelse(xy[, 1] < 400, "A", "B")
x$pop <- 40

# Enumeration areas within communes
g <- unit_graph(x, within = "commune", between = TRUE)
ea <- group_units(g, x$pop, max = 200, seed = 1)

# Supervision areas of about four EAs, across the commune line
gg <- group_graph(g, ea, within = rep("cercle", nrow(x)))
ea_pop <- tapply(x$pop, factor(ea, unique(ea)), sum)
sa <- group_units(gg, ea_pop, max = 800, seed = 1)
table(sa[match(ea, unique(ea))], x$commune)
#>    
#>      A  B
#>   1 16  0
#>   2  0 16