flowmap() looks like one step, but the figure is the end
of a short pipeline. Each step answers one question and is steered by
one or two arguments. Knowing the steps tells you which argument to
change when the map is not the one you wanted.
The example data are the package’s 24 invented towns and their
commuting flows. Every number below is read from
m$spec$meta, the record that each run keeps of itself.
1 · The problem: places and pairs
The input is a table of nodes and a table of flows. The first step
turns them into places and pairs: duplicate rows of a
pair are summed, a flow from a node to itself is set aside as internal
volume, and, on request, nodes are merged into larger places.
Two arguments change what a place is. group merges the
nodes that share a value in a column of your own; aggregate
merges neighbouring places automatically until a given number remains.
What happens inside a merged place becomes internal volume: it is still
counted, it is no longer drawn.
a<-flowmap(demo_nodes, demo_flows, boundary =demo_boundary, aggregate =8, style =list(title ="Eight places", lang ="en"))a$spec$meta$metrics[c("input_nodes", "places")]#> $input_nodes#> [1] 24#> #> $places#> [1] 8print(a)#> <milkflow> 8 places (from 24 nodes), 8 segments, 3 width classes#> Volume: 88,974 in 49 pairs (49 routed)#> in corridors 45.8%#> internal 54.2%#> filtered out 0.0%#> without a path 0.0%#> Lines: 3 categories on 8 segments, 15 crossings at nodes#> leisure 15.8% of the drawn volume#> study 23.2% of the drawn volume#> work 61.1% of the drawn volume#> Detour: mean 1.07, max 3.02
a
2 · The pairs that design the corridors
With n places there can be n² pairs, most of them
small. A subset, the design pairs, decides where the corridors
go. By default each place keeps its k = 3 largest pairs out
and in; filter chooses another rule (dominant flows, a
disparity backbone, the largest pairs, a share of the volume, a
threshold).
The other pairs are not thrown away. With
represent = "all" (the default) they are routed on the
corridors the design pairs produced; with
represent = "design" they are left out and counted as
filtered.
The corridors are edges of a sparse network over the places, the
skeleton. It starts as the Delaunay triangulation of the places
(every place linked to its natural neighbours) and is pruned: a segment
stays when the detours it saves are worth its ink. mu sets
that trade: larger values keep more segments.
segments<-function(mu){flowmap(demo_nodes, demo_flows, mu =mu)$spec$meta$metrics$segments}c(`mu = 1` =segments(1), `mu = 4` =segments(4), `mu = 16` =segments(16))#> mu = 1 mu = 4 mu = 16 #> 23 25 30
When the corridors are known in advance (roads, rail lines, a river),
give them as skeleton with
skeleton_source = "input" and this step is skipped.
4 · Routes
Every represented pair is sent along the skeleton, and the volumes
that share a segment add up. This is where the map stops being one line
per pair. The price is the detour: the length of a route over the
straight distance of its pair.
A mean near 1 says the corridors follow the flows closely; a large
maximum points at a pair that travels far around, usually one that did
not design the corridors. max_stretch bounds the detour of
the design pairs.
5 · The spec: widths and accounting
The routed network becomes the drawing specification. Two things are
decided here. The volume of each segment is put in a width
class, so that a width in the figure can be read against the
legend:
This is the part of the output to quote next to a figure: it says how
much of the data the picture shows.
6 · Lines
When the flows have a category, each corridor is split
into one line per category, and the lines are ordered along the
corridors so that they cross as little as possible, in the manner of a
transit map.
m$spec$legend$categories#> id label total#> 1 leisure leisure 12883#> 2 study study 20919#> 3 work work 53522m$spec$meta$metrics$line_crossings#> [1] 129
lines = "off" ignores the categories and draws each
corridor as one band.
7 · The schematic layout
On request (schematic = TRUE), the places are moved so
that every segment runs at a multiple of 45 degrees, keeping the order
of the segments around each place. The spec of the schematic drawing is
a copy of the geographic one with other positions: the volumes, the
classes and the accounting are the same.
When no valid layout is found, the run does not fail: the geographic
map stands and the reason is in s$schematic_error.
8 · The drawings
The last step paints the spec. The SVG is always produced; the
interactive page, the TikZ picture and the GeoJSON come from the same
scene, on request, so they agree with each other.