[R-sig-eco] combine 2 plots - one network and one phylogenetic tree

Augusto Ribas ribas.aca at gmail.com
Wed Jun 20 20:35:00 CEST 2012


What i wanted is something like:
#Data
dados<-matrix(c(1,1,1,1,0,0,
                0,1,1,1,0,0,
                0,0,1,1,0,0,
                0,0,0,0,1,0,
                0,0,0,0,0,1,
                0,0,0,0,0,1),byrow=T,ncol=6,nrow=6,
                dimnames=list(paste("P",1:6,sep=""),paste("H",1:6,sep="")))
dados


library(igraph)
dados.network<-cbind(expand.grid(rownames(dados),colnames(dados)),Presence=c(dados[,]))
dados.network<-dados.network[which(dados.network$Presence==1),1:2]
dados.igraph<-graph.data.frame(dados.network)

library(ape)
host.tree<-rtree(6,rooted=TRUE,tip.label=paste("H",1:6,sep=""))

ordem<-as.numeric(as.factor(host.tree$tip.label))

#Plot
par(mfrow=c(2,1),mar=c(0,9.5,0,9.5))
plot(host.tree,use.edge.length=F,direction="downwards",show.tip.label=F)
par(mar=c(0,0,0,0))
plot(dados.igraph,layout=matrix(c(1:6,ordem,rep(c(1,2),each=6)),ncol=2,nrow=12),
vertex.color=c(rep("red",6),rep("green",6)),edge.arrow.size=0.2,vertex.size=20,
vertex.label=c(paste("P",1:6,sep=""),paste("H",1:6,sep="")))

I asked on the igraph list like suggest and got a hint that made me
work better on the layout.
Thanks for the help guys.


2012/6/20 Augusto Ribas <ribas.aca at gmail.com>:
> What i want is something like:
>
> #Take the order of the hosts from the tree
> order<-host.tree$tip.lab
>
> par(mfrow=c(2,1))
>
> #remove the host names from the tree to not repeat the network
> plot(host.tree,use.edge.length=F,direction="downwards",show.tip.label =F)
>
> #now plot the network, with the order of the tree
> plotweb(dados[,order],method = "normal")
>
> #but cant make the  H2 label of the tree in the exactly place of the
> #H2 of the network, and for all the Host species.
> #I dont know a strategy to make a plot this way.
> #The distance from H1 to H2 to H3 are variable in the network plot, i
> #would like someway to make them be equall, then maybe i could work the
> #mar parameter in par to make things plug right, i dont know.
>
> #something like this draw:
> http://oi47.tinypic.com/2j2szcy.jpg
>
> 2012/6/20 Alan Haynes <aghaynes at gmail.com>:
>> Sorry, for the multiple posting.
>>
>> Assuming you want what I think you want, you actually have everything in
>> your code, just in the wrong order and with the mfrow argument reversed:
>>
>>
>> library(igraph)
>> dados.network<-cbind(expand.grid(rownames(dados),colnames(dados)),Presence=c(dados[,]))
>> dados.network<-dados.network[which(dados.network$Presence==1),1:2]
>> dados.igraph<-graph.data.frame(dados.network)
>>
>>
>> library(ape)
>>
>> host.tree<-rtree(6,rooted=TRUE,tip.label=paste("H",1:6,sep=""))
>>
>> par(mfrow=c(1,2))
>>
>> plot(dados.igraph,layout=layout.fruchterman.reingold,
>>
>> vertex.color=c(rep("red",6),rep("green",6)),edge.arrow.size=0.2,vertex.size=20,
>>      vertex.label=c(paste("P",1:6,sep=""),paste("H",1:6,sep="")))
>> plot(host.tree,use.edge.length=F,direction="right")
>>
>>
>>
>> If you particularly want the graphic in portrait then you use tiff or jpeg
>> functions to generate a tall, narrow figure. Something like this
>>
>> tiff("filename.tiff", width=3, height = 6, units="in", res=600)
>>
>> par(mfrow=c(2,1))
>>
>> plot(dados.igraph,layout=layout.fruchterman.reingold,
>>
>> vertex.color=c(rep("red",6),rep("green",6)),edge.arrow.size=0.2,vertex.size=20,
>>      vertex.label=c(paste("P",1:6,sep=""),paste("H",1:6,sep="")))
>> plot(host.tree,use.edge.length=F,direction="right")
>>
>> dev.off()
>>
>>
>> Does that solve it?
>>
>>
>>
>> Alan
>>
>>
>> --------------------------------------------------
>> Email: aghaynes at gmail.com
>> Mobile: +41794385586
>> Skype: aghaynes
>>
>>
>> On 20 June 2012 16:23, Charles Novaes de Santana <charles.santana at gmail.com>
>> wrote:
>>>
>>> Hi Augusto,
>>>
>>> Which layout do you want to reproduce? The layout of the first network
>>> you plot?
>>>
>>> Do you know the "tkplot" function of igraph? You can plot the network
>>> using tkplot and then you can read the coordinates of the nodes into a
>>> variable. Like this:
>>>
>>>
>>> tkplot(dados.igraph,layout=layout.fruchterman.reingold,vertex.color=c(rep("red",6),rep("green",6)),edge.arrow.size=0.2,vertex.size=20,vertex.label=c(paste("P",1:6,sep=""),paste("H",1:6,sep="")));
>>> my_layout<-tkplot.getcoords(1);
>>>
>>> So you can use the variable my_layout (a matrix of coordinates) in
>>> other plots of network you want to:
>>>
>>> plot(dados.igraph, layout=my_layout)
>>>
>>> Sorry if it is not what you are looking for. Maybe if you write
>>> directly to the igraph-help mailing list you can have more luck. They
>>> use to answer the questions quickly:
>>>
>>>
>>> https://groups.google.com/forum/?fromgroups#!forum/network-analysis-with-igraph
>>>
>>> All the best,
>>>
>>> Charles
>>>
>>> On Wed, Jun 20, 2012 at 4:04 PM, Augusto Ribas <ribas.aca at gmail.com>
>>> wrote:
>>> > Hello.
>>> > I was using the package ipgraph to plot networks of parasites and hosts.
>>> > Then i though about plug a phylogeny tree on the plot. But I'm having
>>> > some
>>> > difficulties to do so.
>>> >
>>> > I'll give and CMR , hope someone can help.
>>> >
>>> > #I have data that look like this, 6 parasites ("P") and six host species
>>> > ("P")
>>> > dados<-matrix(c(1,1,1,1,0,0,
>>> >                0,1,1,1,0,0,
>>> >                0,0,1,1,0,0,
>>> >                0,0,0,0,1,0,
>>> >                0,0,0,0,0,1,
>>> >                0,0,0,0,0,1),byrow=T,ncol=6,nrow=6,
>>> >
>>> >  dimnames=list(paste("P",1:6,sep=""),paste("H",1:6,sep="")))
>>> > dados
>>> >
>>> > #Then i convert the data a way i can use with igraph packages
>>> > #Is there a more elegant way to change data like this anyway?
>>> >
>>> > library(igraph)
>>> >
>>> > dados.network<-cbind(expand.grid(rownames(dados),colnames(dados)),Presence=c(dados[,]))
>>> > dados.network<-dados.network[which(dados.network$Presence==1),1:2]
>>> > dados.igraph<-graph.data.frame(dados.network)
>>> >
>>> > plot(dados.igraph,layout=layout.fruchterman.reingold,
>>> >
>>> > vertex.color=c(rep("red",6),rep("green",6)),edge.arrow.size=0.2,vertex.size=20,
>>> > vertex.label=c(paste("P",1:6,sep=""),paste("H",1:6,sep="")))
>>> >
>>> > #now i would like to combine 2 plots,
>>> > #one phylogenetic tree of the hosts:
>>> >
>>> > library(ape)
>>> > host.tree<-rtree(6,rooted=TRUE,tip.label=paste("H",1:6,sep=""))
>>> >
>>> > #with the netwrok, but the network would have to had  a layout like in
>>> > package bipartite
>>> > library(bipartite)
>>> >
>>> > #something like this
>>> > par(mfrow=c(2,1))
>>> > plot(host.tree,use.edge.length=F,direction="downwards")
>>> > plotweb(dados)
>>> >
>>> >
>>> > Althoguht plotweb() do what i would like, in a pretty beautiful graph, i
>>> > could not make it do equidistant representation  for the host species,
>>> > nor
>>> > reproduce this layout with igraph.
>>> > If i could reproduce the layout on plot.igraph(), i think it would be
>>> > easy
>>> > to plug the tree on the network.
>>> > So anyone know if there is an easy way to do what i'm trying? I'm pretty
>>> > stuck here
>>> >
>>> > Thanks for your attention.
>>> >
>>> > Best wishes
>>> > Augusto Ribas
>>> >
>>> >
>>> > --
>>> > Grato
>>> > Augusto C. A. Ribas
>>> >
>>> > Site Pessoal: http://augustoribas.heliohost.org
>>> > Lattes: http://lattes.cnpq.br/7355685961127056
>>> >
>>> >        [[alternative HTML version deleted]]
>>> >
>>> > _______________________________________________
>>> > R-sig-ecology mailing list
>>> > R-sig-ecology at r-project.org
>>> > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>>>
>>>
>>>
>>> --
>>> Um axé! :)
>>>
>>> --
>>> Charles Novaes de Santana
>>> http://www.imedea.uib-csic.es/~charles
>>> PhD student - Global Change
>>> Laboratorio Internacional de Cambio Global
>>> Department of Global Change Research
>>> Instituto Mediterráneo de Estudios Avanzados(CSIC/UIB)
>>> Calle Miquel Marques 21, 07190
>>> Esporles - Islas Baleares - España
>>>
>>> Office phone - +34 971 610 896
>>> Cell phone - +34 660 207 940
>>>
>>> _______________________________________________
>>> R-sig-ecology mailing list
>>> R-sig-ecology at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>>
>>
>
>
>
> --
> Grato
> Augusto C. A. Ribas
>
> Site Pessoal: http://augustoribas.heliohost.org
> Lattes: http://lattes.cnpq.br/7355685961127056



-- 
Grato
Augusto C. A. Ribas

Site Pessoal: http://augustoribas.heliohost.org
Lattes: http://lattes.cnpq.br/7355685961127056



More information about the R-sig-ecology mailing list