R-在IGRAPH中组合图时如何保留顶点名称



我正在尝试使用igraph中的 graph.union结合图,但是当我执行结果时,该图不会保留其顶点标签。

testGraph1 = barabasi.game(3, 
    m = 5,
    power = 0.6, 
    out.pref = TRUE,
    zero.appeal = 0.5,
    directed = TRUE)
V(testGraph1)$name = c('one', 'two', 'three')   
testGraph2 = barabasi.game(5, 
    m = 5,
    power = 0.6, 
    out.pref = TRUE,
    zero.appeal = 0.5,
    directed = TRUE)    
V(testGraph2)$name = c('one', 'two', 'three', 'four', 'five')   
combine = graph.union(testGraph1, testGraph2)
V(combine)$name #Is NULL

我也尝试使用graph.union.by.name,但我认为有一个错误,因为两个测试图肯定是指导的,但我遇到了一个奇怪的错误。

combine = graph.union.by.name(testGraph1, testGraph2)
#Error: !is.directed(g1) & !is.directed(g2) is not TRUE

在graph.union.by.name的开头似乎与文档不匹配。如果您将其删除,并且还将定向选项添加到最后一行,我认为您确实得到了您想要的东西:

gubm = function (g1, g2, dir=FALSE) 
{
  #stopifnot(!is.directed(g1) & !is.directed(g2))
    dv1 = igraph:::get.vertices.as.data.frame(g1)
    dv2 = igraph:::get.vertices.as.data.frame(g2)
    de1 = igraph:::get.edges.as.data.frame(g1)
    de2 = igraph:::get.edges.as.data.frame(g2)
    dv = igraph:::safer.merge(dv1, dv2, all = TRUE)
    de = igraph:::safer.merge(de1, de2, all = TRUE)
    g = igraph:::graph.data.frame(de, directed = dir, vertices = dv)
    return(g)
}
> combine=gubm(testGraph1,testGraph2,TRUE)
> V(combine)$name
[1] "one"   "three" "two"   "five"  "four" 

,但请在大量示例中检查一下,以确保其行为正确。我怀疑IGRAPH开发人员会在这里发现这一点,但是您应该在IGRAPH邮件列表或Igraph Bug Tracker中报告为错误。

我认为 graph.union不保留名称的原因是因为无法保证呼叫中的所有图表都在其节点上具有相同的属性,而且要检查太多了...

<...

最新更新