marc
marc

Reputation: 971

How to print scale in a heatmap in R

While trying to create a Heatmap in R as mentioned in http://davetang.org/muse/2010/12/06/making-a-heatmap-with-r/

data <- read.table("test.txt",sep="\t",header=TRUE,row.names=1)
data_matrix <- data.matrix(data)
install.packages("RColorBrewer")
library("RColorBrewer")
heatmap(data_matrix,Colv=NA,col=brewer.pal(9,"Blues"))

How do I get the scale of colours beside the heatmap that shows range of values corresponding to shades of colours used, (small value corresponding to light shade and high value to a dark shade) similar to the first heatmap in Creating a continuous heat map in R enter image description here

Upvotes: 0

Views: 2176

Answers (1)

Stephen Henderson
Stephen Henderson

Reputation: 6532

I am simply copying/adapting the following example code from the ggplot2 docs site:

> library(ggplot2)
> library(reshape2) # for melt
> M=melt(volcano)
> head(M)
  Var1 Var2 value
1    1    1   100
2    2    1   101
3    3    1   102
4    4    1   103
5    5    1   104
6    6    1   105

> ggplot(M, aes(x=Var1, y=Var2, fill=value)) + geom_tile()

enter image description here

geom_tile is the important bit here. You can choose your own colours by adding something like e.g.

+ scale_fill_gradient(low="green", high="red")

Upvotes: 1

Related Questions