[R] Nodes & edges with similarity matrix

Seth Falcon sfalcon at fhcrc.org
Wed Aug 29 06:31:27 CEST 2007


Hi Paul,

"H. Paul Benton" <hpbenton at scripps.edu> writes:
>     I have a matrix which gives me the similarity of each item to each
> other. I would like to turn this matrix into something like what they
> have in the graph package with the nodes and edges.
> http://cran.r-project.org/doc/packages/graph.pdf . However I cannot find
> a method to convert my matrix to an object that graph can use.
>
> my similarity matrix looks like:
>> sim[1:4,]
>                 a          b          c          d
> [a]  1.000000000  0.0223676  0.9723831  0.3943310
> [b]  0.325141612  1.0000000  0.9644216  0.5460461
> [c]  0.002109751  0.3426540  1.0000000  0.7080224
> [d]  0.250153137  0.1987485  0.7391222  1.0000000
>
> please don't get caught up with the numbers I simple made this to show.
> I have not produce the code yet to make my similitary matrix.
>
> Does anyone know a method to do this or do I have to write something. :(
> If I do any starter code :D jj. If I've read something wrong or
> misunderstood my apologies.

The matrix you have can be interpreted as an adjacency matrix where a
value of zero means no relationship and a non-zero value indicates an
edge between two nodes with edge weight determined by the value.

You can create a graph object like this:

library("graph")
g <- new("graphAM", adjMat=sim, values=list(weight=0))

The default is to create an undirected graph and in this case you must
provide a symmetric matrix.  Generally I would expect a similarity
matrix to be symmetric, but the similitary (sic) matrix you have above
is not.  In this case, you can use a directed graph:

g <- new("graphAM", adjMat=sim, values=list(weight=0),
         edgemode="directed")

To visualize your graph you can use the Rgraphviz package which will
allow you to do:

  plot(g)

You might also be interested in the RBGL package which provides many
powerful graph algorithms.

All of these packages are available via Bioconductor (no bio required)
and can be installed as:

   source("http://bioconductor.org/biocLite.R")
   biocLite(c("RBGL", "Rgraphviz"))

+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
BioC: http://bioconductor.org/
Blog: http://userprimary.net/user/



More information about the R-help mailing list