【问题标题】:How to combine phylogenetic tree and heatmap?如何结合系统发育树和热图?
【发布时间】:2019-10-07 18:14:43
【问题描述】:

我需要结合系统发育树和热图,所以我一直在尝试通过在 R 中使用 ggtree 和 phytools 包来做同样的事情。但是,我没有成功。 我的数据集如下,

((org1:0.03398193,org2:0.07721021)0.7400:0.00589058,org3:0.09199544,org4:0.09205519);
data.csv
x       x1  x2  x3  x4  x5
org1    50  20  40  70  50
org2    10  15  60  78  20  
org3    40  50  40  70  20
org4    80  50  40  20  30

以下教程采用的代码, http://www.randigriffin.com/2017/05/11/primate-phylogeny-ggtree.html 代码如下,

tree = read.tree(text = "org1:0.03398193,org2:0.07721021)0.7400:0.00589058,org3:0.09199544,org4:0.09205519);")
d <- data.frame(read.csv("data.csv"))
traits <- data.frame(d, fastBM(tree))
p8 <- ggtree(tree) +  xlim(0, 125) + geom_tiplab(size = 2, offset = 17)
p9 <- gheatmap(p8, traits, offset = 0.2, width = 0.2, low = "white", high = "black", colnames_position = "top", font.size = 2)

当我遵循相同的代码而不做任何更改时,它工作得非常好。但是,当我尝试使用我的数据时,它显示错误。我不知道,如何使用 fastBM 将我的树与数据文件结合起来。我想我应该使用其他功能,而不是 fastBM。请帮我做同样的事情。

【问题讨论】:

    标签: r heatmap phylogeny ggtree


    【解决方案1】:

    我找到了解决方案,你应该把 Randi Griffin 在他的帖子中提出的所有论点都收起来,确实这让你的表述有误。

    您将需要这些软件包:

    library(ape)
    library(dplyr)
    library(phytools)
    
    # install from Bioconductor
    source("https://bioconductor.org/biocLite.R")
    biocLite("ggtree")
    
    library(ggtree)
    
    
    tree <- read.tree(text = "(org1:1, org2:1, (org3:0.15, org4:0.15):0.8500);")
    traits <- data.frame(fastBM(tree, nsim=5))
    
    plot(tree)
    
    p8 <- ggtree(tree) +
      geom_tiplab(size=2) 
    
    # add heatmap
    p9 <-  gheatmap(p8,
                    traits,
                    offset=0.2, low="white", high="black", colnames_position = "top", font.size=2)
    
    p9
    

    此建议适用于具有相同分支长度的树。但它也适用于不同的长度。事实上,在他的示例中,他输入了xlim(0, 125),因为他的边长最多为 40,但在您的示例中,边长更小。因此,最好让 R 函数为您的图表找到最佳限制。

    您的以下格式“,”的数据将更容易使用 R 导入:

    x,x1,x2,x3,x4,x5
    org1,50,20,40,70,50
    org2,10,15,60,78,20  
    org3,40,50,40,70,20
    org4,80,50,40,20,30
    

    此代码用于导入数据:

    df <- read_csv("stack.txt")
    df <- as.data.frame(df)
    rownames(df) <- df$x
    df <- df[,-1]
    

    您只需将traits 更改为df,因为您有正确的行和列名称。

    p8 <- ggtree(tree) +
      geom_tiplab(size=2) 
    
    
    # add heatmap
    p9 <-  gheatmap(p8,
                    df,
                    offset=0.2, low="white", high="black", colnames_position = "top", font.size=2)
    
    p9
    

    您可以查看以下地址的 ape 包小插图,了解如何手动构建,就像您想要 R 中的 data.tree 对象一样:

    https://cran.r-project.org/web/packages/ape/index.html

    它将帮助您完成其余的工作,它还将帮助我们为您提供帮助。

    您的问题代码暂时不正确,我们无法知道您想要什么树。

    【讨论】:

    • 非常感谢雷米·库洛。它工作得很好。但是,热图没有反映 data.csv/stack.csv 值。
    • 热图不反映 data.csv/stack.csv 值,而是反映其他内容。
    • 谢谢 Remi,刚才我已经改进了我的树文件代码。没事吧。
    • 很高兴能为您提供帮助。
    • 感谢 Remi 的宝贵时间和帮助。效果很棒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多