[BioC] How to use the same node layout for a set of graph plots using graph and Rgraphviz?
Rand, Hugh
rand at amgen.com
Mon Feb 4 22:02:19 CET 2008
Hi all,
Following up on Robert's idea, I have built a simple example of how to display several graphs that use the same node locations. The example is very bare-bones, and contains one bug (see below), but will suffice for simple cases. It is based on the release version of graph and Rgraphviz. The devel version (I am told) greatly improves the handling of attributes. I will send out a new version of this code once I have a bit more time to work with the current devel version of the libraries and make the code a bit less bare-boned (but that may take me a litte while).
Hugh
#Bug: The template graph that is used to display all the graphs is the union
# of all the given graphs. As such, if you are working with directed
# graphs, the template will not correctly display edge orientation.
#
#sessionInfo:
# R version 2.6.1 (2007-11-26)
# i386-pc-mingw32
#
# locale:
# English_United States.1252
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] Rgraphviz_1.16.0 graph_1.15.6
#
# loaded via a namespace (and not attached):
# [1] cluster_1.11.9 tools_2.6.1
#
#
#Define Functions
#
#plot a graph whose edges are all in a second graph (template)
plotGraphWithTemplate = function(g1,template,...){
e1 = edgeNames(g1);
e2 = edgeNames(template);
indexMatchingEdges = is.element(e2,e1);
for(i in which(!indexMatchingEdges)){
template at AgEdge[[i]]@color="transparent";
}
for(i in which(indexMatchingEdges)){
template at AgEdge[[i]]@color="blue";
}
plot(template,...);
}
#create a graph from the union of all graphs in a list
unionGraphList = function(aGraphList){
a = union(aGraphList[[1]],aGraphList[[2]]);
for(i in 3:length(aGraphList)){ a = union(a,aGraphList[[i]]); }
return(a);
}
#example to display a list of graphs with the vertices in the same location
exampleDemo = function(){
#build graphs and template for plotting
set.seed(123);
n = 6; #number of graphs
v = letters[1:8]; #vertex names
m = c(1:3); #food for randomGraph
p = .4; #more food for randomGraph
aGraphList = replicate(n,randomGraph(V=v,M=m,p=p)); #a list of graphs
aAll = unionGraphList(aGraphList); #all the edges
template = agopen(aAll,name="union"); #a 'template'
#show plots with and without a template
par(mfcol=c(3,4));
lapply(aGraphList,plot);
lapply(aGraphList,plotGraphWithTemplate,template=template);
}
#
#Run the Example
#
require(graph);
require(Rgraphviz);
exampleDemo();
-----Original Message-----
From: Robert Gentleman [mailto:rgentlem at fhcrc.org]
Sent: Wednesday, January 30, 2008 1:45 PM
To: Rand, Hugh
Subject: Re: [BioC] How to use the same node layout for a set of graph plots using graph and Rgraphviz?
life will be much easier in devel than release, we greatly improved the
handling of attributes and colors etc
Rand, Hugh wrote:
> Hi Robert,
>
> Thanks for the work-around. I will post an example when I get a nice example sorted out. Sorry about not running sessionInfo to provide version info.
>
> Hugh
>
> -----Original Message-----
> From: Robert Gentleman [mailto:rgentlem at fhcrc.org]
> Sent: Tuesday, January 29, 2008 9:22 AM
> To: Rand, Hugh
> Cc: bioconductor at stat.math.ethz.ch
> Subject: Re: [BioC] How to use the same node layout for a set of graph plots using graph and Rgraphviz?
>
> Hi Hugh,
> Sorry to say not easily. And sorry for the long delay in responding,
> I was trying a few things out, but solving this problem seems to require
> a bit of redesign.
>
> The only thing that might work, in a fairly straightforward fashion
> is, something like this:
>
> set.seed(123);
> V = letters[1:7];
> M = c(1:3);
> g1 = randomGraph(V,M,.5);
> g2 = randomGraph(V,M,.5);
> g3 = union(g1, g2)
>
> then layout g3, make a copy of it, set the edge color for g1 edges in
> one copy to the background and for g2 in the other copy.
>
> You did not say which version of Rgraphviz you are using, but there
> have been a lot of changes to the devel version to make setting node and
> edge attributes for rendering simpler.
>
> best wishes
> Robert
>
>
>
> Rand, Hugh wrote:
>> Hi,
>>
>> I am using the packages "graph" and "Rgraphviz". I am using them to display a
>> set of graphs which all have the same nodes, but different edges. To aid in the
>> visual comparison, it would be useful to use the same node layout for all of
>> the graphs. It would be fine to use the node layout from the first graph. It is
>> unclear (to me) how to do this using the layout engine used in Rgraphviz.
>>
>> A concrete example:
>>
>> library("graph");
>> library("Rgraphviz");
>> set.seed(123);
>> V = letters[1:7];
>> M = c(1:3);
>> g1 = randomGraph(V,M,.5);
>> g2 = randomGraph(V,M,.5);
>> par(mfrow=c(1,2));
>> plot(g1);
>> plot(g2);
>> # The question: How do I now display both g1 and g2 (as seperate plots),
>> # but with the nodes in the same place? I would be content with the node
>> # locations taken from the layout for g1.
>>
>> Thanks,
>>
>> Hugh Rand
>> rand at amgen.com
>>
>> [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> Bioconductor mailing list
>> Bioconductor at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/bioconductor
>> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
>>
>
> --
> Robert Gentleman, PhD
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M2-B876
> PO Box 19024
> Seattle, Washington 98109-1024
> 206-667-7700
> rgentlem at fhcrc.org
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
>
--
Robert Gentleman, PhD
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
PO Box 19024
Seattle, Washington 98109-1024
206-667-7700
rgentlem at fhcrc.org
More information about the Bioconductor
mailing list