forceNetwork的索引不为零 [英] forceNetwork is not zero indexed

查看:73
本文介绍了forceNetwork的索引不为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的forceNetwork,但是该图无法渲染.我不断收到以下警告:

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.

我该如何解决?请注意,simpleNetwork正常工作,因此问题似乎出在我如何指定数据上.

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)

推荐答案

由于networkD3使用javascript,因此您需要将索引从0开始而不是从1开始进行链接.只需从您的节点/链接中减去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')

这篇关于forceNetwork的索引不为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆