简体   繁体   中英

add taxa manually to phylogenetic tree in R

I have a phylogenetic tree in R and currently the variables are the taxa_names. I want to assign taxa_names from a data.frame (the tree is based on said data.frame).

 matrix <- data.frame(row.names = c('aa','bb','cc'),aa=c(0,1,1),bb=c(1,1,0),cc=c(0,1,1))
> matrix
   aa bb cc
aa  0  1  0
bb  1  1  1
cc  1  0  1

d.matrix <- dist(matrix)
h.matrix <- hclust(d.matrix) %>% ape::as.phylo(.)

The taxa names are aa,bb,cc . But I want to set the taxa_names to t and a . These letters are from the data.frame dd .

dd <- data.frame(row.names = c('aa','bb','cc'),values = c('t','a','a'))
> dd
   values
aa      t
bb      a
cc      a

dd actually has a lot more columns but I cut it short here.

How to manually set taxa_names for a tree in R? The desired taxa_names are specyfied in a data.frame

EDIT: I tried taxa_names <- c() and AssignTaxonomy() (from dada2 package) without success

First, I find it's good practice to read your data as character instead of factor (the default for data.frames). This can be done by using the stringsAsFactors argument:

dd <- data.frame(row.names = c('aa','bb','cc'),values = c('t','a','a'), stringsAsFactors = FALSE)

Then you can just assign the name directly to the specific entry of the phylo object (you can reorder as you want it):

h.matrix$tip.label = dd$values

To know more about the phylo class check https://www.rdocumentation.org/packages/ape/versions/2.6-3/topics/read.tree

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM