您的代码引用了我们没有的文件“grph.graphml”,因此我无法使用您的示例。相反,我将使用随机图来说明。
## Generate random graph
library(igraph)
set.seed(1234)
g = erdos.renyi.game(15, 0.2)
## Your code to generate labels
random_list <- sample(c(TRUE,FALSE), gorder(g), TRUE)
V(g)$label <- random_list
可以编写一个小函数来测试边的端点是否具有相同的标签,并将其应用于所有边。然后删除两端相同的。
SameLabel = function(e) {
V(g)[ends(g, e)[1]]$label == V(g)[ends(g, e)[2]]$label }
g2 = delete_edges(g, which(sapply(E(g), SameLabel)))
您可以通过绘制来检查它是否正在做正确的事情。
set.seed(1066)
LO = layout_with_fr(g)
par(mfrow=c(1,2), mar=c(1,1,1,1))
plot(g, layout=LO, frame=TRUE)
plot(g2, layout=LO, frame=TRUE)
其中一些看起来是错误的,因为远程节点之间的链接位于其他类型相反的节点之后。