【问题标题】:igraph - Color Edges of Union of two different treesigraph - 两棵不同树的联合颜色边缘
【发布时间】:2017-09-18 09:38:42
【问题描述】:

您好,我正在使用 igraph 来合并两棵树。在建立这个联合时,请注意这两棵树有共同的名字。我想为图形联合着色,以便保留边缘的颜色。

n<-3
n2<-3
tree<-make_tree(n)
tree2<-make_tree(n2)

tree<-set.vertex.attribute(tree, "name", value=letters[1:n])
tree2<-set.vertex.attribute(tree2, "name", value=sample(letters[1:n2]))
E(tree)$color <- "blue"
E(tree2)$color<-"red"
plot(tree)
plot(tree2)
tree_union<-tree %u% tree2
plot(tree_union)

例如,a->b 和 a->c 仍然是蓝色的(树),而 c->a 和 c->b 仍然是红色的(tree2)。显然,我不想手动设置每条边的颜色。我在想类似的东西

E(tree_union)[E(tree_union)==E(tree)]$color<-"blue"

但是,它不起作用,因为相等不检查两条边是否相等。

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    原始颜色保留在 color_1 和 color_2 中。因此,您可以轻松创建一个颜色变量来保持边缘颜色。但是,在某些情况下,您的一条边会绘制在另一条边上,因此您所看到的只是箭头。

    TU_col = edge_attr(tree_union, "color_1")
    E2 = which(is.na(edge_attr(tree_union, "color_1")))
    TU_col[E2] = edge_attr(tree_union, "color_2")[E2]
    tree_union2 = set_edge_attr(tree_union, "color", value=TU_col)
    plot(tree_union2)
    

    【讨论】:

      最近更新 更多