3

Say I have a simple matrix A generated by

 A = matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)

Now, my goal is to make a heatmap out of A, such that each number is replaced by a colour field. How to do that? I tried

heatmap(A)

but it produced three coloured stripes instead of 9 distinct fields.

Joe
  • 1,628
  • 3
  • 25
  • 39
  • you should look at the method geom_tile in ggplot2 package (as an example) and quite likely convert your matrix to a data.frame. This link can be helpful: https://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/ – Picarus Jan 07 '16 at 21:59

1 Answers1

8

One of the possibilities that have not been described in the solutions referred to in the comments consists in using the pheatmap package:

library(pheatmap)
A <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
pheatmap(A, cluster_rows = FALSE, cluster_cols = FALSE)

which gives:

enter image description here

RHertel
  • 23,412
  • 5
  • 38
  • 64