Sankey diagram

Used to display flows from one set of values to another – several nodes are represented by rectangles or text and their links are represented by arrows or arcs having widths proportional to the flow rate

Previous Next

The chart

The code

library(networkD3)
library(dplyr)

# Load energy projection data
links <- read.csv("sankey_sample - Final_notext.csv")


# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(
  name=c(as.character(links$source),
         as.character(links$target)) %>% unique()
)

# With networkD3, connection must be provided using id, not using real name
# like in the links dataframe.. So we need to reformat it.
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1

# Make the Network
p <- sankeyNetwork(Links = links, Nodes = nodes,
                   Source = "IDsource", Target = "IDtarget",
                   Value = "value", NodeID = "name", width=1150, height=550,
                   sinksRight=FALSE, fontSize = 14,
                   colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);"))
p

# save the widget
library(htmlwidgets)
saveWidget(p, "/Users/sidravi/temp/sankeyBasic1.html")

Other details

This chart was created by Neha Verma and Raghav Adlakha.