iGraph图中的顶点名称在哪里

时间:2022-06-14 21:45:54

My general problem is that I loose the vertex names / labels (not sure about the right word here) when generating a graph using iGraph.

我的一般问题是,当使用iGraph生成图形时,我松开了顶点名称/标签(这里不确定正确的单词)。

I have an edge list IC_edge_sub of a bipartite network, that looks like the following:

我有一个二分网络的边缘列表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

I then create a graph element:

然后我创建一个图元素:

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

Then collapse it to identify only connections between companyIDs

然后折叠它以仅识别companyID之间的连接

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

And then get the adjacency matrix:

然后得到邻接矩阵:

CC_matrix_IC_based <- get.adjacency(CC_graph_IC_based); CC_matrix_IC_based

In iGraph node numbering starts at zero and thus also the matrix naming starts at zero. However, I would instead now need the "new_companyID" as specified in the 2nd column of the edgelist in the eventual CC_matrix_IC_based matrix.

在iGraph中,节点编号从零开始,因此矩阵命名也从零开始。但是,我现在需要在最终的CC_matrix_IC_based矩阵中的edgelist的第2列中指定的“new_companyID”。

Can you help me how to use the information form the original edgelist to put in rownames and colnames in the eventual adjacency matrix?

你能帮助我如何使用原始边缘列表中的信息在最终的邻接矩阵中放入rownames和colnames吗?

I googled it and searched stack flow, but could not really find a working answer. Thanks a lot for your help

我用Google搜索并搜索了堆栈流,但无法找到合适的答案。非常感谢你的帮助

2 个解决方案

#1


24  

Vertex names are usually stored in a vertex attribute named name in igraph. So, if your graph is stored in the variable g, then you can use V(g)$name to retrieve the names of all the vertices.

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

#2


1  

I know, fairly presumptuous to answer one's own questions.

我知道,回答一个人自己的问题是相当冒昧的。

I think I have resolved it. The key issue was that I had not saved the names when generating the graph. Thanks to Tamas. Without her answer I would not have realized that. Afterwards I needed to ensure not to loose the data. In the following the overall solution:

我想我已经解决了。关键问题是我在生成图表时没有保存名称。感谢Tamas。没有她的回答我就不会意识到这一点。之后我需要确保不要丢失数据。以下是整体解决方案:

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

#1


24  

Vertex names are usually stored in a vertex attribute named name in igraph. So, if your graph is stored in the variable g, then you can use V(g)$name to retrieve the names of all the vertices.

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

#2


1  

I know, fairly presumptuous to answer one's own questions.

我知道,回答一个人自己的问题是相当冒昧的。

I think I have resolved it. The key issue was that I had not saved the names when generating the graph. Thanks to Tamas. Without her answer I would not have realized that. Afterwards I needed to ensure not to loose the data. In the following the overall solution:

我想我已经解决了。关键问题是我在生成图表时没有保存名称。感谢Tamas。没有她的回答我就不会意识到这一点。之后我需要确保不要丢失数据。以下是整体解决方案:

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