[R] community finding in a graph and heatplot

Aziz, Muhammad Fayez aziz4 at illinois.edu
Tue Jun 5 19:53:35 CEST 2012


Superb. It works. The results loo great. I just tweaked the function a little bit to suit my needs though. I passed merges and vcount separately as fgc$merges and vcount(g). The vcount attribute was not there in fgc object. Also, I removed the last row from the merges matrix before returning as it was adding the very root level undesired by the heatmap.2 function I was using. The final function looks like this:

# To generate a complete dendogram from disconnected subgraphs in merges given by fgc
complete.dend <- function(merges, vcount) {
  if (nrow(merges) < vcount-1) {
    miss <- seq_len(vcount + nrow(merges))[-as.vector(merges)]
    miss <- c(miss, seq_len(length(miss)-2) + vcount+nrow(merges))
    miss <- matrix(miss, byrow=TRUE, ncol=2)
    merges <- rbind(merges, miss)
  }
  storage.mode(merges) <- "integer"

  merges[-nrow(merges),] # skip last row
}

Thank you so much Gabor for this custom complete.dend fucntion. I couldn't find it in the regular igraph library (http://igraph.sourceforge.net/doc/R/00Index.html).

Best,
Fayez

________________________________________
From: csardi.gabor at gmail.com [csardi.gabor at gmail.com] on behalf of Gábor Csárdi [csardi at rmki.kfki.hu]
Sent: Monday, June 04, 2012 6:33 PM
To: Aziz, Muhammad Fayez
Cc: r-help at r-project.org; Caetano-Anolles, Gustavo
Subject: Re: [R] community finding in a graph and heatplot

On Sun, Jun 3, 2012 at 4:11 PM, Aziz, Muhammad Fayez <aziz4 at illinois.edu> wrote:
>
> Hmm interesting. To come to think of it there could be many disconnected components in the graph and thus there should be a generic way to either mitigate the disconnectedness in the dendrogram or in the original graph. I had no luck in finding such a trick though google search. I then ran the script on minute-scale graphs and have following results:
>
> 1) disconnected graph with three modules:
>
> *Vertices 9
> *Edges
> 1 2 1
> 2 3 1
> 3 1 1
> 4 5 1
> 5 6 1
> 6 4 1
> 7 8 1
> 8 9 1
> 9 7 1
>
> corresponding fgc$merges matrix:
>
>     [,1] [,2]
> [1,]    1    0
> [2,]    2    9
> [3,]    7    6
> [4,]    8   11
> [5,]    4    3
> [6,]    5   13
>
> 2) connected graph by adding links 1-2 and 4-7 in graph 1):
>
> *Vertices 9
> *Edges
> 1 2 1
> 2 3 1
> 3 1 1
> 4 5 1
> 5 6 1
> 6 4 1
> 7 8 1
> 8 9 1
> 9 7 1
> 1 4 1
> 4 7 1
>
> corresponding fgc$merges matrix:
>
>     [,1] [,2]
> [1,]    2    1
> [2,]    0    9
> [3,]    8    7
> [4,]    6   11
> [5,]    5    4
> [6,]    3   13
> [7,]   14   12
> [8,]   15   10
>
> There needs to be a generic way to get fgc$merges of the form 2) from 1). Hints please.

Well, how do you merge the unconnected components? I guess you could
come up with an order based on modularity, or just merge them in
arbitrary order. The following is from igraph 0.6, and I haven't tried
it on your data, but it might just work. It merges the individual
subtrees in arbitrary order.

complete.dend <- function(comm) {
  merges <- comm$merges
  if (nrow(merges) < comm$vcount-1) {
    miss <- seq_len(comm$vcount + nrow(merges))[-as.vector(merges)]
    miss <- c(miss, seq_len(length(miss)-2) + comm$vcount+nrow(merges))
    miss <- matrix(miss, byrow=TRUE, ncol=2)
    merges <- rbind(merges, miss)
  }
  storage.mode(merges) <- "integer"

  merges
}

Best,
Gabor

[...]



More information about the R-help mailing list