This README includes information on set up and a number of basic examples. For more information see the package’s main page.
Here’s an example of simpleNetwork
:
library(networkD3)
# Create fake data
<- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
src <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
target <- data.frame(src, target)
networkData
# Plot
simpleNetwork(networkData)
Here’s forceNetwork
:
# Load data
data(MisLinks)
data(MisNodes)
# Plot
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 0.4,
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);"))
Here’s sankeyNetwork
using a downloaded JSON data
file:
# Recreate Bostock Sankey diagram: http://bost.ocks.org/mike/sankey/
# Load energy projection data
<- paste0("https://cdn.rawgit.com/christophergandrud/networkD3/",
URL "master/JSONdata/energy.json")
<- jsonlite::fromJSON(URL)
Energy
# Plot
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30)
You can use igraph to create
network graph data that can be plotted with networkD3.
The igraph_to_networkD3
function converts igraph graphs to
lists that work well with networkD3. For example:
# Load igraph
library(igraph)
# Use igraph to make the graph and find membership
<- make_graph("Zachary")
karate <- cluster_walktrap(karate)
wc <- membership(wc)
members
# Convert to object suitable for networkD3
<- igraph_to_networkD3(karate, group = members)
karate_d3
# Create force directed network plot
forceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes,
Source = 'source', Target = 'target', NodeID = 'name',
Group = 'group')
Use saveNetwork
to save a network to stand alone HTML
file:
library(magrittr)
simpleNetwork(networkData) %>% saveNetwork(file = 'Net1.html')
networkD3 began as a port of d3Network package to the htmlwidgets framework. d3Network is no longer supported.