-1

I have my matrix and I'm ready to plot a heatmap using the pheatmap package in R.

My matrix is like:

Name A B C
Apple 1 2 3
Banana 4 5 6
Pear 7 8 9

If I don't cluster the column, the heatmap will order the columns as A, B and C. What if I want to set the order as B, C, A?

I tried:

colnames(matrix)<-factor(colnames(matrix),levels = c("B","C","A"))

but it doesn't work.

M--
  • 25,431
  • 8
  • 61
  • 93
Lennon Lee
  • 194
  • 1
  • 14

1 Answers1

1

Just re-order the columns of your matrix manually.

mat <- as.matrix(data.frame(df[, -1], row.names = df[, 1]))

library(pheatmap)
pheatmap(mat[, c("B", "C", "A")], cluster_rows = F, cluster_cols = F)

enter image description here


Sample data

df <- read.table(text =
    "Name A B C
Apple 1 2 3
Banana 4 5 6
Pear 7 8 9", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Thanks for your answer!! But can I set the level for column names outside the pheatmap? As I would like to apply the level to other plots as well – Lennon Lee Nov 19 '18 at 05:29
  • 1
    @LennonLee I'm not really sure what you mean. You can just reorder the columns prior to plotting, e.g. `mat <- mat[, c("B", "C", "A")]`. – Maurits Evers Nov 19 '18 at 12:16