the manipulation of networkd3 objects in r

To my understanding is the data format slightly different for forceNetwork compared to simpleNetwork.

The Links argument needs a table holding the indices of the nodes stored in the Node table (2 columns: ‘from’ – ‘to’, or as in the example ‘source’ – ‘target’). Be aware that the index should start with 0 and not 1. A 3rd column of ‘Links’ should be the edge width.

The Nodes argument needs a table with the 1) the name and 2) the group.

The additional arguments you are using in your example are column names of corresponding columns in the table you use in ‘Links’ and ‘Nodes’. For instance when you use NodeID = "c$a" that means that your node table has a column named ‘c$a’.

Have a look at the help page :?forceNetwork they describing the arguments much better than I can, but also scroll down to the examples and check the shape of their example data (MisLinks and MisNodes).

Maybe this litte code can give you an orientation (continues with your ‘c’ object):

c$a = as.character(c$a)
c$b = as.character(c$b)

Nodes_IDs <- data.frame(name=sort(unique(c(c$a, c$b))))
Nodes_IDs$gr <- 1

# JavaScript need zero-indexed IDs.
c$a <- match(c$a, Nodes_IDs[,1]) -1
c$b <- match(c$b, Nodes_IDs[,1]) -1
c$width <- .5

forceNetwork(Links = c, Nodes = Nodes_IDs,
             Source = "a", Target = "b",
             Value = "width", NodeID = "name",
             Group = "gr",
             opacity = 0.8)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top