A Sankey Diagram is a flow plot designed to show the change in magnitude of some quantity as it flows between states. The states (often on the x-axis) can be over space, time, or any other discreet index. There are different libraries which can be used to create Sankey diagrams in R, the one chosen below, networkD3, creates an interactive plot which displays information about the flows when the mouse is hovered over them.
The most difficult part of this Sankey Diagram was structuring the data. Each node is allocated a number according to the index position in the ‘nodes’ data frame. The first Node (CUBD15_CAULFIELD_SOUTH) is Node 0, the next (CUDBEM1) is Node 1 etc. The links matrix provides ‘From Node’, ‘To Node’ and ‘Weight’ in each row, where the Nodes are referenced by their Node number.
nodes = data.frame("name" = c("CUBD15_CAULFIELD_SOUTH","CUBDEM1","GMWE15_GLENHUNTLY_WEST","CUBD15_CAULFIELD_SOUTH",
"OMND15_ORMOND","BTED15_BRIGHTON_EAST","GMWEEM2","GMWEEM3","CUBDEM2","CUBDEM3",
"OMNDEM3","BTEDEM3","CUBDEM2","GVAD15_GARDENVALE","HUID15_HURLINGHAM_PARK",
"GVADEM1","HUIDEM3","OMNDEM3","GVADEM3","GVADEM2"))
links = as.data.frame(matrix(c(
0,1,6861,1,2,5284,1,3,1111,1,4,349,1,5,117,2,6,3368,2,7,1916,3,8,590,3,9,521,4,10,349,5,11,117,0,8,11979,8,13,7616,8,14,1283,
8,4,1213,8,3,1474,8,5,393,13,15,6866,14,16,1283,4,10,1213,3,9,904,3,1,570,13,18,440,5,11,393,13,19,310),
byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize= 12, nodeWidth = 30)