rpheatmapr-rownames

Remove NAs from non-selected rownames in pheatmap in R


I have a selected group of genes I would like to plot which I have renamed. But when plotting them I also get the NAs for the non-selected. How could I remove the NAs only plotting my selected genes?

library(pheatmap)
set.seed(2020)
    mat = matrix(rnorm(200),20,10)
    rownames(mat) = paste0("g",1:20)
    rownames(mat)[rownames(mat) == "g2"] <- "abc"
    rownames(mat)[rownames(mat) == "g4"] <- "pqr"
    rownames(mat)[rownames(mat) == "g6"] <- "zzz"
    selected_genes <- c("abc","pqr", "zzz")
    obj = pheatmap(mat,cluster_rows = T, scale = 'row', show_rownames = T, labels_row = selected_genes)

enter image description here

I have seen you can also do something like:

labRow <- c(row.names(mat)[1], rep('', length(row.names(mat))-2)) # Selected row2 as example
pheatmap(mat,cluster_rows = T, scale = 'row', show_rownames = T, labels_row = labRow) 

enter image description here


Solution

  • Since you already discovered that padding the label_row parameter with non-printing values, it seems you should have tried:

    obj = pheatmap(mat,cluster_rows = T, scale = 'row', show_rownames = T, 
                               labels_row = c(selected_genes,rep("   ",17) ) ) 
    

    enter image description here