rheatmappheatmap

color the heatmap based only on values != 0


I have this data frame:

     head(df)                                            gene1    gene2     gene3
leukocyte activation involved in immune response        0             0         0.5
cell activation involved in immune response             0            -1         2
alpha-beta T cell activation                            0.3           1         0
alpha-beta T cell differentiation                       0            -0.8      -0.2
positive regulation of cytokine production              1.7           0        -1
leukocyte differentiation                               0             0         1

please how can I color just the values different from zero? I want to left 0 values white. I used

my_col <- colorRampPalette(c("blue", "white", "red"))(101)  

pheatmap(df, 
         cluster_rows = TRUE,  
         cluster_cols = TRUE, 
         fontsize_row = 8,     
         fontsize_col = 8,
         color = my_col)

but is wrong because I don't want to include 0 in color scale, I would like the zero values to be white by default, like in this example: enter image description here

thank you


Solution

  • Using breaks should give the desired result

    pheatmap(mat, 
             cluster_rows = TRUE,  
             cluster_cols = TRUE, 
             fontsize_row = 8,     
             fontsize_col = 8,
             color = my_col, 
             breaks = seq(-max(mat), max(mat), length.out=length(my_col) + 1))
    

    An alternative using heatmap.2 from gplots (base::heatmap works too)

    library(gplots)
    
    heatmap.2(mat, Rowv=NA, Colv=NA, dendrogram="none",  trace="none", 
              col=colorRampPalette(c("blue", "white", "red"))(41), 
              symbreaks=T, margins=c(15,17), cexRow=0.8)
    

    heatmap

    Data

    mat <- structure(c(0, 0, 0.3, 0, 1.7, 0, 0, -1, 1, -0.8, 0, 0, 0.5, 
    2, 0, -0.2, -1, 1), dim = c(6L, 3L), dimnames = list(c("leukocyte activation involved in immune response", 
    "cell activation involved in immune response", "alpha-beta T cell activation", 
    "alpha-beta T cell differentiation", "positive regulation of cytokine production", 
    "leukocyte differentiation"), c("gene1", "gene2", "gene3")))