Skip to contents

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 eight steps of the pipeline: problem, design pairs, skeleton, routes, spec, lines, schematic, drawings. 

library(milkflow)
m <- flowmap(demo_nodes, demo_flows, boundary = demo_boundary)

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.

m$spec$meta$metrics[c("input_nodes", "places")]
#> $input_nodes
#> [1] 24
#> 
#> $places
#> [1] 24
m$spec$meta$accounting[c("total", "internal", "pairs")]
#> $total
#> [1] 88974
#> 
#> $internal
#> [1] 1650
#> 
#> $pairs
#> [1] 180

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] 8
print(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
Eight places 46% of the volume in 8 segments · 54% internal to the places leisure Lorkel → Salgor: 946 Salgor → Lorkel: 567 Total: 1,513 · 13% of the segment study Lorkel → Salgor: 1,443 Salgor → Lorkel: 974 Total: 2,417 · 20% of the segment work Lorkel → Salgor: 4,502 Salgor → Lorkel: 3,481 Total: 7,983 · 67% of the segment work Lorkel → Ostros: 5,029 Ostros → Lorkel: 3,468 Total: 8,497 · 58% of the segment study Lorkel → Ostros: 1,759 Ostros → Lorkel: 1,755 Total: 3,514 · 24% of the segment leisure Lorkel → Ostros: 1,818 Ostros → Lorkel: 781 Total: 2,599 · 18% of the segment work Marar → Salgor: 1,581 Salgor → Marar: 1,692 Total: 3,273 · 63% of the segment study Marar → Salgor: 637 Salgor → Marar: 453 Total: 1,090 · 21% of the segment leisure Marar → Salgor: 317 Salgor → Marar: 515 Total: 832 · 16% of the segment work Salgor → Ostpal: 2,120 Ostpal → Salgor: 1,861 Total: 3,981 · 60% of the segment study Salgor → Ostpal: 1,078 Ostpal → Salgor: 743 Total: 1,821 · 27% of the segment leisure Salgor → Ostpal: 362 Ostpal → Salgor: 460 Total: 822 · 12% of the segment leisure Marar → Belpal: 321 Belpal → Marar: 646 Total: 967 · 22% of the segment study Marar → Belpal: 325 Belpal → Marar: 545 Total: 870 · 20% of the segment work Marar → Belpal: 1,669 Belpal → Marar: 897 Total: 2,566 · 58% of the segment leisure Corbel → Enlor: 459 Enlor → Corbel: 126 Total: 585 · 14% of the segment study Corbel → Enlor: 493 Enlor → Corbel: 791 Total: 1,284 · 30% of the segment work Corbel → Enlor: 1,228 Enlor → Corbel: 1,132 Total: 2,360 · 56% of the segment leisure Corbel → Ostpal: 338 Ostpal → Corbel: 198 Total: 536 · 12% of the segment study Corbel → Ostpal: 674 Ostpal → Corbel: 688 Total: 1,362 · 31% of the segment work Corbel → Ostpal: 1,317 Ostpal → Corbel: 1,233 Total: 2,550 · 57% of the segment leisure Enlor → Ostpal: 347 Ostpal → Enlor: 173 Total: 520 · 13% of the segment study Enlor → Ostpal: 390 Ostpal → Enlor: 531 Total: 921 · 23% of the segment work Enlor → Ostpal: 1,327 Ostpal → Enlor: 1,297 Total: 2,624 · 65% of the segment Marar Origin and destination (drawn): 7,214 Internal: 21,890 Groups 8 nodes Belpal Origin and destination (drawn): 4,403 Internal: 300 Lorkel Origin and destination (drawn): 24,275 Internal: 11,527 Groups 3 nodes Salgor Origin and destination (drawn): 9,290 Internal: 519 Groups 2 nodes Ostros Origin and destination (drawn): 14,610 Internal: 2,899 Groups 3 nodes Corbel Origin and destination (drawn): 8,677 Internal: 8,657 Groups 3 nodes Enlor Origin and destination (drawn): 8,294 Internal: 1,971 Groups 2 nodes Ostpal Origin and destination (drawn): 4,703 Internal: 478 Groups 2 nodes LorkelMararOstrosCorbelEnlorSalgorOstpalBelpalEight placesColour = categoryleisure 6.4kstudy 9.4kwork 25korigin and destinationinternal flow46% of the volume in 8 segments · 54% internal to the places

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).

m$spec$meta$params[c("k", "represent")]
#> $k
#> [1] 3
#> 
#> $represent
#> [1] "all"

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.

d <- flowmap(demo_nodes, demo_flows, filter = "dominant", represent = "design")
unlist(d$spec$meta$accounting[c("routed", "internal", "filtered")])
#>   routed internal filtered 
#>    24666     1650    62658

3 · The skeleton

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.

m$spec$meta$metrics[c("stretch_mean", "stretch_max")]
#> $stretch_mean
#> [1] 1.104717
#> 
#> $stretch_max
#> [1] 5.094018

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:

m$spec$legend$width_classes
#>   class  min   max
#> 1     0  282   500
#> 2     1  500  1000
#> 3     2 1000  2000
#> 4     3 2000  5000
#> 5     4 5000 17243

And the accounting is closed: every unit of volume in the input is in a corridor, internal to a place, filtered out, or without a path.

acc <- m$spec$meta$accounting
with(acc, total == routed + internal + filtered + unroutable)
#> [1] TRUE

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 53522
m$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.

s <- flowmap(demo_nodes, demo_flows, schematic = TRUE, style = list(lang = "en"))
identical(s$spec$meta$accounting, s$schematic_spec$meta$accounting)
#> [1] TRUE
htmltools::HTML(s$schematic_svg)
Flow map 98% of the volume in 25 segments · 2% internal to the placeswork Marar → Quinmar: 3,341 Quinmar → Marar: 2,645 Total: 5,986 · 62% of the segmentstudy Marar → Quinmar: 1,603 Quinmar → Marar: 681 Total: 2,284 · 24% of the segmentleisure Marar → Quinmar: 613 Quinmar → Marar: 699 Total: 1,312 · 14% of the segmentleisure Marar → Falir: 296 Falir → Marar: 630 Total: 926 · 18% of the segmentstudy Marar → Falir: 467 Falir → Marar: 627 Total: 1,094 · 22% of the segmentwork Marar → Falir: 2,042 Falir → Marar: 953 Total: 2,995 · 60% of the segmentwork Marar → Arjun: 2,665 Arjun → Marar: 1,531 Total: 4,196 · 56% of the segmentstudy Marar → Arjun: 951 Arjun → Marar: 971 Total: 1,922 · 26% of the segmentleisure Marar → Arjun: 734 Arjun → Marar: 673 Total: 1,407 · 19% of the segmentleisure Marar → Kellor: 376 Kellor → Marar: 393 Total: 769 · 8% of the segmentstudy Marar → Kellor: 2,172 Kellor → Marar: 719 Total: 2,891 · 31% of the segmentwork Marar → Kellor: 3,230 Kellor → Marar: 2,543 Total: 5,773 · 61% of the segmentwork Quinmar → Halbel: 2,436 Halbel → Quinmar: 2,006 Total: 4,442 · 66% of the segmentstudy Quinmar → Halbel: 904 Halbel → Quinmar: 502 Total: 1,406 · 21% of the segmentleisure Quinmar → Halbel: 378 Halbel → Quinmar: 539 Total: 917 · 14% of the segmentwork Halbel → Palhal: 2,135 Palhal → Halbel: 1,996 Total: 4,131 · 66% of the segmentstudy Halbel → Palhal: 744 Palhal → Halbel: 502 Total: 1,246 · 20% of the segmentleisure Halbel → Palhal: 368 Palhal → Halbel: 497 Total: 865 · 14% of the segmentwork Palhal → Salgor: 1,581 Salgor → Palhal: 1,692 Total: 3,273 · 63% of the segmentstudy Palhal → Salgor: 637 Salgor → Palhal: 453 Total: 1,090 · 21% of the segmentleisure Palhal → Salgor: 317 Salgor → Palhal: 515 Total: 832 · 16% of the segmentwork Lorkel → Salgor: 4,502 Salgor → Lorkel: 3,481 Total: 7,983 · 67% of the segmentstudy Lorkel → Salgor: 1,443 Salgor → Lorkel: 974 Total: 2,417 · 20% of the segmentleisure Lorkel → Salgor: 946 Salgor → Lorkel: 567 Total: 1,513 · 13% of the segmentleisure Lorkel → Artor: 1,125 Artor → Lorkel: 278 Total: 1,403 · 24% of the segmentstudy Lorkel → Artor: 1,062 Artor → Lorkel: 446 Total: 1,508 · 26% of the segmentwork Lorkel → Artor: 1,279 Artor → Lorkel: 1,587 Total: 2,866 · 50% of the segmentleisure Lorkel → Quincor: 2,403 Quincor → Lorkel: 903 Total: 3,306 · 19% of the segmentstudy Lorkel → Quincor: 1,476 Quincor → Lorkel: 2,511 Total: 3,987 · 23% of the segmentwork Lorkel → Quincor: 6,860 Quincor → Lorkel: 3,090 Total: 9,950 · 58% of the segmentleisure Palquin → Salgor: 713 Salgor → Palquin: 383 Total: 1,096 · 13% of the segmentstudy Palquin → Salgor: 783 Salgor → Palquin: 1,204 Total: 1,987 · 24% of the segmentwork Palquin → Salgor: 2,352 Salgor → Palquin: 2,955 Total: 5,307 · 63% of the segmentwork Palquin → Dalen: 2,120 Dalen → Palquin: 1,861 Total: 3,981 · 60% of the segmentstudy Palquin → Dalen: 1,078 Dalen → Palquin: 743 Total: 1,821 · 27% of the segmentleisure Palquin → Dalen: 362 Dalen → Palquin: 460 Total: 822 · 12% of the segmentleisure Dalmar → Ostros: 1,292 Ostros → Dalmar: 372 Total: 1,664 · 18% of the segmentstudy Dalmar → Ostros: 786 Ostros → Dalmar: 1,481 Total: 2,267 · 25% of the segmentwork Dalmar → Ostros: 2,729 Ostros → Dalmar: 2,400 Total: 5,129 · 57% of the segmentwork Dalmar → Quincor: 2,275 Quincor → Dalmar: 3,112 Total: 5,387 · 53% of the segmentstudy Dalmar → Quincor: 1,425 Quincor → Dalmar: 1,153 Total: 2,578 · 25% of the segmentleisure Dalmar → Quincor: 526 Quincor → Dalmar: 1,626 Total: 2,152 · 21% of the segmentleisure Corbel → Dalen: 1,336 Dalen → Corbel: 519 Total: 1,855 · 12% of the segmentstudy Corbel → Dalen: 1,712 Dalen → Corbel: 1,729 Total: 3,441 · 22% of the segmentwork Corbel → Dalen: 5,397 Dalen → Corbel: 5,137 Total: 10,534 · 67% of the segmentleisure Corbel → Enlor: 586 Enlor → Corbel: 388 Total: 974 · 14% of the segmentstudy Corbel → Enlor: 864 Enlor → Corbel: 1,082 Total: 1,946 · 28% of the segmentwork Corbel → Enlor: 2,148 Enlor → Corbel: 1,892 Total: 4,040 · 58% of the segmentwork Belpal → Falir: 897 Falir → Belpal: 1,669 Total: 2,566 · 58% of the segmentstudy Belpal → Falir: 545 Falir → Belpal: 325 Total: 870 · 20% of the segmentleisure Belpal → Falir: 646 Falir → Belpal: 321 Total: 967 · 22% of the segmentwork Lorkel → Junmar: 1,917 Junmar → Lorkel: 1,193 Total: 3,110 · 69% of the segmentstudy Lorkel → Junmar: 606 Junmar → Lorkel: 330 Total: 936 · 21% of the segmentleisure Lorkel → Junmar: 192 Junmar → Lorkel: 255 Total: 447 · 10% of the segmentwork Corbel → Ostpal: 1,093 Ostpal → Corbel: 910 Total: 2,003 · 59% of the segmentstudy Corbel → Ostpal: 528 Ostpal → Corbel: 287 Total: 815 · 24% of the segmentleisure Corbel → Ostpal: 364 Ostpal → Corbel: 187 Total: 551 · 16% of the segmentleisure Enlor → Norcor: 429 Norcor → Enlor: 242 Total: 671 · 14% of the segmentstudy Enlor → Norcor: 373 Norcor → Enlor: 508 Total: 881 · 18% of the segmentwork Enlor → Norcor: 1,642 Norcor → Enlor: 1,582 Total: 3,224 · 68% of the segmentwork Junmar → Ostros: 298 Ostros → Junmar: 374 Total: 672 · 51% of the segmentstudy Junmar → Ostros: 174 Ostros → Junmar: 212 Total: 386 · 29% of the segmentleisure Junmar → Ostros: 178 Ostros → Junmar: 82 Total: 260 · 20% of the segmentwork Ostpal → Tordal: 728 Tordal → Ostpal: 555 Total: 1,283 · 67% of the segmentstudy Ostpal → Tordal: 203 Tordal → Ostpal: 218 Total: 421 · 22% of the segmentleisure Ostpal → Tordal: 142 Tordal → Ostpal: 74 Total: 216 · 11% of the segmentwork Tordal → Norcor: 377 Norcor → Tordal: 567 Total: 944 · 71% of the segmentstudy Tordal → Norcor: 160 Norcor → Tordal: 99 Total: 259 · 19% of the segmentleisure Tordal → Norcor: 46 Norcor → Tordal: 85 Total: 131 · 10% of the segmentleisure Marar → Torost: 17 Torost → Marar: 4 Total: 21 · 6% of the segmentstudy Marar → Torost: 115 Torost → Marar: 46 Total: 161 · 47% of the segmentwork Marar → Torost: 102 Torost → Marar: 57 Total: 159 · 47% of the segmentleisure Dalen → Salpal: 74 Salpal → Dalen: 7 Total: 81 · 29% of the segmentstudy Dalen → Salpal: 13 Salpal → Dalen: 31 Total: 44 · 16% of the segmentwork Dalen → Salpal: 106 Salpal → Dalen: 51 Total: 157 · 56% of the segmentMarar Origin and destination (drawn): 24,178 Internal: 1,200Belpal Origin and destination (drawn): 4,403 Internal: 300Quinmar Origin and destination (drawn): 2,817 Internal: 150Falir Origin and destination (drawn): 1,056Arjun Origin and destination (drawn): 7,525Halbel Origin and destination (drawn): 1,109Kellor Origin and destination (drawn): 9,433Torost Origin and destination (drawn): 341Palhal Origin and destination (drawn): 1,835Lorkel Origin and destination (drawn): 32,960Palquin Origin and destination (drawn): 4,962Junmar Origin and destination (drawn): 5,811Salgor Origin and destination (drawn): 5,366Dalmar Origin and destination (drawn): 4,821Artor Origin and destination (drawn): 5,777Ostros Origin and destination (drawn): 9,776Quincor Origin and destination (drawn): 8,592Corbel Origin and destination (drawn): 15,837Dalen Origin and destination (drawn): 9,872Enlor Origin and destination (drawn): 6,930Ostpal Origin and destination (drawn): 3,199Salpal Origin and destination (drawn): 282Tordal Origin and destination (drawn): 2,460Norcor Origin and destination (drawn): 5,306LorkelMararCorbelDalenOstrosKellorQuincorArjunEnlorJunmarArtorSalgorNorcorPalquinDalmarBelpalOstpalQuinmarTordalPalhalHalbelFalirTorostSalpalColour = categoryleisure 13kstudy 21kwork 54korigin and destinationinternal flow98% of the volume in 25 segments · 2% internal to the places

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.

full <- flowmap(demo_nodes, demo_flows, html = TRUE, tikz = TRUE, geojson = TRUE)
vapply(full[c("svg", "html", "tikz", "geojson")], nchar, numeric(1))
#>     svg    html    tikz geojson 
#>  118873  186267  148138  275982

style belongs to this step alone: a theme, a palette or a title changes how the figure looks, never what was computed in steps 1 to 7.

dark <- flowmap(demo_nodes, demo_flows, style = list(theme = "dark"))
identical(dark$spec$segments, m$spec$segments)
#> [1] TRUE

Which argument for which complaint

The map… Step Try
has too many places 1 group, aggregate
is designed by flows that do not matter 2 k, filter
has too many, or too few, corridors 3 mu
sends a pair far around 3, 4 larger mu, max_stretch
hides too much of the volume 2, 5 represent = "all"; read the accounting
has too many colours 6 max_categories, lines = "off"
is hard to read where places crowd 7 schematic = TRUE
has the wrong look 8 style