iGraph图中的顶点名称在哪里?

20

我的一般问题是,使用iGraph生成图时,我丢失了顶点名称/标签(对于正确的单词不确定)。

我有一个二分网络的边缘列表IC_edge_sub,看起来像下面这样:

  new_individualID new_companyID
1             <NA>     10024354c
3        10069415i      2020225c
4        10069415i     16020347c
5        10069272i      2020225c
6        10069272i     16020347c
7        10069274i      2020225c

然后我创建了一个图形元素:

IC_projected_graphs <- bipartite.projection(IC_twomode, types = 
                         is.bipartite(IC_twomode)$type)

然后折叠它,只识别公司ID之间的连接。

IC_projected_graphs <- bipartite.projection(IC_twomode, types =   
                         is.bipartite(IC_twomode)$type)

然后获取邻接矩阵:

CC_matrix_IC_based <- get.adjacency(CC_graph_IC_based); CC_matrix_IC_based
在iGraph中,节点编号从零开始,因此矩阵命名也从零开始。但是,我现在需要在最终的CC_matrix_IC_based矩阵中使用edgelist中第二列指定的"new_companyID"作为行名和列名。
请问,如何使用原始edgelist中的信息将其放入最终的邻接矩阵中的行名和列名中?
我在谷歌上搜索并查找了Stack Overflow,但并没有找到真正有效的答案。感谢您的帮助。

1
感谢 Andrie 的编辑。我没有看到我的写作所造成的问题。非常感激。 - Henning Piezunka
3个回答

28

在igraph中,顶点名称通常存储在名为name的顶点属性中。因此,如果您的图形存储在变量g中,则可以使用V(g)$name来检索所有顶点的名称。


4
很遗憾,这在我的情况下不起作用。我只是得到了一个空值,太糟糕了。有没有其他替代的想法呢?抱歉,但我真的被卡住了。 - Henning Piezunka
如何获取数字顶点ID和名称之间的关系? - pengchy
@HenningPiezunka 当顶点属性列表不包含“name”条目时,就会出现这种情况,但您仍然可以使用形式为vertex_attr(g, "name") <- c("vector", "of", "names")的语法进行设置,可选择使用“index”参数选择顶点子集,默认为“index = V(graph)” 。请参阅查询图的顶点属性下的文档。 - Anil

2
重要问题是我在生成图表时没有保存名称。之后,我需要确保不会丢失数据。以下是整体解决方案:
# Subsetting / triangulating data for selected games
GC_edge_sub <- subset (GC_edge, mb_titleID %in% loggames_yearly_sample$mb_titleID)
GC_edge_sub <- subset(GC_edge_sub, select = c("new_titleID", "new_companyID"))
head(GC_edge_sub)

# Generating the vertex names
vertex_new_companyID <- data.frame(names = unique(GC_edge_sub$new_companyID))
vertex_new_titleID <- data.frame(names = unique(GC_edge_sub$new_titleID))
vertex <- rbind(vertex_new_companyID, vertex_new_titleID)

# Creation of GC_twomode
GC_twomode <- graph.data.frame(GC_edge_sub, vertices = vertex)
GC_projected_graphs <- bipartite.projection(GC_twomode, 
                                            types = is.bipartite(GC_twomode)$type)
GC_matrix_GC_based <- get.adjacency(GC_twomode)
dim(GC_matrix_GC_based)

# Collapsing the matrix
# Be aware that if you use the classical command 
# `CC_graph_GC_based <- GC_projected_graphs$proj2` 
# it collapses, but looses the colnames and rownames
# I thus: a) create a subset of the adjacency matrix; and
#         b) create the lookef for matrix by multiplication
rowtokeep <- match(vertex_new_companyID$names, colnames(GC_matrix_GC_based))
coltokeep <- match(vertex_new_titleID$names, rownames(GC_matrix_GC_based))
GC_matrix_GC_based_redux <- GC_matrix_GC_based[rowtokeep, coltokeep]
# We now have a CG matrix.Let's build from this a GG matrix.
CC <- GC_matrix_GC_based_redux %*% t(GC_matrix_GC_based_redux)

0

顶点名称存储在label属性中。

如果g是图形,则可以通过V(g)$label访问顶点名称。


在发布解决方案之前,请检查先前的答案,您的答案只会重复先前已经发布的信息。谢谢。 - Leroy Tyrone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接