problem reshaping heatmap in r using pheatmap

604 Views Asked by At

Does anyone know how to reshape my heatmap so it is similar to the second one I have posted? I need to switch all the sides basically. I tried out a couple of solutions but nothing seems to work. I am using pheatmap().

enter image description here

This is my code:

library(pheatmap)
library(grid)

all_data2 <- cbind(amino,sphingo,hexoses,phospha,lyso,acyl)
matrix_data <- as.matrix(amino[, 3:31])
rownames(matrix_data) <- sample_id$`Sample Identification`
heatmap_final <- matrix_data[,!colnames(matrix_data) %in% c('Sample Identification.1','Sample Identification.2','Sample Identification','Time point.1','Time point.2', 'Time point')]

all_data2 = data.frame("Time point" = c(rep("T0",45),rep("T1",45)))
rownames(all_data2) = rownames(heatmap_final) # name matching

heatmap_final[order(rownames(heatmap_final)),order(colnames(heatmap_final))]

draw_colnames_45 <- function (coln, gaps, ...) {
 coord <- pheatmap:::find_coordinates(length(coln), gaps)
 x     <- coord$coord - 0.5 * coord$size
 res   <- grid::textGrob(
   coln, x = x, y = unit(1, "npc") - unit(3,"bigpts"),
   vjust = 0.75, hjust = 1, rot = 45, gp = grid::gpar(...)
 )
 return(res)
}
assignInNamespace(
 x = "draw_colnames",
 value = "draw_colnames_45",
 ns = asNamespace("pheatmap")
)


 
 
pheatmap(
 mat = log2(heatmap_final),
 scale = "column",
 annotation_row = all_data2,
 cluster_rows = F,
 show_rownames = TRUE,
 drop_levels = TRUE,
 fontsize = 5,
 clustering_method = "complete",
 main = "Hierachical Cluster Analysis"
)



It should be sorted like this, and if anyone has an idea on how to switch my Heatmap around to match the second one I would be grateful.

enter image description here

1

There are 1 best solutions below

7
On BEST ANSWER

You can transpose your input matrix with t(). Then you have to change the other parameters that depend on rows/columns of your heatmap accordingly:

pheatmap(
 mat = t(log2(heatmap_final)),
 scale = "row",
 annotation_col = all_data2,
 cluster_cols = F,
 show_rownames = TRUE,
 drop_levels = TRUE,
 fontsize = 5,
 clustering_method = "complete",
 main = "Hierachical Cluster Analysis"
)