1 read tree
library("ggtree")
library("ggplot2")
library("ggstance") # for geom_barh
library("reshape2") # for melt
tree = read.tree("taxon_fig.nwk")
tb = fortify(tree)

2 plot tree
2.1 配色
p_sort = c("Firmicutes",
"Proteobacteria",
"Actinobacteria",
"Bacteroidetes",
"Euryarchaeota",
"Synergistetes",
"Tenericutes",
"Deinococcus_Thermus",
"Fusobacteria",
"Spirochaetes",
"Acidobacteria",
"Cyanobacteria",
"Chlamydiae",
"Chlorobi",
"Deferribacteres",
"Planctomycetes",
"Thermodesulfobacteria",
"Verrucomicrobia")
col_list = read.table("C:/Users/hutongyuan/Desktop/group_color.list",sep="\t", check.names=F, na.string="", stringsAsFactors=F, quote="", comment.char="")
colors = col_list$V1[1:18]
names(colors) <- p_sort

2.2 輸入文件及處理
phylum = read.table("data_phylum.txt", header=T, sep="\t")
phylum$Phylum = factor(phylum$Phylum, levels=p_sort)

2.3 ggtree
base =
ggtree(tree, color="black", layout="rectangular",
size = 0.5) %<+% phylum +
geom_tippoint(aes(col = as.factor(Phylum)), size=6) +
theme(legend.title=element_text(face="bold", size=20), legend.position="right",
legend.text=element_text(size=15),
legend.key=element_rect(size=20)) +
labs(color = "Phylum") +
scale_color_manual(
values = colors)
ggsave(base, file="tree_base.pdf")

3 add box
3.1 輸入文件及處理
bgi = read.table("data_bgi.txt", header=T, sep="\t")
bgi = melt(bgi, id='Genus')

3.2 facet_plot geom_boxploth
add_box =
facet_plot(base, panel = "BGI cohort",
data = bgi,
geom_boxploth,
outlier.size = 0.02,
size = 0.2,
mapping = aes(x=value*100,
group = label,
color = Phylum))

4 add stackplot
需要的是bar圖(分組),但是需要mapping上色,自行構(gòu)造stackplot(把不需要的顏色值設(shè)為0即可)。分組不多手動excel即可,分組太多的話可以用dcast,不完全矩陣會有NA,NA值設(shè)為0即可。其實不用mapping也行,即在aes外用color/fill上色,但是需要明確的樹結(jié)構(gòu)信息,搞不清樹結(jié)構(gòu)誤入。
4.1 輸入數(shù)據(jù)及處理
group = read.table("data_group.txt", header=T, sep="\t")
group = melt(group, id='Genus')

4.1 facet_plot geom_barh
add_group =
facet_plot(add_box, panel = 'Source', data = group, geom = geom_barh,
aes(x = value, fill = variable),
width = 1,
stat='identity') +
theme_tree2() +
labs(fill="Source") +
scale_fill_manual(
values = c("Share" = "green",
"CGR2" = "indianred3",
"BGI_cohort" = "deepskyblue3"))

5 add bar
目前愚笨的辦法是用mapping match color,但是會有0值雜色。最簡法干脆不上色,最好的方法是解析樹結(jié)構(gòu),目前解析一般了吧,以后邂逅同問再捯飭。
5.1 輸入數(shù)據(jù)和處理
cgr2 = read.table("data_cgr2.txt", header=T, sep="\t")
我試圖解析樹結(jié)構(gòu),目前是錯了

5.2 facet_plot geom_barh
add_bar =
facet_plot(add_group, panel = 'CGR2', data = cgr2, geom = geom_barh,
aes(x = value*100, fill = variable), color = NA,
width = 1,
stat='identity') +
theme_tree2()

如圖見,下面的顏色對應(yīng)是錯的
清晰地認(rèn)識到自己R基本功的不足,
1 一個圖,即使是這里的組合組,只能由一套color_scale 一套fill_scale
2 facet_plot基本參數(shù)outlier.size還是驚擾的神架
3 width size 等基本參數(shù)不在庫中
此處,非常感謝Y叔(Prof. 余光創(chuàng))的答疑。
ggtree facet_plot 每個美圖狗必備技能:
facet_plot: a general solution to associate data with phylogenetic tree