Learn R Programming

networkD3 (version 0.1.1)

sankeyNetwork: Create a D3 JavaScript Sankey diagram

Description

Create a D3 JavaScript Sankey diagram

Usage

sankeyNetwork(Links, Nodes, Source, Target, Value, NodeID, height = NULL,
  width = NULL, colourScale = "d3.scale.category20()", fontsize = 7,
  nodeWidth = 15, nodePadding = 10)

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.
height
numeric height for the network graph's frame area in pixels.
width
numeric width for the network graph's frame area in pixels.
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.
nodeWidth
numeric width of each node.
nodePadding
numeric essentially influences the width height.

source

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

Examples

Run this code
# Recreate Bostock Sankey diagram: http://bost.ocks.org/mike/sankey/
# Load energy projection data
library(RCurl)
URL <- "https://raw.githubusercontent.com/christophergandrud/networkD3/master/JSONdata/energy.json"
Energy <- getURL(URL, ssl.verifypeer = FALSE)
# Convert to data frame
EngLinks <- JSONtoDF(jsonStr = Energy, array = "links")
EngNodes <- JSONtoDF(jsonStr = Energy, array = "nodes")

# Plot
sankeyNetwork(Links = EngLinks, Nodes = EngNodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",

Run the code above in your browser using DataLab