【问题标题】:Heatmap with categorical variables and with phylogenetic tree in R具有分类变量和 R 中的系统发育树的热图
【发布时间】:2015-05-14 09:08:38
【问题描述】:

:)

我有一个问题,通过个人搜索没有找到任何答案。 我想用分类变量制作一个热图(有点像这个:heatmap-like plot, but for categorical variables),我想在左侧添加一个系统发育树(像这个:how to create a heatmap with a fixed external hierarchical cluster)。理想的情况是改编第二个,因为它看起来更漂亮! ;)

这是我的数据:

  • 一个newick格式的系统发育树,有3个物种,比方说:

    ((1,2),3);
    
  • 一个数据框:

    x<-c("species 1","species 2","species 3")
    y<-c("A","A","C")
    z<-c("A","B","A")
    df<- data.frame(x,y,z)
    

(其中 A、B 和 C 是分类变量,例如在我的情况下存在/不存在/重复基因)。

你知道怎么做吗?

非常感谢!


编辑:我希望能够选择热图中每个类别的颜色,而不是经典的渐变。假设 A=绿色,B=黄色,C=红色

【问题讨论】:

  • 你知道如何制作树状图吗?对于热图,您可以将分类值映射到数字并将其绘制为矩阵。
  • 是的,我已经做了树状图! mytree &lt;-read.tree("sometree.tre") #turn the phylo tree to a dendrogram object data(mytree) #This is already a phylo object hc &lt;- as.hclust(mytree) #Compulsory step as as.dendrogram doesn't have a method for phylo objects. dend &lt;- as.dendrogram(hc) plot(dend, horiz=TRUE)
  • 但是如果我映射到数值,我可以为每个类别手动选择颜色吗?
  • 当然,您可以选择自定义颜色(有很多关于如何做到这一点的选项)。

标签: r heatmap categorical-data phylogeny


【解决方案1】:

我正在尝试使用与您相同的语法和 R 包 ape、gplots 和 RColorsBrewer 来制作一个热图,其列树状图本质上是一个物种树。

但我无法继续阅读我的 tre 文件。尝试对读入的树文件执行以下任何操作时会出现各种错误: a) 情节,或 b) 计算.brlen,和 c) 在collapse.singles 之后,情节在物种树拓扑方面看起来完全被破坏了

我怀疑我的 tre 输入有问题,但不确定是什么。你会碰巧明白什么是错的,我该如何解决它?谢谢!

((((((((((((Mt3.5v5, Mt4.0v1), 汽车), (((Pvu186, Pvu218), (Gma109, Gma189))), Cca))), (( (Ppe139, Mdo196), Fve226), Csa122)), (((((((Ath167, Aly107), Cru183), (Bra197, Tha173)), Cpa113), (Gra221, Tca233)), (Csi154, ( Ccl165, Ccl182))), ((Mes147, Rco119),(Lus200, (Ptr156, Ptr210)))), Egr201)), Vvi145), ((Stu206, Sly225), Mgu140)), Aco195), ((( Sbi79, Zma181),(Sit164, Pvi202)), (Osa193, Bdi192))), Smo91), Ppa152), (((Cre169, Vca199), Csu227), ((Mpu228, Mpu229), Olu231)));

【讨论】:

    【解决方案2】:

    其实是我自己想出来的。对于那些感兴趣的人,这是我的脚本:

    #load packages
    library("ape")
    library(gplots)
    
    #retrieve tree in newick format with three species
    mytree <- read.tree("sometreewith3species.tre")
    mytree_brlen <- compute.brlen(mytree, method="Grafen") #so that branches have all same length
    
    
    #turn the phylo tree to a dendrogram object
    hc <- as.hclust(mytree_brlen) #Compulsory step as as.dendrogram doesn't have a     method for phylo objects.
    dend <- as.dendrogram(hc)
    plot(dend, horiz=TRUE) #check dendrogram face
    
    #create a matrix with values of each category for each species
    a<-mytree_brlen$tip
    b<-c("gene1","gene2")
    list<-list(a,b)
    values<-c(1,2,1,1,3,2)  #some values for the categories (1=A, 2=B, 3=C)
    mat <- matrix(values,nrow=3, dimnames=list) #Some random data to plot
    
    #plot the hetmap
    heatmap.2(mat, Rowv=dend, Colv=NA, dendrogram='row',col =
            colorRampPalette(c("red","green","yellow"))(3), 
              sepwidth=c(0.01,0.02),sepcolor="black",colsep=1:ncol(mat),rowsep=1:nrow(mat),
          key=FALSE,trace="none",
          cexRow=2,cexCol=2,srtCol=45,
          margins=c(10,10),
          main="Gene presence, absence and duplication in three species")
    
    
    #legend of heatmap
    par(lend=2)           # square line ends for the color legend
    legend("topright",      # location of the legend on the heatmap plot
       legend = c("gene absence", "1 copy of the gene", "2 copies"), # category  labels
       col = c("red", "green", "yellow"),  # color key
       lty= 1,             # line style
       lwd = 15            # line width
    )
    

    这是结果图:)

    【讨论】:

    • 我的声望终于够了!这是结果图像:
    猜你喜欢
    • 2021-10-16
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多