How to show legend in heatmap in R? (original) (raw)

`# Import library for color library(RColorBrewer)

Create data matrix

A = matrix( c(1,2,0,3,4,0,2,1,3,3,0,0,4, 4,1,4,3,1,4,3,0,2,1,1,4), nrow = 5, ncol = 5)

Plot a heatmap

heatmap(A, Rowv = NA, Colv = NA, col = colorRampPalette(brewer.pal(8,"Blues"))(3))

Plot a legend in bottom right part of heatmap

legend(x = "bottomright", legend = c("low", "medium", "high"), cex = 0.8, fill = colorRampPalette(brewer.pal(8, "Blues"))(3))

`