ggtree是ggplot2的拓展包,主要用来画进化树,而且还能对进化树进行美化,绘制一些非常精美的发表级图片。ggtree也是Y叔(余老师)的作品,虽然没有clusterProfiler广为人知,但是ggtree使用也颇为广泛,是一个绘图利器,值得学习。

下面简单介绍ggtree的安装及使用:

1)安装


# 安装
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("ggtree")

2)ggtree简单介绍

ggtree可用的树的绘制布局有很多主要有下面几种:


# 官方链接:https://yulab-smu.top/treedata-book/chapter4.html#tree-layouts
library(ggtree)
set.seed(2017-02-16)
tree <- rtree(50)
ggtree(tree)
ggtree(tree, layout="roundrect")
ggtree(tree, layout="slanted")
ggtree(tree, layout="ellipse")
ggtree(tree, layout="circular")
ggtree(tree, layout="fan", open.angle=120)
ggtree(tree, layout="equal_angle")
ggtree(tree, layout="daylight")
ggtree(tree, branch.length='none')
ggtree(tree, layout="ellipse", branch.length="none")
ggtree(tree, branch.length='none', layout='circular')
ggtree(tree, layout="daylight", branch.length = 'none')
# 图片拼接可以用cowplot,如:plot_grid(p1, p2, p3, p4, ncol=4, labels=c("A","B", "C", "D"))

还有一些布局的变体,也可以自己设计:


# https://yulab-smu.top/treedata-book/chapter4.html#fig:layout2
ggtree(tree) + scale_x_reverse()
ggtree(tree) + coord_flip()
ggtree(tree) + layout_dendrogram()
ggplotify::as.ggplot(ggtree(x), angle=-30, scale=.9)
ggtree(tree, layout='slanted') + coord_flip()
ggtree(tree, layout='slanted', branch.length='none') + layout_dendrogram()
ggtree(tree, layout='circular') + xlim(-10, NA)
ggtree(tree) + layout_inward_circular()
ggtree(tree) + layout_inward_circular(xlim=15)

我们也可以对树加以美化,添加各种标注等:


library(ggtree)
set.seed(2015-12-21)
tree <- rtree(30)
p <- ggtree(tree) + xlim(NA, 6)

p + geom_cladelabel(node=45, label="test label") +
    geom_cladelabel(node=34, label="another clade")
p + geom_cladelabel(node=45, label="test label", align=TRUE,  offset = .2, color='red') +
    geom_cladelabel(node=34, label="another clade", align=TRUE, offset = .2, color='blue')

p + geom_cladelabel(node=45, label="test label", align=T, angle=270, hjust='center', offset.text=.5, barsize=1.5) +
    geom_cladelabel(node=34, label="another clade", align=T, angle=45, fontsize=8)
p + geom_cladelabel(node=34, label="another clade", align=T, geom='label', fill='lightblue')

还可以用geom_hilight高亮某一区域:


nwk <- system.file("extdata", "sample.nwk", package = "treeio")
tree <- read.tree(nwk)
ggtree(tree) + geom_hilight(node = 21, fill = "steelblue", alpha = .6) +
  geom_hilight(node = 17, fill = "darkgreen", alpha = .6)

ggtree(tree, layout = "circular") + geom_hilight(node = 21, fill = "steelblue", alpha =.6) +
  geom_hilight(node = 23, fill = "darkgreen", alpha = .6)

3)下面就用一个示例结束ggtree的介绍(对于ggplot2系列要把握一个核心:图层的展示,以及布局):


library(ggtree)

# 用hclust计算距离
hc <- hclust(dist(mtcars))
den <- as.dendrogram(hc)
# 树的分类树,此处为4
clus <- cutree(hc, 4)
g <- split(names(clus), clus)

# 绘制聚类树
p <- ggtree(hc, linetype = 'dashed')
clades <- sapply(g, function(n)
  MRCA(p, n))

p <-
  groupClade(p, clades, group_name = 'subtree') + aes(color = subtree)

# 标签数据
d <- data.frame(label = names(clus),
                cyl = mtcars[names(clus), "cyl"])

# %<+%是ggtree定义的一个方法
p %<+% d +
  layout_dendrogram() +
  geom_tippoint(
    size = 5,
    shape = 21,
    aes(fill = factor(cyl), x = x + .5),
    color = 'black'
  ) +
  geom_tiplab(aes(label = cyl),
              size = 3,
              hjust = .5,
              color = 'black') +
  geom_tiplab(
    angle = 120,
    hjust = 1,
    offset = -10,
    show.legend = F
  ) +
  scale_color_brewer(palette = 'Set2', breaks = 1:4) +
  theme_dendrogram(plot.margin = margin(6, 6, 80, 6)) +
  theme(legend.position = c(.9, .7))

参考资料:

1.https://github.com/GuangchuangYu/ggtree

2.https://yulab-smu.top/treedata-book/chapter9.html