Re: [R-sig-phylo] Adding a custom legend underneath a phylo plot with coloured tips

2016-11-18 Thread Gabriel Yedid
This is what finally worked for me for setting up the plot with the
legend how I wanted:
(result is in attached PDF)

Perhaps some of you will find it useful for your own work...

#Zeileis et al.'s function for drawing palettes
pal <- function(col, border = "light gray", ...)
{
  n <- length(col)
  plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1), axes = FALSE,
xlab = "", ylab = "", ...)
  rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}


library(phytools)
library(colorspace)

setwd("C:/EXTRACT_LEAF_TRAITS/TRAITVAR_RESULTS/")

MYTREE = read.nexus("s4970_t500_1.nex")
MYDATA = read.csv("s4970_t500_1.csv")

MYCOLRANGE = read.table("colorrange2.txt",header=F,sep="\t")
MCRD = dim(MYCOLRANGE)
#PAL = diverge_hcl(MCRD[1],h=c(246,40),c=96)
#PAL = heat_hcl(MCRD[1],c=c(80,30),l=c(30,90),power=c(1/5,1.5))

#PAL = heat.colors(MCRD[1])
PAL = rainbow(MCRD[1])

MYCOLRANGE[,3] = PAL #to coerce palette values to character instead of factor

tips = getExtinct(MYTREE,tol=0.01)
neo_tree = drop.tip(MYTREE,tips)

EXT_TIP_TRAITS = MYDATA[which(MYDATA[,3]==T),]
TIPLABCOLS = vector(mode="character",length=dim(EXT_TIP_TRAITS)[1])

for (i in 1:length(neo_tree$tip.label))
{
  CurrTVal = EXT_TIP_TRAITS[i,2]
  #message(CurrTVal)
  for (j in 1:dim(MYCOLRANGE)[1]){
#message(MYCOLRANGE[j,1])
#message(MYCOLRANGE[j,2])
#message(MYCOLRANGE[j,3])
if ((CurrTVal >= MYCOLRANGE[j,1]) && (CurrTVal <= MYCOLRANGE[j,2])){
  #message(MYCOLRANGE[j,3])
  TIPLABCOLS[i] = MYCOLRANGE[j,3]
  break
}
  }
}

BINVALCHRVEC = vector(mode="character")

for (n in 1:dim(MYCOLRANGE)[1]) {

  BINVALCHR = paste(as.character(MYCOLRANGE[n,1])," - ",
as.character(MYCOLRANGE[n,2]),sep="")
  BINVALCHRVEC = c(BINVALCHRVEC,BINVALCHR)

}

colours = MYCOLRANGE[,3]


dev.off()
layout(rbind(1,2), heights=c(5.5,2.5))
layout.show(n=2)

par(mar=c(1, 0.25, 1, 0.25))
plot.phylo(neo_tree,type="phylogram",root.edge=TRUE, no.margin=FALSE,
show.tip.label=FALSE)
tiplabels(pch=22, col=TIPLABCOLS, bg=TIPLABCOLS, cex=0.3)

par(mar=c(0, 0, 0, 0))
# c(bottom, left, top, right)
plot.new()

legend("left", "groups", bty='n', legend=BINVALCHRVEC, ncol=6,
col=MYCOLRANGE[,3], bg = MYCOLRANGE[,3], pch =
rep(15,length(MYCOLRANGE[1:3])), cex=0.59)


cheers,

Gabe

On 10/28/16, Gabriel Yedid <gyedi...@gmail.com> wrote:
> Hello all,
>
> I have a phylo plot where each tip of the tree has a certain trait
> value associated with it.  The trait is represented by a
> continuously-valued number bounded by  x and y (2.0 and 15.0 for these
> purposes).  In order to make display tractable, the values are binned,
> with each bin being assigned one colour from a sequential palette.
>
> I have been able to display the tree as I want, with each tip getting
> a single coloured square that represents its trait value (within the
> bin range), but now I want to create a custom legend and put it below
> the phylo plot.  I know how to divide the plot window to position the
> legend there, but not how to assemble and display the legend itself.
>
> What I want is to have a coloured square representing the bin, with
> text showing the bin's lower and upper limits to the right of the
> square,
>
> [ ]   bin_a_lower - bin_a_upper [ ]   bin_d_lower - bin_d_upper
> [ ]   bin_b_lower - bin_b_upper [ ]   bin_e_lower - bin_e_upper
> [ ]   bin_c_lower - bin_c_upper 
>
> Each of the [ ] would get a different colour specified by the hex
> codes in the palette's character vector.
>
> Does anybody know how to put the legend together from elements that
> would already be in the workspace? (the palette vector, the bin limit
> values, etc.)  Is there a way to do it with the default legend()
> function that I just don't know, or does it have to be assembled
> piece-by-piece?
>
> Alternatively, how would I get a colourbar to display in that same
> space below the plotted tree?
>
> cheers,
>
> Gabe
>


Rplot03.pdf
Description: Adobe PDF document
___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

[R-sig-phylo] Adding a custom legend underneath a phylo plot with coloured tips

2016-10-27 Thread Gabriel Yedid
Hello all,

I have a phylo plot where each tip of the tree has a certain trait
value associated with it.  The trait is represented by a
continuously-valued number bounded by  x and y (2.0 and 15.0 for these
purposes).  In order to make display tractable, the values are binned,
with each bin being assigned one colour from a sequential palette.

I have been able to display the tree as I want, with each tip getting
a single coloured square that represents its trait value (within the
bin range), but now I want to create a custom legend and put it below
the phylo plot.  I know how to divide the plot window to position the
legend there, but not how to assemble and display the legend itself.

What I want is to have a coloured square representing the bin, with
text showing the bin's lower and upper limits to the right of the
square,

[ ]   bin_a_lower - bin_a_upper [ ]   bin_d_lower - bin_d_upper
[ ]   bin_b_lower - bin_b_upper [ ]   bin_e_lower - bin_e_upper
[ ]   bin_c_lower - bin_c_upper 

Each of the [ ] would get a different colour specified by the hex
codes in the palette's character vector.

Does anybody know how to put the legend together from elements that
would already be in the workspace? (the palette vector, the bin limit
values, etc.)  Is there a way to do it with the default legend()
function that I just don't know, or does it have to be assembled
piece-by-piece?

Alternatively, how would I get a colourbar to display in that same
space below the plotted tree?

cheers,

Gabe

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/


[R-sig-phylo] Implementation of Mir et al.'s (2013) tree balance index?

2015-12-09 Thread Gabriel Yedid
Hello all,

Is there any R implementation yet of the tree balance metric described in:


Mir, Arnau, Francesc Rosselló, and Lidia Rotger. "A new balance index
for phylogenetic trees." Mathematical biosciences 241.1 (2013):
125-136.


cheers,

Gabe

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

[R-sig-phylo] Problem loading phytools on Mac

2014-12-22 Thread Gabriel Yedid
Hello list,

Has anyone else experienced the following error when trying to load
phytools on a Mac, even though it seems to have been successfully
installed?  Note that this problem doesn't seem to be with phytools
itself, but with rgl:

library(phytools)
 Loading required package: ape
Loading required package: maps
Error : .onLoad failed in loadNamespace() for 'rgl', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object
'/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rgl/libs/rgl.so':
  
dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rgl/libs/rgl.so,
6): Library not loaded: /opt/X11/lib/libGLU.1.dylib
  Referenced from:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rgl/libs/rgl.so
  Reason: image not found
Error: package or namespace load failed for ‘phytools’‍



I haven't experienced this problem using phytools on a PC.  Anybody
know what gives here?

cheers,

Gabe

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

[R-sig-phylo] Problem processing trees with more nodes than tips...

2014-11-12 Thread Gabriel Yedid
Hello all,

I'm having a curious problem in that I have trees that seem to have
more nodes than tips.  They're part of a time series of trees (not
generated with an R package) that I'm reading in and doing tree shape
analyses of.  Not all of the trees have this problem, but once it
crops up it persists in all trees generated after that point.

The tree file (in Nexus format) is read in OK, but when I try to plot it I see:

plot(INTREE, show.tip.label=FALSE)
Error in plot.phylo(INTREE, show.tip.label = FALSE) :
  tree badly conformed; cannot plot. Check the edge matrix.
summary(INTREE)

Phylogenetic tree: INTREE

  Number of tips: 5796
  Number of nodes: 5797
  Branch lengths:
mean: 9.617864
variance: 149.9489
distribution summary:
   Min. 1st Qu.  Median 3rd Qu.Max.
  0.000   2.480   6.213  12.740 188.200
  Root edge: 17.5434
  First ten tip labels: taxa_1000
taxa_10001
taxa_10002
taxa_10003
taxa_10004
taxa_10005
taxa_10006
taxa_10008
taxa_10009
taxa_10019
  No node labels.

And when I try to drop all of the extinct tips of the tree, all tips
are dropped!

What could be causing this behaviour?  How can it be fixed (in R)?

cheers,

Gabe

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/


Re: [R-sig-phylo] Problem processing trees with more nodes than tips...

2014-11-12 Thread Gabriel Yedid
OK, I think I've figured out what the trouble is, it's a problem with
the way the input Nexus files are being written.  The trouble is with
the data in the translation table.  If I extract the Newick tree
structure by itself, without the translation labels, the tree can be
read and processed normally (though of course the tip labels are
anonymous now).

Is there some way of getting read.nexus() to ignore the translation table?

cheers,

Gabe


On Wed, Nov 12, 2014 at 7:03 PM, Gabriel Yedid gyedi...@gmail.com wrote:
 Hello all,

 I'm having a curious problem in that I have trees that seem to have
 more nodes than tips.  They're part of a time series of trees (not
 generated with an R package) that I'm reading in and doing tree shape
 analyses of.  Not all of the trees have this problem, but once it
 crops up it persists in all trees generated after that point.

 The tree file (in Nexus format) is read in OK, but when I try to plot it I 
 see:

plot(INTREE, show.tip.label=FALSE)
 Error in plot.phylo(INTREE, show.tip.label = FALSE) :
   tree badly conformed; cannot plot. Check the edge matrix.
summary(INTREE)

 Phylogenetic tree: INTREE

   Number of tips: 5796
   Number of nodes: 5797
   Branch lengths:
 mean: 9.617864
 variance: 149.9489
 distribution summary:
Min. 1st Qu.  Median 3rd Qu.Max.
   0.000   2.480   6.213  12.740 188.200
   Root edge: 17.5434
   First ten tip labels: taxa_1000
 taxa_10001
 taxa_10002
 taxa_10003
 taxa_10004
 taxa_10005
 taxa_10006
 taxa_10008
 taxa_10009
 taxa_10019
   No node labels.

 And when I try to drop all of the extinct tips of the tree, all tips
 are dropped!

 What could be causing this behaviour?  How can it be fixed (in R)?

 cheers,

 Gabe

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/


[R-sig-phylo] problem dropping extinct taxa from large tree

2014-09-30 Thread Gabriel Yedid
Hello all,

I am trying to drop extinct taxa from a large phylogeny generated with
a non-R-based tree simulation program.  The tree was saved in a Nexus
file which was read in with read.nexus.  It's quite large--10911
leaves, but only 2055 are alive i.e. lineages that survived to the
end of the simulation.  It's these lineages I want to keep around for
further analysis.

I've tried both the drop.fossil (from APE) and drop.extinct(from
geiger) functions, but both of them give problems.  If I use the
customary low tolerance for either of these functions, drop.fossil
produces the following error:

 INTREE_EXTANT = drop.fossil(INTREE, tol=1e-6)
Error in xmat[, 1] : incorrect number of dimensions

Lowering the tolerance value to 1e-4 trims off some of the living tips
I wanted kept (the resulting tree is smaller than it's supposed to
be).

drop.extinct just seems to hang and doesn't return even after about 10
minutes, no matter what tolerance value I supply.

Is there some other way I can remove extinct lineages from a large
tree like this?

cheers,

Gabe

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/


[R-sig-phylo] Meaning of output from phytools findMRCA ?

2014-01-28 Thread Gabriel Yedid
Hi all,

I'm trying to use picante and phytools in combination to analyze particular
aspects of communities of digital organisms.  I use picante to read in the
data for community, trait, and phylogeny data, and make sure everything is
matched up properly.  My interest in using phytools (at least right now) is
using the findMRCA function to find the MRCA of groups of organisms with
particular phenotypic traits.

The data look like this:

 TRAIT1 TRAIT2 TRAIT3 TRAIT4 TRAIT5 TRAIT6 TRAIT7 TRAIT8 TRAIT9
F3666295  0167  0 83  0 81  0  0  0
F3664184  0165  0 83  0 82  0  0  0
F3665912121 42  0 41  0  0  0  0  0
F3665920  0165  0 83  0 81  0  0  0
F3654948  0  0  0 82  0 80  0  0  0
F3663933 81 82  0 82  0 80  0  0  0
 TRAIT10 PHENO
F3666295 15.376584
F3664184 15.186084
F3665912 14.709322
F3665920 15.209384
F3654948 15.023380
F3663933 14.872186


The row names are the unique IDs of the genotypes in the population.
Traits 1 through 10 are traits reflecting various aspects of the phenotype;
the trait called PHENO is a numerical summary of traits 1-9.  It's this
that I want to use as the index for finding MRCAs (e.g. find the MRCA of
all organisms with PHENO = 84)

After having read in and cleaned up everything with picante, I

  attach(TRAITS.EDITED.MATCHED.WORK$data)  #this is the object of
matched-up tree + data produced by picante
  #names(TRAITS.EDITED.MATCHED.WORK$data)

   SUBJECT = TRAITS.EDITED.MATCHED.WORK$data[PHENO==84,] #just for example,
this extracts the vector of tip labels needed by findMRCA

  findMRCA(TRAITS.EDITED.MATCHED.WORK$phy,tips=
row.names(SUBJECT),type=node)
  findMRCA(TRAITS.EDITED.MATCHED.WORK$phy,tips=
row.names(SUBJECT),type=height)

This produces output like:

   findMRCA(TRAITS.EDITED.MATCHED.WORK$phy,tips= 
 row.names(SUBJECT),type=node)[1] 1322   
 findMRCA(TRAITS.EDITED.MATCHED.WORK$phy,tips= 
 row.names(SUBJECT),type=height)[1] 73681


What exactly is the meaning of these numbers?  The vector of node labels
for the associated tree doesn't have that many elements, so what does node
= 1322 mean?  Is it actually meaningful, or is this some kind of error?
What I really want, in this case, is the name of the node, which has a
unique genotype ID like those shown above.

And is the node height measured relative to the root, or to the tips?

Any help is much appreciated!

cheers,

Gabe

[[alternative HTML version deleted]]

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/


[R-sig-phylo] ATTN Liam Revell: please contact me off-list

2013-10-03 Thread Gabriel Yedid
This is a shout-out to Liam Revell:  I've been trying to contact you
off-list but I'm not sure if my messages have been getting through.  Please
contact me at gyedi...@gmail.com.

We now return you to your regularly scheduled programming...

cheers,

Gabe

[[alternative HTML version deleted]]

___
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/