Mapping Flows in R

[This article was first published on » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Last year I published the above graphic, which then got converted into the below for the book London: The Information Capital. I have had many requests for the code I used to create the plot so here it is!

The data shown is the Office for National Statistics flow data. See here for the latest version. The file I used for the above can be downloaded here (it is >109 mb uncompressed so you need a decent computer to load/plot it all at once in R). You will also need this file of area (MSOA) codes and their co-ordinates. The code used is pasted below with comments above each segment. Good luck!




Load the flow data required – origin and destination points are needed. See above for where you can get the table used here.

We only need the first 3 columns of the above


The UK Census file above didn’t have coordinates just area codes. Here is a lookup that provides those. See above for download.


Lots of joining to get the xy coordinates joined to the origin and then the destination points.


Now for plotting with ggplot2.This first step removes the axes in the resulting plot.


Let’s build the plot. First we specify the dataframe we need, with a filter excluding flows of <10

ggplot(dest.xy[which(dest.xy$trips>10),], aes(oX, oY))+

The next line tells ggplot that we wish to plot line segments. The “alpha=” is line transparency and used below

geom_segment(aes(x=oX, y=oY,xend=dX, yend=dY, alpha=trips), col="white")+

Here is the magic bit that sets line transparency – essential to make the plot readable

scale_alpha_continuous(range = c(0.03, 0.3))+

Set black background, remove axes and fix aspect ratio

theme(panel.background = element_rect(fill='black',colour='black'))+quiet+coord_equal()

We used ggave() to export from R and then we imported the pdf into Illustrator to produce the final graphic for print…


To leave a comment for the author, please follow the link and comment on their blog: » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)