forceNetwork 不是零索引的

forceNetwork is not zero indexed

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

由于 networkD3 使用 javascript,您需要从 0 开始索引,而不是从 1 开始链接。只需从 nodes/links 中减去 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')