Note!
Codes are from: https://bioconductor.org/packages/devel/bioc/vignettes/ggtree/inst/doc/treeVisualization.html
library("ggtree")
## Loading required package: ggplot2
## Loading required package: treeio
## ggtree v1.10.0 For help: https://guangchuangyu.github.io/ggtree
##
## If you use ggtree in published research, please cite:
## Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36, doi:10.1111/2041-210X.12628
You can basicly create a tree:
tr <- "((Pan_paniscus,Pan_troglodytes),((Homo_sapiens,Homo_erectus),Homo_abilis));"
tr <- read.tree(text = tr)
plot(tr)
Create some node labels for them:
tr <- makeNodeLabel(tr, "u", nodeList = list(Pan = "Pan", Homo = "Homo"))
plot(tr, show.node.label = TRUE)
You can add another label and you can adjust node names:
tr <- makeNodeLabel(tr, "u", nodeList = list(Hominid = c("Pan","Homo")))
plot(tr, show.node.label = TRUE, label.offset = 0.2, edge.color = "blue", cex = 1)
You can
mycol <- c("blue", "blue", "red", "red", "red")
tiplabels(pch=21, col="black", adj=0.5, bg=mycol, cex=1)
Assume that you have a “big” phylogenetic tree. E.g:
data("chiroptera")
str(chiroptera)
plot(chiroptera)
Zoom on a portion of tree. E.g: Plecotus
gzoom(chiroptera, grep("Plecotus", chiroptera$tip.label))
make things a little bit colorful:
groupInfo <- split(chiroptera$tip.label, gsub("_\\w+", "", chiroptera$tip.label))
chiroptera <- groupOTU(chiroptera, groupInfo)
p <- ggtree(chiroptera, aes(color=group)) + geom_tiplab() + xlim(NA, 23)
gzoom(p, grep("Plecotus", chiroptera$tip.label), xmax_adjust=2)
Annotate clades:
set.seed(2015-12-21)
tree = rtree(30)
p <- ggtree(tree) + xlim(NA,5)
p+geom_cladelabel(node=45, label="test label") +
geom_cladelabel(node=34, label="another clade")
align and color
p+geom_cladelabel(node=45, label="test label", align=T, color='red') +
geom_cladelabel(node=34, label="another clade", align=T, color='blue')
Colorful clades
call a new sample tree:
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
ggtree(tree)
check the nodes:
ggtree(tree) + geom_text2(aes(subset=!isTip, label=node), hjust=-.3) + geom_tiplab()
indicate node 20 or another else: for eg:16
tree <- groupClade(tree, node=20)
ggtree(tree, aes(color=group, linetype=group)) + geom_tiplab()
grouping OTUs
tree <- groupOTU(tree, focus=c("A","B","C","D"))
ggtree(tree, aes(color=group)) + geom_tiplab()
Interactive annotation
set.seed(123)
tr1 <- rtree(60)
p <- ggtree(tr1, size=2)
print(p)
cols <- rainbow(5)
for (i in 1:5) {
p <- p + geom_balance(identify(p), fill=cols[i])
print(p)
}
for (i in 1:5) {
p <- p + geom_hilight(identify(p), fill=cols[i])
print(p)
}
Also you can use groupOTU function to whole taxa classification. So, you can create a colorful tree.
groupInfo <- split(chiroptera$tip.label, gsub("_\\w+", chiroptera$tip.label))
chiroptera <- groupOTU(chiroptera, groupInfo)
ggtree(chiroptera, aes(color=group), layout = "circular") +
geom_tiplab(size=1, aes(angle=angle))
Visualize tree with associated matrix:
beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
beast_tree <- read.beast(beast_file)
genotype_file <- system.file("examples/Genotype.txt", package="ggtree")
genotype <- read.table(genotype_file, sep="\t", stringsAsFactor=F)
colnames(genotype) <- sub("\\.$", "", colnames(genotype))
p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_treescale(x=2008, y=1, offset=2)
p <- p + geom_tiplab(size=2)
gheatmap(p, genotype, offset = 5, width=0.5, font.size=3, colnames_angle=-45, hjust=0) +
scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), values=c("steelblue", "firebrick", "darkgreen"))
fasta <- system.file("examples/FluA_H3_AA.fas", package="ggtree")
msaplot(ggtree(beast_tree), fasta)
msaplot(ggtree(beast_tree), fasta, window=c(150, 200)) + coord_polar(theta='y')
Tree annotation with Phylopic
biocLite("EBImage")
pp <- ggtree(tree) %>% phylopic("79ad5f09-cf21-4c89-8e7d-0c82a00ce728", color="steelblue", alpha = .3)
print(pp)
pp %>% phylopic("79ad5f09-cf21-4c89-8e7d-0c82a00ce728", color="#86B875", alpha=.8, node=4) %>%