我试图创建一个简单的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。只需从您的节点/链接中减去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')