sankeyNetwork: Create a D3 JavaScript Sankey diagram

Description Usage Arguments Source See Also Examples

Description

Create a D3 JavaScript Sankey diagram

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
sankeyNetwork(Links, Nodes, Source, Target, Value, NodeID, NodeGroup = NodeID,
  LinkGroup = NULL, NodePosX = NULL, NodeValue = NULL, NodeColor = NULL,
  NodeFontColor = NULL, NodeFontSize = NULL, units = "",
  colourScale = JS("d3.scaleOrdinal().range(d3.schemeCategory20)"),
  fontSize = 7, fontFamily = NULL, fontColor = NULL, nodeWidth = 15,
  nodePadding = 10, nodeStrokeWidth = 1, nodeCornerRadius = 0,
  margin = NULL, title = NULL, numberFormat = ",.5g",
  orderByPath = FALSE, highlightChildLinks = FALSE,
  doubleclickTogglesChildren = FALSE, xAxisDomain = NULL, dragX = FALSE,
  dragY = FALSE, height = NULL, width = NULL, iterations = 32,
  zoom = FALSE, align = "justify", showNodeValues = TRUE,
  linkType = "bezier", curvature = 0.5, linkColor = "#A0A0A0",
  nodeLabelMargin = 2, linkOpacity = 0.5, linkGradient = FALSE,
  nodeShadow = FALSE, scaleNodeBreadthsByString = FALSE,
  xScalingFactor = 1, yOrderComparator = NULL)

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.

NodePosX

character specifying a column in the Nodes data frame that specifies the 0-based ordering of the nodes along the x-axis.

NodeValue

character specifying a column in the Nodes data frame with the value/size of each node. If NULL, the value is calculated based on the maximum of the sum of incoming and outoging links

NodeColor

character specifying a column in the Nodes data frame with the color of each node. Overrides colourScale.

NodeFontColor

character specifying a column in the Nodes data frame with the color of the label of each node.

NodeFontSize

character specifying a column in the Nodes data frame with the size of the label of each node.

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/mbostock/d3/wiki/Ordinal-Scales.

fontSize

numeric font size in pixels for the node text labels.

fontFamily

font family for the node text labels.

fontColor

font color for the node text labels.

nodeWidth

numeric width of each node.

nodePadding

numeric essentially influences the width height.

nodeStrokeWidth

numeric width of the stroke around nodes.

nodeCornerRadius

numeric Radius for rounded nodes.

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.

title

character Title of plot, put in the upper-left corner of the Sankey

numberFormat

number format in toolstips - see https://github.com/d3/d3-format for options.

orderByPath

boolean Order the nodes vertically along a path - this layout only works well for trees where each node has maximum one parent.

highlightChildLinks

boolean Highlight all the links going right from a node or link.

doubleclickTogglesChildren

boolean Show/hide target nodes and paths to the left on double-click. Does not hide incoming links of target nodes, yet.

xAxisDomain

character[] If xAxisDomain is given, an axis with those value is added to the bottom of the plot. Only sensible when also NodeXPos are given.

dragX

boolean Allow moving nodes along the x-axis?

dragY

boolean Allow moving nodes along the y-axis?

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.

zoom

logical value to enable (TRUE) or disable (FALSE) zooming

align

character Alignment of the nodes. One of 'right', 'left', 'justify', 'center', 'none'. If 'none', then the labels of the nodes are always to the right of the node.

showNodeValues

boolean Show values above nodes. Might require and increased node margin.

linkType

character One of 'bezier', 'l-bezier', 'trapezoid', 'path1' and 'path2'.

curvature

numeric Curvature parameter for bezier links - between 0 and 1.

linkColor

numeric Color of links.

nodeLabelMargin

numeric margin between node and label.

linkOpacity

numeric Opacity of links.

linkGradient

boolean Add a gradient to the links?

nodeShadow

boolean Add a shadow to the nodes?

scaleNodeBreadthsByString

Put nodes at positions relatively to string lengths - only work well currently with align='none'

xScalingFactor

numeric Scale the computed x position of the nodes by this value.

yOrderComparator

Order nodes on the y axis by a custom function instead of ascending or descending depth.

Source

D3.js was created by Michael Bostock. See http://d3js.org/ and, more specifically for Sankey diagrams http://bost.ocks.org/mike/sankey/.

See Also

JS

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 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)


## End(Not run)

fbreitwieser/sankeyD3 documentation built on May 16, 2019, 12:03 p.m.