Search code examples
rcluster-computingheatmappheatmap

Pheatmap : How to keep only the dendogram


I have a heatmap I made through R with pheatmap package :

Heatmap

The problem is that it's a bit unlisible ( I 've exported it on a pdf but we need to zoom to see the whole picture) plus I only want to keep the dendogram and tidy the y axis with the group column to have something like this :

Final Result

Here is a look of my datas :

    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             

The code I made only works with the tibble without "phylogroup" column, but I'll put it :

#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

okay so I tried what @Marco Sandri suggested and I got this :

Dendogram

But as you can see it's not so clear, since I just begin to use the dendextend library do you know how to make it clearer ?


Solution

  • The pheatmap function returns the row and column dendrograms, which can be plotted separately.
    See the following example.

    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,1,2))
    phtmap[[2]] %>%
     as.dendrogram() %>%
     set("branches_k_color", k=3) %>%
     set("labels_colors", k=3) %>%
     set("branches_lwd", 2) %>%  
     plot(horiz=T, lwd=2)
    

    enter image description here