We can explore flow data with the stplanr package:

devtools::install_github("robinlovelace/stplanr")
library(stplanr)
## Loading required package: sp

This is what flow data looks like:

data("flow")
head(flow[1:3])
##        Area.of.residence Area.of.workplace All
## 920573         E02002361         E02002361 109
## 920575         E02002361         E02002363  38
## 920578         E02002361         E02002367  10
## 920582         E02002361         E02002371  44
## 920587         E02002361         E02002377  34
## 920591         E02002361         E02002382   7

This is what centroid data looks like

data("cents")
head(coordinates(cents))
##      coords.x1 coords.x2
## [1,] -1.546463  53.80952
## [2,] -1.511861  53.81161
## [3,] -1.524205  53.80410
## [4,] -1.550807  53.82442
## [5,] -1.535617  53.82847
## [6,] -1.516734  53.82887
plot(cents)

You need to allocated the flows to the centroids to visualise the flows:

l <- gFlow2line(flow = flow, zones = cents)
plot(l)

Now we can allocate the flows to the travel network:

library(leaflet)
bbox(l)
##         min       max
## x -1.550807 -1.511861
## y 53.804098 53.828874
cyclep <- gLines2CyclePath(l, plan = "fastest")
## [1] "10 % out of 49 distances calculated"
## [1] "The first 10 routes have been saved, be patient. I'll say when 10% have been loaded."
## [1] "20 % out of 49 distances calculated"
## [1] "31 % out of 49 distances calculated"
## [1] "41 % out of 49 distances calculated"
## [1] "51 % out of 49 distances calculated"
## [1] "61 % out of 49 distances calculated"
## [1] "71 % out of 49 distances calculated"
## [1] "82 % out of 49 distances calculated"
## [1] "92 % out of 49 distances calculated"
leaflet() %>% addTiles() %>% 
  addPolylines(data = l) %>% 
  addPolylines(data = cyclep, color = "red")