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 and Target for each link. An optional Value 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.

R
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:

NetworkD3

“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')

Similar Reads

Simple Network

A simple network is a basic form of a Forced-directed network, we can use Force network to create a Simple network by providing appropriate data and customization options. For a simple form of a Force-directed network, we can use a simple network (). It works based on a “node-link” diagram and is connected by edges. It provides us with amazing animation with an intuitive way of observing data and also allows us to tweak the networks with cursors....

Force Network

A Force network is also known as node-link diagram though it is useful for visualizing networks with nodes and edges which are connected by forces. Force network provides wonderful animations for understanding the network’s data element and also allows us to tweak the data in a repulsive and attractive manner....

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....

Radial Network

A radial network also known as the is Reingold-Tilford Tree network, is a type of network visualization where nodes are arranged in a circular or radial layout. In a , radial network the root node is the center present at the centre of the network and the parent node present at is outside of the circular graph. Radial network visualization is particularly useful when we want to display hierarchical relationships between nodes, such as organizational structures, family trees, or classification hierarchies....

Dendro network

The Dendro network is a network visualization that uses a hierarchical type of visualization of data. It is particularly useful when visualizing `hierarchical clustering or tree structures. It contains the root node where the tree graph starts and the parent node where the tree graph ends. It can help for understanding complex information through network visualization....

Chord Network

A Chord network is also known as a circular network. It is a type of network visualization that represents relationships and connections between entities or categories. It is particularly useful for showing the interactions and associations between different groups or entities. In a chord network, entities or groups are represented as arcs on a circle, and the connections or relationships are displayed as chords connecting the arcs. The width of the chords can be used to represent the strength, magnitude, or frequency of the connections....