【问题标题】:How to colour the branches of an unrooted tree using a variable in R如何使用R中的变量为无根树的分支着色
【发布时间】:2020-06-02 17:43:00
【问题描述】:

我想从输入的单倍型数据生成无根的邻居加入树,然后根据变量为树的分支着色。 我正在使用包 Ape 和 ggtree。 单倍型和协变量(元数据)位于两个具有匹配样本名称的单独文件中。 我已经能够生成树木并通过变量为树木的尖端着色,但不能通过树枝。

使用模拟数据 -

# Packages
library('ggplot2')
library('ape')
library('phangorn')
library('dplyr')
library('ggtree')
library('phylobase')

# Generate haplotype dataframe
Sample <- c('Sample_A', 'Sample_B', 'Sample_C', 'Sample_D', 'Sample_E', 'Sample_F')
SNP_A <- c(0, 1, 1, 0, 1, 1)
SNP_B <- c(0, 1, 1, 0, 1, 1)
SNP_C <- c(0, 0, 1, 1, 1, 0)
SNP_D <- c(1, 1, 0, 0, 1, 0)
SNP_E <- c(0, 0, 1, 1, 0, 1)
SNP_F <- c(0, 0, 1, 1, 0, 1)
df = data.frame(Sample, SNP_A, SNP_B, SNP_C, SNP_D, SNP_E, SNP_F, row.names=c(1))
df

# Metadata
Factor_A <- c('a', 'a', 'b', 'c', 'a', 'b')
Factor_B <- c('d', 'e', 'd', 'd', 'e', 'd')
df2 = data.frame(Sample, Factor_A, Factor_B)
df2

# Generate Euclidian pairwise distance matrix
pdist = dist(as.matrix(df), method = "euclidean")

# Turn pairwise distance matrix into phylo via neighbour joining method
phylo_nj <- nj(pdist)

我可以在 Ape 中绘制树:

# Example tree plot using Ape
plot(unroot(phylo_nj),
     type="unrooted",
     cex=1,
     use.edge.length=TRUE,
     show.tip.label = TRUE,
     lab4ut="axial",
     edge.width=1.5)

我可以在 ggtree 中绘制树,通过颜色/形状将变量添加到尖端:

# Plotting in ggtree
mytree <- ggtree(phylo_nj, layout="equal_angle", size=0.5, linetype=1)
mytree

# Adding metadata variables to tree plot
mytree2 <- mytree %<+% df2 + geom_tippoint(aes(shape = Factor_A,
                                               colour = Factor_B),
                                               size = 9,
                                           alpha=0.7)
mytree2

但我不知道如何在 Ape 或 ggtree 中使分支由变量(而不是尖端)着色。我只想要终端分支的颜色,而不是树的所有线条。我的目标是显示两个(分类)变量 - 一个按分支颜色,一个按尖端的形状(或颜色)。我所追求的粗略版本如下图所示(Factor_A 由尖端形状编码(中性色如图所示),Factor_B 由分支颜色编码。

提前感谢您的帮助。

【问题讨论】:

    标签: r phylogeny ape-phylo ggtree


    【解决方案1】:

    您可以在使用ape::plot.phylo 绘制树之后使用函数ape::edges 对特定边缘进行着色,方法是通过指定开始/结束节点使边缘着色。

    ## Colouring the first edge with a red dashed line
    plot(unroot(phylo_nj), type = "unrooted")
    edges(7, 8, col = "red", lty = 2)
    

    或者您可以直接在ape::plot.phylo 函数中提供颜色向量:

    ## Making rainbow edges
    plot(unroot(phylo_nj), type = "unrooted", edge.color = rainbow(9))
    

    您可以使用 phylo 对象 (phylo_nj$edge) 中的边表找出要从数据框中着色的边。例如:

    ## Which labels have level "a"
    labels_a <- df2$Factor_A %in% "a"
    
    ## Which edges connect to these labels?
    edge_a <- phylo_nj$edge[,2] %in% match(phylo_nj$tip.label, df2$Sample[labels_a])
    
    ## Plotting the factors with the labels a coerced as numeric
    plot(unroot(phylo_nj), type = "unrooted", edge.color = c("blue", "orange")[edge_a+1])
    

    您当然可以按照此方法将其扩展到多个级别,以检测哪个边缘导致具有任何因子级别的提示。

    【讨论】:

    • 你的第一行 ("edges(7, 8, col = "red", lty = 2)") 给我一个错误信息:"Error in (function (classes, fdef, mtable) :无法为签名'“numeric”'”找到函数'edges'的继承方法。第二行的问题是我不清楚如何将颜色链接到元数据变量,例如通过Factor_A着色边缘和在我的示例代码中,Factor_B 的尖端形状。
    • 我已经更新了答案,展示了一个关于如何检测导致提示的边缘水平的示例。
    • 您好,感谢您的回复。我已经编辑了我的示例代码以包含第三类 Factor_A,因此它不能只是非此即彼。主要问题是分支颜色与输入数据不匹配 - 例如,df2 将 Sample_A、Sample_B 和 Sample_E 作为 Factor_A 的“a”。但是使用您的代码生成的绘图具有以橙色着色的 Sample_A、Sample_B 和 Sample_C 的分支,即不匹配。我尝试使用不同类别的 Factor_A 和 Factor_B,但分支始终与 df2 不匹配。解决该问题后,我需要处理多种颜色和尖端形状!
    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 2018-01-07
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多