forceNetwork不是零索引

时间:2016-11-03 15:12:55

标签: javascript r igraph htmlwidgets networkd3

我正在尝试创建一个简单的forceNetwork,但该图不会呈现。我一直收到以下警告:

Warning message: It looks like Source/Target is not zero-indexed. This is required in JavaScript and so your plot may not render.

我该如何解决这个问题?请注意,simpleNetwork工作正常,所以问题似乎在于我如何指定数据。

library(igraph)
library(networkD3)
set.seed(42)
temp<-data.frame(source=c(1,2,3,4),target=c(2,3,4,4))#csv[1:500,]

links<-data.frame(source=temp$source,target=temp$target)
g<-graph.data.frame(cbind(temp$source,temp$target), directed=T)
nodes<-data.frame(name=1:length(V(g)),group=1)

forceNetwork(Links=links,Nodes = nodes,
             Source = 'source', Target = 'target', 
             NodeID = 'name', Group = 'group')

simpleNetwork(temp)

1 个答案:

答案 0 :(得分:4)

由于networkD3使用javascript,因此您需要将索引设置为0而不是1开始链接。只需从节点/链接中减去1即可重新索引:

links = links-1
nodes$name = nodes$name-1 #might want to re-index nodes, too
forceNetwork(Links=links,Nodes = nodes,
             Source = 'source', Target = 'target', 
             NodeID = 'name', Group = 'group')