2

I have a dataframe, it contains dates and status at this date. Status ranges from -1 to 2. I would like to create a heat map (grouped by days of the week and number of the week in the year), which is not a problem.

My problem is i want the following color mapping:

-1 => gray
0 => white
1 => blue
2 => green

Any ideas?. Thank you!

EDIT: As requested a sample code below the way I would do it and it doesnt work for me:

breaks <- seq(-2,2,1)
pal <- c("gray","white","green","blue")
data <- sample(-1:2, 5*6, replace=T)
m <- matrix(unlist(data),ncol=6)
m
image(m,
      col = c("gray","white","green","blue"),
      breaks = c(-2,-1,0,1,2)
      )
NeatNerd
  • 2,305
  • 3
  • 26
  • 49
  • 2
    Please provide us with a reproducible example, i.e. the code and the data needed to produce the graph as it is right now. Right now we don't even know which function or graphics library you use to create the graph. – Paul Hiemstra May 07 '13 at 09:46
  • its not about the specific code or graphics library. Honestly speaking i dont care if you use `heatmap, image or ggplot`. My question if it is possible to do such a mapping in any of them – NeatNerd May 07 '13 at 10:18
  • It's much easier (and faster) to answer if we have a sample of your data and a concrete example of what you want to get. – juba May 07 '13 at 10:38
  • I believe the answer is: http://stackoverflow.com/a/10806683/1486768 – Lorinc Nyitrai May 07 '13 at 13:12
  • nope, thats not what i wanted. Please, check my edit – NeatNerd May 08 '13 at 09:23

2 Answers2

2

You could try the heatmap.2() function from the gplots package and create your own color palette like

my_palette <- c("gray","white","green","blue"), which you then use as argument for the col parameter

heatmap.2(..., col = my_palette, ...

assuming you have your data scaled appropriately.

I touched this topic of customizing heat map colors really briefly in my book Heat Maps in R: How-To. However, with the current packages there is not a really "good" solution, but it's more like work arounds :(

1

The following seems to do what you want:

set.seed(14)
a<-matrix(floor(runif(21)*4)-1,nrow=7)
col<-c("grey","white","blue","green")
image(1:8,1:4,a,col=col)

For the gplot version:

#color scale
col<-c("red","orange","grey","lightblue","darkblue")
#creating the dataframe for 3 example weeks
(d<-t(matrix(c(1,1,2,3,2,3,4,
    2,1,2,2,3,4,4,
    5,3,2,4,5,4,5),nrow=3,byrow=TRUE)))
df<-data.frame(day=rep(1:7,3),week=rep(1:3,each=7),werte=as.numeric(d))
#the ggplot
p<-ggplot(df,aes(x=day,y=max(df$week)+1-week))+
    #tile layer
    geom_tile(aes(fill=factor(werte))) +
    #setting the color
    scale_fill_manual(
        values=col,
        breaks=c("1","2","3","4","5"),
        labels=c("one","two","three","four","five"))

p
user1965813
  • 671
  • 5
  • 16