导读
上一篇:R语言ggtree绘制进化树(一)
上一篇相关:Phylophlan(二)种间进化分析
tree文件和绘图mapping文件来源:Phylophlan(三)将新物种插入进化树
一、ggtree画树状图:长方型、加对齐线
关键参数:
%<+% map # 引入注释文件
layout="rectangular" # 长方型树状图
align=TRUE # 添加对齐虚线
col=Phylum # 以Phylum给虚线着色
legend.position="bottom" # legend至于底部
legend.box="horizontal" # legend水平放置
library(ggplot2)
library(ggtree)
tree=read.tree("all.tree.int.nwk")
data=fortify(tree)
map=read.table("mapping.txt", header=T, sep="\t")
# 长方形(线型)
gra=ggtree(tree, layout="rectangular", size=0.1) %<+% map +
# 树型、线粗细、末端颜色 + 注释信息
geom_tiplab(aes(label=NA, col=Phylum), hjust=-0.5, align=TRUE, linesize=0.1)+
# 注释、颜色、高度、对其、虚点大小
theme(legend.title=element_text(face="bold"), legend.position="bottom", legend.box="horizontal", legend.text=element_text(size=rel(0.5)))+
# 图例位置、文字大小
xlim(NA, max(data$x)*1.3)
pdf("tree_rectangular_line.pdf")
gra
dev.off()
打开绘图结果tree_rectangular_line.pdf,如下:
二、ggtree画树状图:长方型、末端着色
关键参数:
ggtree(aes(col=Phylum)) # 末端“枝”颜色
geom_tippoint(aes(color=Phylum)) # 末端“点”颜色
gra=ggtree(tree, aes(col=Phylum), layout="rectangular", size=0.1) %<+% map +
# 树型、线粗细、末端颜色 + 注释信息
geom_tippoint(aes(color=Phylum), size=0.1)+
# 端点颜色、大小
geom_tiplab(aes(label=NA), size=0.8)+
# 注释
theme(legend.title=element_text(face="bold"), legend.position="bottom", legend.box="horizontal", legend.text=element_text(size=rel(0.5)))+
# 图例位置、文字大小
xlim(NA, max(data$x)*1.3)
pdf("tree_rectangular_point.pdf")
gra
dev.off()
打开绘图结果tree_rectangular_point.pdf,如下:
三、ggtree画树状图:圆型、加对齐线
关键参数:
layout="circular" # 画圆型树状图
# 圆形(线型)
gra=ggtree(tree, layout="circular", size=0.1) %<+% map +
# 树型、线粗细、末端颜色 + 注释信息
geom_tiplab(aes(label=NA, col=Phylum), hjust=2, align=TRUE, linesize=0.1)+
# 注释、颜色、高度、对其、虚点大小
theme(legend.title=element_text(face="bold"), legend.position="bottom", legend.box="horizontal", legend.text=element_text(size=rel(0.5)))+
# 图例位置、文字大小
xlim(NA, max(data$x)*1.3)
pdf("tree_circular_line.pdf")
gra
dev.off()
打开绘图结果tree_circular_line.pdf,如下:
四、ggtree画树状图:长方型、末端着色
关键参数;
ggtree(aes(col=Phylum)) # 末端“枝”颜色
geom_tippoint(aes(color=Phylum)) # 末端“点”颜色
gra=ggtree(tree, aes(col=Phylum), layout="circular", size=0.1) %<+% map +
# 树型、线粗细、末端颜色 + 注释信息
geom_tippoint(aes(color=Phylum), size=0.1)+
# 端点颜色、大小
geom_tiplab(aes(label=NA, col=NA), size=0.8)+
# 注释、注释的颜色
theme(legend.title=element_text(face="bold"), legend.position="bottom", legend.box="horizontal", legend.text=element_text(size=rel(0.5)))+
# 图例位置、文字大小
xlim(NA, max(data$x)*1.3)
pdf("tree_circular_point.pdf")
gra
dev.off()
打开绘图结果tree_circular_point.pdf,如下:
五、ggtree画树状图:长方型、加对齐线、加注释
1 准备注释文件
# 制作注释文件
cd /home/cheng/huty/softwares/phylophlan/data
cat ppafull.tax.txt | sed 's/.p__/\t/g' | sed 's/.c__/\t/g' | sed 's/.g__/\t/g' | awk 'BEGIN{OFS="\t"}{print $1, $3, $5}' > tax.txt
# 绘图准备
touch tax_bin.txt
echo -e 'id\tPhylum\tSpecies' > tax_bin.txt
cat bin_taxonomy.txt | sed '1d' | sed 's/.p__/\t/' | sed 's/.c__/\t/' | awk '{printf("%s\t%s\t%s\n", $1, $3, $1)}' >> tax_bin.txt
cat /home/cheng/huty/softwares/phylophlan/data/tax.txt >> tax_bin.txt
2 绘图
# 绘制insert tree
library(ggplot2)
library(ggtree)
tree=read.tree("wm.insert.tree.int.nwk")
data=fortify(tree)
map=read.table("tax_bin.txt", header=T, sep="\t")
gra=ggtree(tree, layout="rectangular", size=0.1) %<+% map +
# 树型、线粗细、末端颜色 + 注释信息
geom_tiplab(aes(label=Species, col=Phylum), align=TRUE, size=0.18, linesize=0.05)+
# 注释、颜色、高度、对其、虚点大小
theme(legend.title=element_text(face="bold"), legend.position="bottom", legend.box="horizontal", legend.text=element_text(size=rel(0.5))) +
# 图例位置、文字大小
xlim(NA, max(data$x)*1.3)
ggsave(gra, filename="tree_rectangular.pdf", height=48, width=10)
六、ggtree + gheatmap组合图
library(ggplot2) # 加载ggplot2
library(ggtree) # 加载ggtree
tree=read.tree(list.files()[grepl("denovo.tree.nwk", list.files())]) # 读取nwk文件
map=read.table("map.txt", header=T, sep="\t", comment.char="")
data=fortify(tree)
abun = read.table("/home/cheng/client2/wangmeng/X101SC19061144-Z01-J041/Bin_all/Bin_quant/bin_abundance_table.txt", header=T, sep="\t", row.names=1)
abun = abun[, order(colnames(abun), decreasing=F)]
p = ggtree(tree, layout="rectangular", ladderize=FALSE, branch.length="none", size=0.8, aes(color = Phylum)) %<+% map +
# 树型、线粗细、末端颜色 + 注释信息
geom_tiplab(aes(col=Phylum), align=TRUE, size=2.5) +
# 注释、颜色、高度、对其、虚点大小
theme(legend.position = 'none')
ggsave(p, file="bin_pheatmap.pdf", width=8, height=8)
p2 = gheatmap(p, abun, offset = 0.6, width = 0.5,
font.size=3, low = "green", high = "red", color = "white",
colnames_level = colnames(abun),
colnames_angle=90, hjust=.90)
ggsave(p2, file="bin_pheatmap2.pdf", width=14)
七、ggtree + facet_plot柱形图
1 读树、丰度读取合并组
# 准备丰度数据
abun = read.table("/home/cheng/client2/wangmeng/X101SC19061144-Z01-J041/Bin_all/Bin_quant/bin_abundance_table.txt", header=T, sep="\t", row.names=1)
abun = abun[, order(colnames(abun), decreasing=F)]
# 合并列,样品组
df = abun[, c(1:7)]
k = 1
for(i in c(1, 4, 7, 10, 13, 16, 19))
{
# 每组求和后取平均数,放在最后一列
df[, k] = apply(abun[, c(i, i+1, i+2)], 1, sum)/3
# 最后一列重命名,用组名
colnames(df)[k] = substr(colnames(abun)[i], 1, 2)
k = k + 1
}
# tree
tree=read.tree("/media/cheng/disk3/Projects/wangmeng/X101SC19061144-Z01-J041/Bin_all/Bin_phylo/wm.insert/wm.insert.tree.int.nwk")
data=fortify(tree)
# 注释文件
anno = read.table("/media/cheng/disk3/Projects/wangmeng/X101SC19061144-Z01-J041/Bin_all/Bin_phylo/wm.insert/tax_bin.txt", header=1, sep="\t")
2 切割丰度表、切割树
# 制作丰度表
bin = read.table("list2.txt") ## 挑选bin
table = read.table("table2.txt", header=T, sep="\t", row.names=1)
## 挑选物种
## 0填充丰度
df2=df[rownames(df)%in%bin$V1,] # 提取bin丰度
df2=rbind(df2, table) # 列合并
df2$tax=rownames(df2) # 行名加到注释列
rownames(df2)=1:nrow(df2) # 重排行名
# 注释
anno2=anno[anno$Species%in%df2$tax,]
rownames(anno2) = 1:nrow(anno2)
# 切割丰度表:注释 + 丰度
anno_abun=merge(anno2, df2, by.x='Species', by.y='tax', all.x=T)
ck = data.frame(id=anno_abun$id, num=anno_abun$CK*10)
t1 = data.frame(id=anno_abun$id, num=anno_abun$T1*10)
t2 = data.frame(id=anno_abun$id, num=anno_abun$T2*10)
t3 = data.frame(id=anno_abun$id, num=anno_abun$T3*10)
t4 = data.frame(id=anno_abun$id, num=anno_abun$T4*10)
t5 = data.frame(id=anno_abun$id, num=anno_abun$T5*10)
t6 = data.frame(id=anno_abun$id, num=anno_abun$T6*10)
# 树的切割
# https://www.jianshu.com/p/e1d0a0fce5a2
# ggtree:::getSubtree(tree, data_sub$node)
sub = anno2$id
data_drop = data[!(data$label%in%sub),]
tree_sub = drop.tip(tree, data_drop$label)
data_sub=fortify(tree_sub)
# id 列放在首列
gra = ggtree(tree_sub, aes(color = Phylum),layout="rectangular", size=0.5) %<+% anno2 +
# 树型、线粗细、末端颜色 + 注释信息
geom_tiplab(aes(label=Species, color = Phylum), align=TRUE, size=4, linesize=1) +
# 注释、颜色、高度、对其、虚点大小
theme(legend.position = 'none') +
xlim(0, max(data$x)*2)
ggsave(gra, filename="task2_tree.pdf", height=10, width=10)
3 用facet_plot添加多个柱形图
library("ggstance")
# 需要ggstance提供barh函数
p <- facet_plot(gra, panel = 'CK', data = ck , geom = geom_barh, aes(x=num), fill="#db616a", color="#db616a", stat='identity') %>%
facet_plot(panel = 'T1', data = t1 , geom = geom_barh, aes(x = num), fill="#dbac61", color="#dbac61", stat='identity') %>%
facet_plot(panel = 'T2', data = t2 , geom = geom_barh, aes(x = num), fill="#6cdb61", color="#6cdb61", stat='identity') %>%
facet_plot(panel = 'T3', data = t3 , geom = geom_barh, aes(x = num), fill="#36bfa2", color="#36bfa2", stat='identity') %>%
facet_plot(panel = 'T4', data = t4 , geom = geom_barh, aes(x = num), fill="#00BFFF", color="#00BFFF", stat='identity') %>%
facet_plot(panel = 'T5', data = t5 , geom = geom_barh, aes(x = num), fill="#836FFF", color="#836FFF", stat='identity') %>%
facet_plot(panel = 'T6', data = t6 , geom = geom_barh, aes(x = num), fill="#EE00EE", color="#EE00EE", stat='identity') +
theme_tree2() +
scale_x_continuous(limits=c(0, 11), breaks = c(0, 5, 10)) +
labs(x="Avg(TPM) X 10") +
theme(legend.position="left", text=element_text(family="serif"))
ggsave(p, filename="task2_tree_bar.pdf", height=10, width=50, limitsize = FALSE)
# combine
hmo = map[, 3:ncol(map)]
rownames(hmo) = map$label
hmo_tmp = hmo
for(i in 1:nrow(hmo))
{
for(j in 1:ncol(hmo))
{
if(hmo[i, j] != 0){hmo_tmp[i, j] = 1} # 0/1
}
}
tmp = hmo_tmp
num = seq(10, 100, by=10)
for(i in 1:10)
{
tmp[,i] = as.character(tmp[,i]*num[i]) # (10, 100, by=10)
}
# base
gra=ggtree(data, color="black", layout="fan", size=1, open.angle=10) %<+% map +
geom_tippoint(aes(col=Phylum), size=5) +
theme(legend.title=element_text(face="bold", size=15), legend.position="right", legend.text=element_text(size=13)) +
theme(text=element_text(family="serif")) +
scale_color_manual(
values = c("Actinobacteriota" = "#33a02c",
"Bacteroidota" = "#EE1289",
"Desulfobacterota" = "#b2df8a",
"Firmicutes" = "#009ACD",
"Fusobacteriota" = "#EEAD0E",
"Proteobacteria" = "#EE6363",
"Synergistota" = "#EEB4B4",
"Verrucomicrobiota" = "#9F79EE"))
ggsave(gra, file="tree.pdf", height=30, width=30)
# gheatmap ten color
colours=c("white","#20B2AA","#DC143C","#0000FF","#9370DB","#1E90FF","#7CFC00","#FFFF00","#FF00FF","#FA8072","#7B68EE")
names(colours) <- seq(0, 100, by=10)
cazys = c("GH2","GH20","GH29","GH33","GH42","GH95","GH112","GH136","CBM32","CBM51")
p2 <- gheatmap(gra, tmp, offset = 0, width=0.5, font.size=10, color = NULL, hjust = -0.1, colnames_level=colnames(hmo), colnames_angle=-90, family="serif", legend_title="CAZyme") + scale_fill_manual(values=colours, breaks=seq(0, 100, by=10))
ggsave(p2, file="tree_heat2.pdf", height=30, width=30)
# base
gra=
ggtree(data, color="black", layout="fan", branch.length="none", size=0.5, open.angle=15) %<+% map +
geom_tippoint(aes(col=Phylum), size=5) +
theme(legend.title=element_text(face="bold", size=20), legend.position="right",
legend.text=element_text(size=15),
legend.key=element_rect(size=20)) +
theme(text=element_text(family="serif")) +
scale_color_manual(
values = c("Actinobacteriota" = "#33a02c",
"Bacteroidota" = "#EE1289",
"Desulfobacterota" = "#b2df8a",
"Firmicutes" = "#009ACD",
"Fusobacteriota" = "#EEAD0E",
"Proteobacteria" = "#EE6363",
"Synergistota" = "#EEB4B4",
"Verrucomicrobiota" = "#9F79EE"))
ggsave(gra, file="tree2.pdf", height=30, width=40)
##############################
## gheatmap two color
##############################
colours=c("white","#20B2AA","#20B2AA","#20B2AA","#20B2AA","#20B2AA","#20B2AA","#20B2AA","#20B2AA","#DC143C","#DC143C")
names(colours) <- seq(0, 100, by=10)
##
p3 <- gheatmap(gra, tmp, offset = 0, width=1, font.size=14, color = NULL, hjust = -0.1, colnames_level=colnames(hmo), colnames_angle=-90, family="serif", legend_title="CAZyme") + scale_fill_manual(values=colours, breaks=seq(0, 100, by=10))
ggsave(p3, file="tree_heat2.pdf", height=30, width=30)
旋转
gra2 = rotate_tree(gra, 90)
ggsave(gra2, file="enzyme_tree/tree2.pdf", height=30, width=30)
gheatmap参数:
# gheatmap参数:
offset = 0.6 # heat to tree 偏移
width = 0.5 # heat 宽度
low # 低值颜色
high # 高知颜色
color # cell border 颜色
colnames_level # 列名label
colnames_angle = 90 # 列名旋转度数
font.size = 0.1 # colname 大小
hjust=.90 # 列名位置调整
参考:
1 ggplot2版聚类物种丰度堆叠图
2 Two Methods for Mapping and Visualizing Associated Data on Phylogeny Using Ggtree
3 ggtree facet_plot gheatmap
4 用ggtree画进化树
5 R doc gheatmap
6 ggtree: 系统发育树(phylogenetic tree)可视化
7 plotTree-ggtree