为三角形中的每个边缘着色(igraph)

时间:2016-11-04 22:01:35

标签: r igraph

我想使用igraph中的R为图表中的三角形着色。下面的图g具有蝴蝶结形状,我想以不同的方式为两个三角形着色。

library(igraph)
edge_vector <- c(1, 2, 1, 3, 2, 3, 3, 4, 3, 5, 4, 5)
g <- make_undirected_graph(edge_vector)
g
plot(g)

enter image description here

我可以按如下方式识别三角形,其中每列构成三角形中的三个顶点。

T <- matrix(triangles(g), nrow=3)
print(T)
##      [,1] [,2]
## [1,]    3    3
## [2,]    1    4
## [3,]    2    5

循环遍历列,即对于每个三角形,我可以将三角形的三个边形成为数字对(例如3,1和3,2和1,2),但我怎样才能进行查找例如,在E(g)的那些边上,以便我可以设置每条边的颜色属性?

我在更大的图表中意识到,边缘可能是多个三角形,但我想知道如何在这个简单的情况下做到这一点。

1 个答案:

答案 0 :(得分:2)

colors <- c("blue", "red")
for (i in seq(ncol(T))){
  E(g, c(T[1, i], T[2, i], 
         T[1, i], T[3, i], 
         T[2, i], T[3, i]))$color <- colors[i]
}
plot(g)

enter image description here

相关问题