liamrevell / phytools

GNU General Public License v3.0
198 stars 56 forks source link

Plot phylo.heatmap and paintBranches() tree in one plot? #33

Open HeatherKates opened 5 years ago

HeatherKates commented 5 years ago

Hello! Is it possible to plot a tree object with painted branches using phylo.heatmap? There's no option in the current phylo.heatmap function to color branches anything other than black, and colors= is passed to the heatmap plot. My code below with the attempt, obviously not functional, at bottom.

#Read tree
extree <- rtree(n = 20)
# Make data to paint branches by
tip_data <- (sample.int(3, 20, replace = TRUE))
tip_cols <- as.data.frame(cbind(extree$tip.label,tip_data))
b <- factor(tip_cols$tip_data)
names(b) <- tip_cols$V1
present <- names(b)[b=="1"]
absent <- names(b)[b=="2"]
unknown <- names(b)[b=="3"]
#paint branches
paintedtree <- paintBranches(extree,edge=sapply(present,match,extree$tip.label),
                    state="present",anc.state="new")
paintedtree <-paintBranches(paintedtree ,edge=sapply(absent,match,extree$tip.label),
                  state="absent")
paintedtree <-paintBranches(paintedtree ,edge=sapply(unknown,match,extree$tip.label),
                  state="unknown")
cols<-setNames(c("blue","orange","gray","black"),c("present","absent","unknown","new"))
#plot the tree with colored branches
plot(textree,colors=cols,lwd=1,fsize=1)
#Make data for a heatmap
char1 <- (sample.int(3, 20, replace = TRUE))
char2 <- (sample.int(3, 20, replace = TRUE))
heatmap <- as.data.frame(cbind(char1,char2))
row.names(heatmap) <- extree$tip.label
#plot the tree with the heatmap
phylo.heatmap(extree,heatmap,fsize=1,lwd=1)
#Try to plot the painted branch tree using phylo.heatmap (obviously coloring does not work)
phylo.heatmap(paintedtree,heatmap,fsize=1,lwd=1,colors = cols)
JGorzynski commented 4 years ago

This would be an in incredibly useful functionality to add - either coloured branches or coloured tip labels.