Mammoth
Mammoth

Reputation: 179

How to consistently plot a tree after removing tips?

Imagine I have a tree (or dendrogram)

require(ape)
fulltree <- rtree(n=50, br=NULL)

...and then I remove some tips

prunetree <- drop.tip(fulltree,node=5)

If I plot the pruned tree, R rescales it so that only those tips remaining are considered.

par(mfrow=c(1,2))
plot(fulltree, type="fan")
plot(prunetree, type="fan")

But this makes it really hard to tell what part of the tree is now missing.

Is there a simple way that I can plot the pruned tree in the same scale/arrangement/etc. as the complete tree so that none of the remaining branches appear to move around? (In this example, I would get some kind of pac-man shape rather than a full circle) I'm thinking this could be done by coloring branches white or light grey. It would be really useful if someone wanted to animate a tree that was losing tips.

Upvotes: 3

Views: 866

Answers (2)

Mammoth
Mammoth

Reputation: 179

Thanks jeremycg for the ggtree tip. I think this was more what I was looking for.

require(ape)
library(ggtree)
library(gridExtra)
library(ggplot2)

set.seed(1234)

fulltree <- rtree(n=50, br=NULL)

#These are the tips to drop
prunetips <- c("t41","t44","t42","t8")

#But get the tips to keep
keeptips <- fulltree$tip.label[!fulltree$tip.label %in% prunetips]

#Group the tips to keep
prunetree <- groupOTU(fulltree, focus=keeptips)

#And plot
ggtree(prunetree, layout="fan", aes(color=group))+
scale_color_manual(values=c("lightgrey","black"))+
geom_tiplab()

Tree with pruned tips

Upvotes: 1

jeremycg
jeremycg

Reputation: 24945

The problem with this, is as you stated, the data is removed from the new tree so it is rescaled. To fix this, you might be better off plotting the tree with a new color for the desired tip(s).

We can do this using the excellent package ggtree (amongst other methods):

set.seed(1234)

library(ggtree)
library(gridExtra)

fulltree <- rtree(n=10, br=NULL)
col <- rep(1, 2*fulltree$Nnode + 1)
col[5] <- 10

grid.arrange(ggtree(fulltree, layout = "fan") + geom_text(aes(label=label)),
             ggtree(fulltree, col = col, layout = "circular") + geom_text(aes(label=label)))

enter image description here

The actual coloring comes from the col[5] <- 20: change the col[5] to your desired dropped tip, and the 20 to your desired colour.

Upvotes: 1

Related Questions