[R] Heatmap in R and/or ggplot2

idris idris.raja at gmail.com
Tue Jun 14 19:56:51 CEST 2011


Thanks for your help!

Follow up question: My data contains x, y, height, and day.

I want to create the heatmap for each day, keeping the color coding
consistent.

I've created an example with 2 days, and you can see the charts below.

Notice that the legend changes from day 1 to day 2. How can I make the
legend consistent?

Also, my real data contains hundreds of days. Is there a way to create a
'movie' or a sequence of the heatmaps in chronological order of days? 

The way my code works now is obviously very naive as it just repeats the
same code for day 1 and day 2. Is there a better way to do this?

Thanks in advance, data from dput below.

http://r.789695.n4.nabble.com/file/n3597288/day1.png 
http://r.789695.n4.nabble.com/file/n3597288/day2.png 

#-----------------------
library(ggplot2)

#create breaks for legend
breaks<-seq(0, 35, 5)

#heatmap for day 1
sdf<-subset(df, day==1)
p<- ggplot(sdf, aes(y,x)) 
p<- p + geom_tile(aes(fill=height), colour="white")
p<- p + scale_fill_gradientn(colours = c("dark red", "white", "dark green"
), breaks=breaks, labels=format(breaks))
p<- p + opts(title='Day 1')
p

#heatmap for day 1
sdf<-subset(df, day==2)
p<- ggplot(sdf, aes(y,x)) 
p<- p + geom_tile(aes(fill=height), colour="white")
p<- p + scale_fill_gradientn(colours = c("dark red", "white", "dark green"
), breaks=breaks, labels=format(breaks))
p<- p + opts(title='Day 2')
p

#------------------------------------------------------------------------------
#actual data used
structure(list(x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L,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 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), height = c(0L, 0L, 
0L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 
6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 
11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 
15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 
19L, 20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 
24L, 24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 
28L, 28L, 29L, 29L, 29L, 30L, 30L, 30L, 31L, 31L, 31L, 32L, 32L, 
32L, 33L, 11L, 3L, 4L, 9L, 4L, 10L, 12L, 3L, 9L, 14L, 10L, 9L, 
15L, 13L, 13L, 15L, 14L, 12L, 10L, 11L, 12L, 9L, 8L, 11L, 13L, 
12L, 13L, 15L, 19L, 19L, 12L, 19L, 14L, 22L, 21L, 12L, 13L, 18L, 
20L, 14L, 14L, 15L, 24L, 16L, 23L, 19L, 23L, 26L, 19L, 20L, 26L, 
24L, 21L, 22L, 29L, 21L, 26L, 27L, 20L, 28L, 24L, 24L, 31L, 30L, 
31L, 31L, 26L, 26L, 29L, 28L, 27L, 29L, 27L, 27L, 27L, 27L, 27L, 
35L, 28L, 29L, 34L, 32L, 38L, 32L, 34L, 37L, 32L, 30L, 34L, 30L, 
41L, 34L, 32L, 35L, 41L, 33L, 34L, 36L, 35L, 34L), day = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L)), .Names = c("x", "y", "height", 
"day"), class = "data.frame", row.names = c(NA, -200L))

--
View this message in context: http://r.789695.n4.nabble.com/Heatmap-in-R-and-or-ggplot2-tp3594590p3597288.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list