9.9 Sankey Diagrams

  • Building a Sankey diagram requires two data frames, one containing node names and the second containing the links between the nodes and the amount of the flow between them.

  • Let’s look at UK energy forecast data which contains energy production and consumption forecasts for the year 2050 to demonstrate.

  • Here we have 2 data frames: a ‘links’ data frame with 3 columns (from, to, value), and a ‘nodes’ data frame that gives the name of each node.

library(networkD3) # Load package
 
# Load energy projection data
URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
Energy <- jsonlite::fromJSON(URL)
head(Energy$links)
##   source target   value
## 1      0      1 124.729
## 2      1      2   0.597
## 3      1      3  26.862
## 4      1      4 280.322
## 5      1      5  81.144
## 6      6      2  35.000
head(Energy$nodes)
##                   name
## 1 Agricultural 'waste'
## 2       Bio-conversion
## 3               Liquid
## 4               Losses
## 5                Solid
## 6                  Gas