如何在商图上使用原始图中的名称?

How to use the names from original graph on quotient graph?

我有一个图表 g1,我需要找到 quotient graph g2。 我的尝试是:

library(igraph)

n = 8
m <- t(matrix(c(
0,0,0,0,0,0,0,8,
3,0,0,0,0,0,0,0,
5,0,0,5,1,0,0,0,
0,0,6,0,0,7,1,0,
0,6,2,0,0,0,0,0,
0,0,0,0,0,0,0,0,
7,4,0,0,8,0,0,3,
0,3,0,0,0,9,0,0),ncol=n))

g1 <- graph_from_adjacency_matrix(m, weighted=TRUE, mode="directed")
V(g1)$names <- letters[1:n]
V(g1)$label <- V(g1)$names

g2 <- contract(g1, components(g1, mode = "strong")$membership, vertex.attr.comb=toString)
g2 <- simplify(g2)

图表 g2 具有三个组成部分:{a, b, h}, {c, d, e, g} 和 {f} 我需要使用每个组成部分的第一个字母作为 vertex.labels.

plot(g2, vertex.label = substr(toupper(V(g2)$label), 1, 1))

结果对我来说是正确的:

问题。不使用附加属性V(g1)$label是否可以解决问题?

您应该使用$name(而不是$names)来添加顶点名称属性,例如

g1 <- graph_from_adjacency_matrix(m, weighted = TRUE, mode = "directed")
V(g1)$name <- letters[1:n]

g2 <- contract(g1, components(g1, mode = "strong")$membership, vertex.attr.comb = toString)
g2 <- simplify(g2)

然后,当你运行plot(g2)时,你会看到