Creating Custom Sankey Diagrams Using R

Displayr
4 min readMar 29, 2017

--

I have previously shown how Sankey diagrams can easily be used to visualize response patterns in surveys and to display decision trees. Following on from these posts, I will now be getting a bit more technical, and describe how to create custom Sankey diagrams in R. I will start by explaining the basics of Sankey diagrams, and then provide examples of automatically created and manually controlled layouts.

Create a Sankey Diagram in R with Displayr!

The elements of a Sankey diagram

A Sankey diagram consists of three sets of elements: the nodes, the links, and the instructions which determine their positions.

To begin with, there are the nodes. In the diagram above, a node is wherever the lines change direction. However, in the example below, boxes represent the four nodes.

The second element of a Sankey diagram is the links (or edges), that connect the nodes together. These links have a value associated with them, which is represented by the thickness of the link. In the example below, the first link that connects Node A with Node B, is half the width of the second link that connects A with C. Furthermore, the link from B to D is bigger again, and the largest link is from C to D.

Lastly, instructions specify where the nodes should appear in relation to each other. There are two strategies for positioning the nodes. One is to give specific coordinates. This is what is illustrated in the example above: the position of the nodes reflects places in France, Russia, and Poland. Alternatively, the nodes can be placed automatically using an algorithm (most commonly, a variant of the force-directed graph layout algorithm is used).

https://embedded.azureedge.net/75668/196641/4f6b26ca-b8fa-404c-b271-7004876f8687.html?v=34429236311807

Using R

I’ve created the example above using R from within Displayr.

It is created using the following R code:

Some aspects of this code to note:

  • Line 1 is loading a package (networkD3).
  • Lines 2 to 6 are creating a data frame that contains a single variable, called name. It contains four nodes, which I have creatively named A, B, C, and D.
  • Lines 7 to 11 specify the links. Line 8, for example, shows that the link from node 0 (i.e., A) to node 1 (i.e., B), has a value of 10.
  • The final rows use the sankeyNetwork function.

If you want to adapt this example, you only need to modify the nodes (lines 3 to 6 in this example), and the links (lines 8 to 11). Additionally, you can play around with, and modify, the example live in Displayr by clicking here. Clicking on any of the examples in Displayr will show you the R code. Modify the code first, and then run it by pressing Calculate.

Create a Sankey Diagram in R with Displayr!

Sankey diagrams using automated layout

You can also use Sankey diagrams to create conversion funnels, illustrated in the next example. Following this, another one shows data on load energy projections. My first post on Sankey diagrams also features this latter example.

https://embedded.azureedge.net/75668/196641/77aa82ad-8a24-4301-ab4a-970bc3e2986c.html?v=34429564726730

https://embedded.azureedge.net/75668/196641/07fc5154-de31-474e-82b7-01adfdb072b5.html?v=34429-1193108925

Sankey diagrams with manual layout

In Minard’s classic Sankey diagram of the march of Napoleon to Moscow and back, the thickness of the line shows the size of Napoleon’s army. The nodes are where the line changes direction. Automatic placement determined the position of the nodes in the previous examples, whereas here, the nodes represent the locations of places in Europe.

1280px-minard

Below you can see Minard’s visualization reproduced in R. The code used to create this example has basically the same structure as used in the previous examples, except that x and y coordinates are provided for the nodes, and the color is explicitly set.

https://embedded.azureedge.net/75668/196641/2371758b-9224-4635-94d9-4a9f5596dce9.html?v=344291020503949

TRY IT OUT
You can investigate this data set further or even work on your own data in Displayr.

Acknowledgements

The final example uses January Weiner’ s riverplot package for R. All the other examples use a modified version of networkD3, created by Kenton Russell (timelyportfolio/networkD3@feature/responsive). networkD3 is an HTMLwidget version of Mike Bostock’s D3 Sankey diagram code, which is inspired by Tom Counsell’s Sankey library. The load energy flow example is from networkD3, which is a reworking of a Sankey library example, using data from the UK’s Department of Energy & Climate Change.

Originally published at Displayr.

--

--

Displayr

Displayr is a data science, visualization, and reporting platform for everyone.