How to create a bipartite network in R with igraph or tnet

16,161

In igraph a bipartite network is one that has a type vertex attribute. This attribute must be logical and must the TRUE for one of the node types and FALSE for the others. So to create a bipartite network from your edge list, you simply create a regular graph and then add the type vertex attribute:

edgelist <- read.table(text="Person    Event
                         Amy       football
                         Bob       picnic
                         Sam       artshow", 
                   header=TRUE)
igraph <- graph.data.frame(edgelist)

V(igraph)$type <- V(igraph)$name %in% edgelist[,1]
igraph
# IGRAPH DN-B 6 3 -- 
# + attr: name (v/c), type (v/x)

The 'B' letter tells you that this is a bipartite graph. You can create the unipartite projections of this network via:

bipartite.projection(igraph)
# $proj1
# IGRAPH UN-B 3 0 -- 
# + attr: name (v/c), type (v/x)
#
# $proj2
# IGRAPH UN-B 3 0 -- 
# + attr: name (v/c), type (v/x)

This will return a list of two graphs. If you think that the projection might be too big, you can first call the bipartite.projection.size function, this will give you the number of vertices and edges in both projections. The memory requirement for an igraph graph is (4m+2n)*8+O(1) bytes, where 'n' is the number of vertices and 'm' is the number of edges.

Share:
16,161
Olga Mu
Author by

Olga Mu

Updated on July 23, 2022

Comments

  • Olga Mu
    Olga Mu almost 2 years

    I have an edgelist for a two mode network, similar to this:

    person  Event
    Amy     football_game
    Sam     picnic
    Bob     art_show
    

    I want to perform an analysis on this in R, but seemingly everything I try fails. Converting it to a one mode network runs into memory limitations, and I can't figure out how to analyze it as bipartite in either igraph or tnet.

    In igraph, bipartite.projection gives me all FALSE, on the igraph object created using

    net <- graph.edgelist(myobject)
    

    On tnet, I can't convert the igraph net to a tnet one, and when I try to use the original data frame, it refuses because of duplicates in the graph.

    So answers to any of the following would be super appreciated:

    1. How do I use the bipartite.mapping function?
    2. How do I input an igraph object into tnet?
    3. If all else fails, how I do I input a data frame with duplicate edges into tnet?

    Sorry if these are basic questions, but there's very little documentation.

    EDIT

    Example:

    edgelist <- read.table(text="Person    Event
                                 Amy       football
                                 Bob       picnic
                                 Sam       artshow", 
                           header=TRUE)
    edgelist <- as.matrix(edgelist)
    
    ## Igraph Issues
    igraph <- graph.edgelist(edgelist)
    typevector <- bipartite.projection(igraph) 
    # gets all FALSE
    
    edgelist2 <- get.edgelist(igraph)
    typevector <- bipartite.projection(edgelist2) 
    # same thing
    
    ## tnet issues
    tnet <- as.tnet(edgelist) 
    # gives error: "There are duplicate events in the edgelist"
    tnet <- as.tnet(edgelist2)
    clusterMat <- clustering_local_tm(tnet)  
    # gives error: "max not meaningful for factors"
    
    onemode <- projecting_tm(tnet, method="Newman") 
    # gives error: "arguments must have same length"
    
  • user1317221_G
    user1317221_G about 10 years
    @Gabor i was trying to project a weighted biparite graph and i get the error negative vectors not allowed which crashes my R session...have you had this before?
  • Gabor Csardi
    Gabor Csardi about 10 years
    @user1317221_G: most likely your graph is not bipartite, i.e. you have connections between vertices of the same type.
  • Koundy
    Koundy about 9 years
    @Gabor Can you please tell how to visualize that graph. with two different kinds of nodes with different colors
  • Gabor Csardi
    Gabor Csardi about 9 years
    Set vertex color based on vertex type, e.g. V(g)$color <- V(g)$type + 1.
  • Zach
    Zach almost 9 years
    "The memory requirement for an igraph graph is (4m+2n)*8+O(1) byte" What does O(1) mean in this equation?
  • Gabor Csardi
    Gabor Csardi almost 9 years
    @Zach Constant amount, independent of the number of vertices and edges.
  • Zach
    Zach almost 9 years
    Ahhh, I see. Thank you.