igraph - 两种不同树木联盟的颜色边缘

时间:2017-09-18 01:15:05

标签: r igraph

您好我正在使用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"

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

1 个答案:

答案 0 :(得分: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)

Tree union with color

相关问题