1

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

doc elfein
  • 27
  • 5

1 Answers1

2

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"
)
starja
  • 9,887
  • 1
  • 13
  • 28
  • you are amazing as always, I managed to do it all !! I was thinking way to complicated. – doc elfein Sep 12 '20 at 21:27
  • 1
    glad to help. If the answer is useful, please consider accepting it – starja Sep 12 '20 at 21:34
  • do you maybe now how to decrease the size of the break legend ? its bigger than my plot right now – doc elfein Sep 12 '20 at 21:53
  • 1
    I've only found [this](https://stackoverflow.com/questions/25495965/pheatmap-formatting-in-r-legend-size-and-creating-a-square-plot), maybe ask a new question – starja Sep 13 '20 at 07:35
  • yes thanks! i just asked a new question, do you have any favourite packages when it comes to Principal component analysis or PLS-DA? these are the next graphs I need to be able to produce. – doc elfein Sep 14 '20 at 18:35
  • 1
    not really, with a quick search I found these tutorials: https://www.datacamp.com/community/tutorials/pca-analysis-r and https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/ – starja Sep 14 '20 at 19:38
  • Thanks, I will have a look at them! – doc elfein Sep 14 '20 at 19:57