-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.

1 Answer 1

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)
2
  • 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 Nov 19, 2018 at 5: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")]. Nov 19, 2018 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.