Search code examples
rpheatmap

hide the NA values when using display_numbers function in pheatmap


I am plotting heatmap by pheatmap package in r.

I applied the display_numbers function to display the values in a matrix into the heatmap, and I got:

heatmap

I got so many NA in my matrix and I would like to hide them in the heatmap, how can I do that?


Solution

  • First off, it is a lot easier for people to help you if you were to provide reproducible and minimal sample data. Please consider reviewing how to provide a minimal reproducible example/attempt for future posts.


    As to your question:

    1. Let's generate some sampe data

      set.seed(2018)
      mat <- matrix(runif(20), 4, 5)
      

      We use a second matrix to display values via the argument display_numbers of pheatmap. Here we simply copy the original matrix and randomly generate some NA values:

      mat2 <- mat
      mat2[mat2 < 0.5] <- NA
      
    2. We now replace NA values with empty strings.

      mat2[is.na(mat2)] <- ""
      
    3. Let's show the heatmap

      pheatmap(mat, display_numbers = mat2)
      

    enter image description here