Learn R Programming

networkD3 (version 0.4)

sankeyNetwork: Create a D3 JavaScript Sankey diagram

Description

Create a D3 JavaScript Sankey diagram

Usage

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 have 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. Currently only grouping variable is allowed.

Source

character string naming the network source variable in the Links data frame.

Target

character string naming the network target variable in the Links data frame.

Value

character string naming the variable in the Links data frame for how far away the nodes are from one another.

NodeID

character string specifying the node IDs in the Nodes. data frame. Must be 0-indexed.

NodeGroup

character string specifying the node groups in the Nodes. Used to color the nodes in the network.

LinkGroup

character string specifying the groups in the Links. Used to color the links in the network.

units

character string describing physical units (if any) for Value

colourScale

character string specifying the categorical colour scale for the nodes. See https://github.com/d3/d3/blob/master/API.md#ordinal-scales.

fontSize

numeric font size in pixels for the node text labels.

fontFamily

font family for the node text labels.

nodeWidth

numeric width of each node.

nodePadding

numeric essentially influences the width height.

margin

an integer or a named list/vector of integers for the plot margins. If using a named list/vector, the positions top, right, bottom, left are valid. If a single integer is provided, then the value will be assigned to the right margin. Set the margin appropriately to accomodate long text labels.

height

numeric height for the network graph's frame area in pixels.

width

numeric width for the network graph's frame area in pixels.

iterations

numeric. Number of iterations in the diagramm layout for computation of the depth (y-position) of each node. Note: this runs in the browser on the client so don't push it too high.

sinksRight

boolean. If TRUE, the last nodes are moved to the right border of the plot.

See Also

JS

Examples

Run this code
# NOT RUN {
# Recreate Bostock Sankey diagram: http://bost.ocks.org/mike/sankey/
# Load energy projection data
URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
              'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)

# Plot
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)

# }

Run the code above in your browser using DataLab