Skip to contents

Simplified flow maps in the style of transit maps, for R.

Origin-destination flows (commuting, migration, trade) drawn one line per pair become unreadable fast. milkflow aggregates, filters and routes the flows through a few shared corridors, draws them with classed widths and reports how much of the volume is drawn, internal or left out. An optional schematic layout redraws the corridors on an octilinear grid; flows with a category become parallel coloured lines along the corridors (the lines mode); the figures come as SVG, as an interactive HTML page, for LaTeX as TikZ and, for web maps and GIS, as GeoJSON.

The computation is done by the package’s Rust core (the crate milkflow, crates/milk in the monorepo); the R package is a thin layer over its JSON door, so the same input gives the same figure, byte for byte, in every language that binds it.

A real example

Journeys by public transport between the 15 municipalities of the Recife metropolitan region, in Brazil, on a working day: one line per part of the region where the journey starts.

Flow map of the Recife metropolitan region: four coloured lines share the corridors that converge on Recife

library(milkflow)
rmr <- jsonlite::fromJSON(
  "https://strategicprojects.github.io/milkflow/data/rmr_municipios_linhas.json"
)
m <- flowmap(rmr$nodes, rmr$flows, boundary = rmr$boundary,
             crs = rmr$crs, units = rmr$units,
             style = list(title = "Public transport in the Recife metropolitan region",
                          lang = "en"))
write_svg(m, "rmr.svg")

The same figure over a base map, with zoom: An interactive map with MapLibre.

How it works

A figure is the end of a short pipeline. Each step answers one question and is steered by one or two arguments of flowmap():

The eight steps of the pipeline: problem, design pairs, skeleton, routes, spec, lines, schematic, drawings

The pipeline runs in a Rust core that every language shares. The R package writes the input and the options as JSON, calls the core and reads the result back, so the same input gives the same bytes in R, in Python, in the browser and at the command line:

The R package, the Python package and the JavaScript package call the JSON door of the Rust core; the command line calls the pipeline directly

The vignettes How a flow map is made and How the package is built go through both.

Installation

Building from source needs Rust (rustc and cargo) 1.85 or later: https://rust-lang.org/tools/install/.

# from a source tarball
install.packages("milkflow_0.1.0.tar.gz", repos = NULL, type = "source")

Example

library(milkflow)
m <- flowmap(demo_nodes, demo_flows, boundary = demo_boundary,
             style = list(title = "Daily commuting"))
m
#> <milkflow> 24 places (from 24 nodes), 25 segments, 5 width classes
#> Volume: 88,974 in 180 pairs (180 routed)
#>   in corridors    98.1%
#>   internal         1.9%
#>   ...
#> Lines: 3 categories on 25 segments, 129 crossings at nodes
#>   leisure  14.8% of the drawn volume
#>   study    24.0% of the drawn volume
#>   work     61.3% of the drawn volume
write_svg(m, "commuting.svg")
plot(m)          # RStudio Viewer or browser

m$spec$lines     # one row per line (category) of each segment

# Corridors designed by the dominant flows, and an interactive page in English.
n <- flowmap(demo_nodes, demo_flows, filter = "dominant", html = TRUE,
             style = list(lang = "en"))
write_html(n, "commuting.html")

# Another look, and a TikZ picture for a paper.
d <- flowmap(demo_nodes, demo_flows, lines = "off", tikz = TRUE,
             style = list(theme = "print", corners = "smooth"))
write_tikz(d, "commuting.tex")   # \input{commuting.tex}, with \usepackage{tikz}

# The drawn figure as GeoJSON, for a web map or a GIS.
g <- flowmap(demo_nodes, demo_flows, geojson = TRUE)
write_geojson(g, "commuting.geojson")

The drawing options (style) are listed in ?flowmap, section “Style”, and the filters in the section “Filters”. The SVG figure, the page and the TikZ picture (drawn from the same scene as the SVG) draw one line per category (style = list(lines = "off") draws the corridors as one band). The lines are also in the specification ($spec$lines). geojson = TRUE gives the drawn figure ($geojson, $schematic_geojson) as GeoJSON — each paint operation in paint order, in the input’s coordinates (reproject before using it as RFC 7946 lon/lat); places[].uses tells which segments/categories each place’s flows use.

group aggregates the places by a column of nodes; one table can hold several nested levels (group = "district", then group = "region"), and the accounting closes at each. With corridors of your own (skeleton_source = "input"), the nodes of the network without flows are drawn as junctions.

Columns can have other names (id, x, y, origin, destination, value, category, label, weight arguments), and nodes can be an sf object of points in a projected CRS.

Development

The Rust core lives in the monorepo (crates/milk); it is not copied into this directory. Inside the monorepo, R CMD INSTALL r/milkflow builds against ../../crates/milk (or MILKFLOW_MILK_DIR) and fetches the other crates with cargo, unless src/rust/vendor.tar.xz exists: then the copy of the core vendored in it is built, so re-run tools/vendor.R (or delete the file) after a change to the core. To make a self-contained source package (offline build, as CRAN requires):

cd r/milkflow
Rscript tools/vendor.R        # writes src/rust/vendor.tar.xz, inst/AUTHORS, LICENSE.note
cd .. && R CMD build milkflow && R CMD check --as-cran milkflow_0.1.0.tar.gz

tools/reference is a small Rust program that writes the native outputs used by the byte-for-byte tests (tests/testthat/ref); after a change to the core or to the example data:

Rscript data-raw/demo.R              # example data (deterministic)
Rscript tools/reference/inputs.R     # inputs of the cases built from it
cargo run --manifest-path tools/reference/Cargo.toml -- tests/testthat/ref

Citing

citation("milkflow") gives the reference. Each release of milkflow is archived at Zenodo, and the DOI 10.5281/zenodo.24779953 always points to the latest version.

Licence

MIT OR Apache-2.0. The bundled Rust crates keep their own licences, listed in LICENSE.note and inst/AUTHORS.