5

我正在尝试创建一个简单的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)
4

1 回答 1

7

由于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')
于 2016-11-03T16:07:57.353 回答