Sankey Network
A Sankey network is a type of visualization that shows the flows and movement of objects between nodes of different categories or groups. Sankey is useful for understanding the transition, distribution, or transformation of quantities or values within a system. It also consists of nodes and links. It is a complex form of network in the networkD3 package because of the massive network components. The link in the Sankey network is of different sizes according to the data flow through the link. The width of the links represents the magnitude or volume of the flow.
Syntax:
sankeyNetwork(Links, Nodes, Source, Target, Value, NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = “”, colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory20);”), fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL, height = NULL, width = NULL, iterations = 32, sinksRight = TRUE)
Arguments:
- Links – A data frame object with the links between the nodes. It should include the
Source
andTarget
for each link. An optionalValue
variable can be included to specify how close the nodes are to one another. - Nodes – A data frame containing the node id and properties of the nodes. If no ID is specified then the nodes must be in the same order as the Source variable column in the
Links
data frame. - Source – A character string naming the network source variable in the
Links
data frame. - Target – A character string naming the network target variable in the
Links
data frame. - Value – A character string naming the variable in the
Links
data frame for how wide the links are. - NodeID – A character string specifying the node IDs in the
Nodes
data frame. - NodeGroup – A character string specifying the node groups in the
Nodes.
- LinkGroup – A character string specifying the groups in the
Links
. Used to color the links in the network. - units – A character string describing physical units for Value.
- color Scale – A character string specifying the categorical color scale for the nodes.
- font Size – The numeric font size in pixels for the node text labels.
- font Family – Font family for the node text labels.
- nodeWidth – The numeric width of each node.
- nodePadding – The numeric essentially influences the width height.
- margin – An integer or a named
list
/vector
of integers for the plot margins. - height – The numeric height for the network graph’s frame area in pixels.
- width – The numeric width for the network graph’s frame area in pixels.
- iterations – Numeric. Number of iterations in the diagram layout for computation of the depth (y-position) of each node
- sinks Right – Logical(
TRUE-
last nodes are moved to the right border of the plot).
Consider there is a number of industries, transportation, aviation, living areas, etc., that need resources like gas, oil, electricity, etc., for their existence and surveillance. Then the industries, transportation, gas, oil, etc., are represented as nodes of the network, and the connections among them can be represented as links(edges) of the network, If we require the amount of usage of the resources used by industries then we can use Sankey network.
library(networkD3)
# Load energy projection data
URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)
# Colour Nodes
sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name',
units = 'TWh', fontSize = 12, nodeWidth = 30)
# Colour links
energy$links$energy_type <- sub(' .*', '',
energy$nodes[energy$links$source + 1, 'name'])
sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name',
LinkGroup = 'energy_type', NodeGroup = NULL)
Output:
“jsonlite::fromJSON” is a function in “jsonlite” package in R that is used to convert JSON data in R objects that represents the JSON data. In the above program, jsonlite::fromJSON(URL) is a function in ‘jsonlite’ package that converts the JSON formatted data into R objects and stores it in the ‘energy’ variable.
- Load the library “networkD3” using load(“library”).
- Create a variable ‘URL’ and concatenate both the URLs.
- Create a variable ‘energy’ and store a json formatted data.
- Create a Sankey network using sankeyNetwork() and assign links, nodes, sources, targets, values, and NodeID.
Again create a Sankey network but this time adds LinkGroup = ‘energy_type’ to get a colorful link in the network.
networkD3 package in R
Data-driven document Network is an R package for creating network graphs which are used for 3-dimensional visualizations of data as network graphs. In R Programming Language networkD3 plots are created using the networkD3 package.
Table of Content
- Simple Network
- Force Network
- Sankey Network
- Radial Network:
- Dendro network
- Chord Network
To use a package in R programming we have to install the package first. For installing the R package in R studio use the command install.packages(“name”). Follow the following steps to get the packages installed on your system.
install.packages('networkD3')
Contact Us