forceNetwork is not zero indexed

試著忘記壹切 提交于 2019-12-31 01:56:09

问题


I am trying to create a simple forceNetwork, but the plot won't render. I keep getting the following warning:

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

How do I fix this? Note that simpleNetwork works fine so the problem seems to be in how I am specifying my data.

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:


Since networkD3 uses javascript, you need to start your indexing at 0 and not 1 for links. Simply subtract 1 from your nodes/links to reindex:

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')


来源:https://stackoverflow.com/questions/40405120/forcenetwork-is-not-zero-indexed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!