为什么igraph图警告“要替换的项目数不是替换长度的倍数"? [英] Why igraph plot warns that "number of items to replace is not a multiple of replacement length"?
问题描述
我有一个在两个节点之间有很多边的图.当我绘制它时,我会收到一条警告,在这种情况下我不理解.
I have a graph with many edges between two nodes. When I plot it, I get a warning I don't understand in this context.
这很好:
library(igraph)
gg <- graph.empty(n=0, directed=TRUE)
gg <- gg + vertex("10501")
gg <- gg + edge("10501", "10501")
gg <- gg + edge("10501", "10501")
plot(gg)
但是同一条带有更多边的图形会发出警告:
But the same graph with one more edge gives a warning:
gg <- graph.empty(n=0, directed=TRUE)
gg <- gg + vertex("10501")
gg <- gg + edge("10501", "10501")
gg <- gg + edge("10501", "10501")
gg <- gg + edge("10501", "10501")
plot(gg)
我看不出原因.为什么会有这个警告?
I fail to see the reason. Why this warning?
推荐答案
在igraph
包源中搜索给出警告的行,看来罪魁祸首功能是plot.common.R
中的autocurve.edges
:
Searching the igraph
package source for the line giving the warning, it seems that the culprit function is autocurve.edges
in plot.common.R
:
autocurve.edges <- function(graph, start=0.5) {
cm <- count.multiple(graph)
el <- apply(get.edgelist(graph, names=FALSE), 1, paste, collapse=":")
ord <- order(el)
res <- numeric(length(ord))
p <- 1
while (p <= length(res)) {
m <- cm[ord[p]]
idx <- p:(p+m-1)
if (m==1) {
r <- 0
} else {
r <- seq(-start, start, length=m)
}
res[ord[idx]] <- r
p <- p + m
}
res
}
根据?igraph.plotting
(和程序包的源代码),默认情况下将调用autocurve.edges
函数来确定如何弯曲边缘:
According to ?igraph.plotting
(and the package source) the autocurve.edges
function is the called by default to determine how to curve edges:
默认情况下,指定曲线的矢量是通过调用autocurve.edges函数来计算的.此功能可确保多个边缘是弯曲的并且全部可见.对于循环边缘,将忽略此参数.
By default the vector specifying the curvatire is calculated via a call to the autocurve.edges function. This function makes sure that multiple edges are curved and are all visible. This parameter is ignored for loop edges.
从igraph.plot
的早期开始,我们可以看到此函数被调用,并传递了整个图形:
From early in igraph.plot
, we can see that this function is called, being passed the whole graph:
curved <- params("edge", "curved")
if (is.function(curved)) { curved <- curved(graph) }
这是问题图形上警告的来源:
This is the source of the warnings on the problematic graph:
autocurve.edges(gg)
# [1] -0.5 -0.5 0.0
# Warning messages:
# 1: In res[ord[idx]] <- r :
# number of items to replace is not a multiple of replacement length
# 2: In res[ord[idx]] <- r :
# number of items to replace is not a multiple of replacement length
要深入研究为什么会看到这些问题,此函数要做的第一件事是调用count.multiple
,该函数返回每个边重复的次数.在有问题的图中:
To delve into why we're seeing these issues, the very first thing this function does is call count.multiple
, which returns the number of times each edge is repeated. In the offending graph:
count.multiple(gg)
# [1] 1.5 1.5 1.5
而不是返回c(3, 3, 3)
(因为循环边重复了3次),而是返回了c(1.5, 1.5, 1.5)
.尽管在?count.multiple
中我没有看到任何有关此的信息,但是发生这种情况的原因是,如果边缘是循环的(即从节点到其自身的边缘),则边缘的计数将除以2,这是由于以下行中的igraph_count_multiple
程序包源中structural_properties.c
文件中的igraph_count_multiple
:
Instead of returning c(3, 3, 3)
(since the looping edge is repeated 3 times), it's returning c(1.5, 1.5, 1.5)
. Though I don't see any mention of this in ?count.multiple
, this is happening because the count of an edge is divided by 2 if it is a loop (aka an edge from a node to itself), due to the following line in igraph_count_multiple
from the structural_properties.c
file in the igraph
package source:
/* for loop edges, divide the result by two */
if (to == from) VECTOR(*res)[i] /= 2;
生成警告的事实是由于存在错误-autocurve.edges
预期会获得c(3, 3, 3)
,但由于循环边缘而从count.multiple
获得了c(1.5, 1.5, 1.5)
.我认为autocurve.edges
实际上可以使用类似以下的方式在纯R中重新实现:
The fact that the warnings are generated is due to a bug -- autocurve.edges
was expecting to get c(3, 3, 3)
but instead got c(1.5, 1.5, 1.5)
from count.multiple
due to the loop edges. I think autocurve.edges
could actually be reimplemented in pure R using something like:
autocurve.edges <- function(graph, start=0.5) {
el <- apply(get.edgelist(graph, names=FALSE), 1, paste, collapse=":")
ave(rep(NA, length(el)), el, FUN=function(x) {
if (length(x) == 1) {
return(0)
} else {
return(seq(-start, start, length=length(x)))
}
})
}
此外,我认为count.multiple
的文档应该进行更新,以提及其对循环边的特殊处理.
Further, I think the documentation of count.multiple
should be updated to mention its special handling of loop edges.
同时,我认为您的解决方案是手动指定曲率参数以避免出现警告:
In the meantime, I think the solution in your case would be to manually specify your curvature parameter to avoid the warnings:
plot(gg, edge.curved=FALSE)
更新:作为拉取请求提交给rigraph项目,现在合并到R igraph项目的dev分支中:
Update: Submitted as a pull request to the rigraph project and now merged into the dev branch of the R igraph project: https://github.com/igraph/rigraph/pull/80
这篇关于为什么igraph图警告“要替换的项目数不是替换长度的倍数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!