Pheatmap:如何仅保留树状图

问题描述

我有一个通过pheatmap包通过R制作的热图:

Heatmap

问题在于它有点难以理解(我已经将其导出为pdf,但是我们需要缩放以查看整个图片),而且我只想保留树状图并使用group列来整理y轴像这样的东西:

Final Result

这是我的数据:

    A tibble: 200 x 305
   file  phylogroup `Colicin-like U… `AAI/SCI-II,he… `Tsh,ECP` `S fimbriae,Sa… `Ferrous iron t…
   <chr> <chr>      <chr>            <chr>            <chr>      <chr>            <chr>           
 1 GCF_… B2         5.33             2.87             0.163      0.475            2.2             
 2 GCF_… E          0.00             0.00             0.000      0.000            0.0             
 3 GCF_… A          0.00             0.00             0.000      0.000            0.0             
 4 GCF_… A          0.00             0.00             0.000      0.000            0.0             
 5 GCF_… B1         0.00             0.00             0.000      0.000            0.0             
 6 GCF_… B2         0.00             0.00             0.000      0.000            1.0             
 7 GCF_… B1         0.00             0.00             0.000      0.000            0.0             
 8 GCF_… B1         0.00             0.00             0.000      0.000            0.0             
 9 GCF_… B2         0.00             0.00             0.000      0.000            1.0             

我编写的代码仅适用于不带“ phylogroup”列的小标题,但我将其放置:

#Import datas
df <- read_tsv("file.tsv")
options("digits"=3)
df = df %>% replace(is.na(.),0) 
heatmap_data = t(df)

#replacing column names
my.names <- heatmap_data[1,] 
colnames(heatmap_data) <- my.names
heatmap_data = heatmap_data[-1,]

#Heatmap
df = as.data.frame(heatmap_data)
df[]=lapply(df,function(x) {
    if(is.factor(x)) as.numeric(as.character(x)) else x
})

df = t(df)
  df %>% pheatmap(color = colorRampPalette(c("dark grey","yellow","firebrick3"))(50),legend = TRUE,treeheight_col = 2000,treeheight_row= 1500,fontsize = 25,cellwidth = 25,cellheight = 25,cutree_cols = 303,filename = "HEATMAP_wo_cells.pdf")

EDITT

好的,所以我尝试了@Marco Sandri的建议,我明白了:

Dendogram

但是正如您看到的那样,它并不是很清楚,因为我刚刚开始使用dendextend库,您知道如何使其更清晰吗?

解决方法

pheatmap函数返回行和列树状图,可以分别绘制。
请参见以下示例。

library(pheatmap)
library(dendextend)

phtmap <- pheatmap(scale(mtcars))

par(mfrow = c(1,2))
par(mar=c(2,2,1,6),oma=rep(0,4))
phtmap[[1]] %>%
 as.dendrogram() %>%
 set("branches_k_color",k=4) %>%
 set("labels_colors",k=4) %>%
 set("branches_lwd",2) %>%  
 plot(horiz=T,lwd=2)

par(mar=c(2,2))
phtmap[[2]] %>%
 as.dendrogram() %>%
 set("branches_k_color",k=3) %>%
 set("labels_colors",k=3) %>%
 set("branches_lwd",lwd=2)

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...