(R) forceNetwork 警告消息:“看起来源/目标不是零索引。这在 JavaScript 中是必需的,因此您的绘图可能无法呈现”

Posted

技术标签:

【中文标题】(R) forceNetwork 警告消息:“看起来源/目标不是零索引。这在 JavaScript 中是必需的,因此您的绘图可能无法呈现”【英文标题】:(R) forceNetwork warning message : "It looks like Source/Target is not zero-indexed. This is required in JavaScript and so your plot may not render" 【发布时间】:2021-02-14 15:00:11 【问题描述】:

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

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

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);")

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

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

有谁知道我做错了什么?

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

谢谢!我将它们转换为因子的原因是因为我在真实数据中的 id 是字母数字(例如 AB100、AB101)。有没有办法以变量类型作为因素来运行它?另外,我尝试将 zoom=T 添加到 forceNetwork()。有没有一种方法可以让您更好地浏览视觉图?感谢您的所有帮助! 没有。如果数据是字母数字,请使用字符类型。

以上是关于(R) forceNetwork 警告消息:“看起来源/目标不是零索引。这在 JavaScript 中是必需的,因此您的绘图可能无法呈现”的主要内容,如果未能解决你的问题,请参考以下文章

R:在 foreach %dopar% 中显示错误和警告消息

主机磁盘使用情况:有关磁盘使用的警告消息

在 Mac 上安装 R - 警告消息:设置 LC_CTYPE 失败,使用“C”

警告消息mayBeLonLat(x):使用R中的裁剪功能,CRS为NA

带有详细子视图的警告消息?

抑制但捕获 R 中的警告