This started as a port of Christopher Gandrud’s R package d3Network for creating D3network graphs to the htmlwidgets framework. The htmlwidgets framework greatly simplifies the package’s syntax for exporting the graphs, improves integration with RStudio’s Viewer Pane, RMarkdown, and Shiny web apps. See below for examples.
It currently supports the following types of network graphs:
networkD3 works very well with the most recent version of RStudio (>=v0.99, download). When you use this version of RStudio, graphs will appear in the Viewer Pane. Not only does this give you a handy way of seeing and tweaking your graphs, but you can also export the graphs to the clipboard or a PNG/JPEG/TIFF/etc. file.
For a full set of examples for each of the functions see this page.
From version 0.2, tree diagrams can be created using radialNetwork or diagonalNetwork.
URL <- paste0(
## Convert to list format
Flare <- jsonlite::fromJSON(URL, simplifyDataFrame =FALSE)
# Use subset of data for more readable diagram
Flare$children = Flare$children[1:3]
radialNetwork(List = Flare, fontSize =10, opacity =0.9)
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 igraphlibrary(igraph)
# Use igraph to make the graph and find membership
karate <- make_graph("Zachary")
wc <- cluster_walktrap(karate)
members <- membership(wc)
# Convert to object suitable for networkD3
karate_d3 <- igraph_to_networkD3(karate, group = members)
# Create force directed network plotforceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes,
Source ='source', Target ='target',
NodeID ='name', Group ='group')
In the shinyUI part of your app.R file (for single-file Shiny apps) include *NetworkOutput (with * as before, but starting with a lowercase letter). The argument placed in this function should be the element specified with output, e.g.:
You can run a simple example with the following code:
Full source code for this example can be found here.
Saving as static PNG image
You can use RStudio to save static images of networkD3 plots as PNG files. Simply create your plot as usual in RStudio. The output should appear in the Viewer pane. Then click Export > Save as Image…. A new window will appear. You can use this window to manipulate the plot, resize it, and save the result as a PNG file.