igraph - 两棵不同树的联合的颜色边缘

igraph - Color Edges of Union of two different trees

您好,我正在使用 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 仍然是红色(树 2)。显然,我不想手动设置每条边的颜色。我在想

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

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

在 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)