Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the vertex names in an iGraph graph

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

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

  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

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.

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

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

like image 635
Henning Piezunka Avatar asked Jun 08 '12 04:06

Henning Piezunka


People also ask

What is a vertex in igraph?

Vertex sequences are usually used as igraph function arguments that refer to vertices of a graph. A vertex sequence is tied to the graph it refers to: it really denoted the specific vertices of that graph, and cannot be used together with another graph.

How do you delete edges in igraph?

The minus operator (' - ') can be used to remove vertices or edges from the graph. The operation performed is selected based on the type of the right hand side argument: If it is an igraph graph object, then the difference of the two graphs is calculated, see difference .

How do you delete a self loop in igraph?

The function SimpleGraph[] removes both self-loops and multiple edges in the same vertices of a Graph.


2 Answers

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.

like image 55
Tamás Avatar answered Oct 02 '22 18:10

Tamás


The key issue was that I had not saved the names when generating the graph. Afterwards I needed to ensure not to lose the data. In the following the overall solution:

# 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)
like image 2
Henning Piezunka Avatar answered Oct 02 '22 18:10

Henning Piezunka