Data preparation with R for networks in d3.js





This documents explains how to prepare your network input data for visualization with d3.js. D3.js requires a very specific JSON format. Here is how to use R to reformat the more usual way of storing the network information.

The Json input format we need for d3.js


Building a network charts requires information on nodes and links. This information can be stored in many different format as described here. Json format is the most convenient way to work with d3.js and looks basically like that:

{ "nodes": [
    { "id": 1, "name": "A" },
    { "id": 2, "name": "B" }
  ],
  "links": [
    { "source": 1, "target": 2 }
  ]}
It is unlikely that your data are currently at this format. You probably need to reformat your data first using another tool like R. The following sections gives a few examples on how to reformat the most common types of input to get a json file.

Json format from adjacency matrix


An adjacency matrix is a square matrix where individuals are the same in row and columns of the matrix. It’s typically the kind of matrix you get when calculating the correlation between each pair of individual.

The following lines of R code build a small adjacency matrix, and use the igraph and d3r libraries to convert it in a json file.

# Let's build an adjacency matrix
data <- matrix(sample(0:1, 25, replace=TRUE), nrow=5)
colnames(data) <- rownames(data) <- LETTERS[1:5]

# Transform it in a graph format
library(igraph)
network <- graph_from_adjacency_matrix(data)

# Transform it in a JSON format for d3.js
library(d3r)
data_json <- d3_igraph(network)

# Save this file
write(data_json, "data.json")

You now have a file called data.json that is ready to be used by d3.

Json format from edge list


An edge list is a table listing all the connections.

The following lines of R code build a small edge list, and use the igraph and d3r libraries to convert it in a json file.

# Let's say I have an edge list
links=data.frame(
  source=c("A","A", "A", "A", "A","F", "B"),
  target=c("B","B", "C", "D", "F","A","E")
)

# Transform it in a graph format
library(igraph)
network=graph_from_data_frame(d=links, directed=F)

# Transform it in a JSON format for d3.js
library(d3r)
data_json <- d3_igraph(network)

# Save this file
write(data_json, "data.json")

You now have a file called data.json that is ready to be used by d3.

Json format from edge and node lists


In addition to the previous edge list, it is possible to add a second data input which provides some features concerning each node. It is useful if you want to highlight dot features on your network chart.

The following lines of R code build a small edge list and a table with node features, and use the igraph and d3r libraries to convert it in a json file.

# Let's say I have an edge list + node list with info
links=data.frame(
  source=c("A","A", "A", "A", "A","F", "B"),
  target=c("B","B", "C", "D", "F","A","E")
)
nodes=data.frame(
  name=LETTERS[1:6],
  carac=c( rep(10,3), rep(30,3))
)

# Turn it into igraph object
library(igraph)
network=graph_from_data_frame(d=links, vertices=nodes, directed=F)

# Transform it in a JSON format for d3.js
library(d3r)
data_json <- d3_igraph(network)

# Save this file
write(data_json, "data.json")

You now have a file called data.json that is ready to be used by d3.