在iGraph中组合图形时如何保留顶点名称?

时间:2012-11-05 04:53:08

标签: r union 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

1 个答案:

答案 0 :(得分:2)

在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 mailing listigraph bug tracker中的错误。

我认为graph.union不保留名称的原因是因为无法保证调用中的所有图形在其节点上都具有相同的属性,并且太麻烦无法检查... < / p>