Learn R Programming

d3Network (version 0.4.2)

d3Sankey: Create a D3 JavaScript Sankey diagram

Description

Create a D3 JavaScript Sankey diagram

Usage

d3Sankey(Links, Nodes, Source, Target, Value = NULL,
    NodeID, height = 600, width = 900, fontsize = 7,
    nodeWidth = 15, nodePadding = 10, standAlone = TRUE,
    file = NULL, iframe = FALSE,
    d3Script = "http://d3js.org/d3.v3.min.js")

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 a 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.
fontsize
numeric font size in pixels for the node text labels.
nodeWidth
numeric width of each node.
nodePadding
numeric essentially influences the width height.
standAlone
logical, whether or not to return a complete HTML document (with head and foot) or just the script.
file
a character string of the file name to save the resulting graph. If a file name is given a standalone webpage is created, i.e. with a header and footer. If file = NULL then result is returned to the console.
iframe
logical. If iframe = TRUE then the graph is saved to an external file in the working directory and an HTML iframe linking to the file is printed to the console. This is useful if you are using Slidify and many other HTML
d3Script
a character string that allows you to specify the location of the d3.js script you would like to use. The default is http://d3js.org/d3.v3.min.js.

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/
## dontrun
# Load energy projection data
# library(RCurl)
# URL <- "https://raw.github.com/christophergandrud/d3Network/sankey/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
# d3Sankey(Links = EngLinks, Nodes = EngNodes, Source = "source",
#          Target = "target", Value = "value", NodeID = "name",
#          fontsize = 12, nodeWidth = 30, file = "~/Desktop/TestSankey.html")

Run the code above in your browser using DataLab