RforceNetwork警告消息:“看来源/目标未零索引这在JavaScript中是必需的,因此您的图可能无法呈现”

问题描述

我正在尝试使用networkD3库创建一个图形网络。我查阅了以下stackoverflow答案(forceNetwork not displaying any edges),并按照说明进行操作,通过从源目标和源链接中“减去1”来确保“源/目标以零为中心”。但是我仍然收到一条警告消息,指示它们尚未居中,并且未生成任何图形。

首先,我模拟了一些虚假数据

DATABASES = {
    
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis','NAME': 'Blog&Cart','USER': 'postgres','PASSWORD': 'C----------','HOST': 'localhost','PORT': '5432'
    }


}

然后,我按照stackoverflow图上的说明创建图形:

library(dplyr)
library(networkD3)

#create file from which to sample from
x5 <- sample(1:1000000000,2000,replace=T)
#convert to data frame
x5 = as.data.frame(x5)

#create first file (take a random sample from the created file)
source = sample_n(x5,1000)
#create second file (take a random sample from the created file)
target = sample_n(x5,1000)

#combine
links = cbind(source,target)
#create dataframe
links = data.frame(links)
#rename column names
colnames(links) <- c("source","target")

#convert to factors
links$source = as.factor(links$source)
links$target = as.factor(links$target)

#add value column (I think all values should be 1?)
links$value =1


#create nodes 
#first create group (since I have no information,I will assume everyone is in the same group)

nodes = links
nodes$nodeID = nodes$source
nodes$group = 1
nodes = nodes[,c(4,5)]

但是我仍然收到以下警告消息:

# build a new Nodes data frame that includes every 
# unique node found in links$source and links$target
nodes_complete <- data.frame(nodeID = unique(c(links$source,links$target)))

# add groups already identified in your original Nodes data frame
nodes_complete$group <- nodes$group[match(nodes_complete$nodeID,nodes$nodeID)]

# convert your links$source and links$target nodes to their index
# in the new Nodes data frame (zero-indexed)
links$source <- match(links$source,nodes_complete$nodeID) - 1
links$target <- match(links$target,nodes_complete$nodeID) - 1

# Now the forceNetwork function will run as expected
library(networkD3)
forceNetwork(Links = links,Nodes = nodes_complete,Source = "source",Target = "target",NodeID = "nodeID",Group = "group",opacity = 0.8,colourScale = "d3.scaleOrdinal(d3.schemeCategory10);")

有人知道我在做什么错吗?

解决方法

如果您在代码中删除这些行,它将起作用

links$source = as.factor(links$source)
links$target = as.factor(links$target)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...