R: ggtree (三) facet_plot

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容