Mr.Spock
Mr.Spock

Reputation: 521

R Github heatmap colors modification

I am using this heatmap to produce several different calendars.

https://github.com/cran/makeR/blob/master/R/calendarHeat.R

My question is, if I can assign a certain value to a specific color in this code. For example, when I have values between 0 and 30, I want 0 stand out very clearly by assigning red to it. All other values from 0.1 to 30 should be from white to blue:

> 0 = #0571B0
> 0.1 - 30 = w2b <- c("#045A8D", "#2B8CBE", "#74A9CF", "#BDC9E1", "#F1EEF6")   #white to blue

Or another example. I have values from -20 to 35 and I want colors from blue to red, with 0 being white. Is that possible with this github code? I found several related topic like this one: R Heatmap with controlled colors and thresholds? , but its not relating to this specific github code.

Upvotes: 0

Views: 214

Answers (1)

Imer Muhović
Imer Muhović

Reputation: 119

Having a specified color for certain values is hard to do with this code, due to it using the levelplot function to do the actual plotting.

You could perhaps try to hack this a bit, for example make a vector of string characters with the values you want for the shades, and put blue at the start of the vector, which would just require you to make a vector like r2r<- c("#ff0033","#a10020") which are the two shades of red that this is going to move from - to. Then down at the levelplot arguments section change

col.regions = calendar.pal(ncolors)

to

col.regions = c('#0367FC', calendar.pal(ncolors-1)),

and beneath that change

colorkey = list(calendar.pal(ncolors), width = 0.6, height = 0.5)

to

colorkey= list(col =c('#0367FC', calendar.pal(ncolors-1)), width = 0.6, height = 0.5)

So you would have blue be the first color in the vector of values for the shading of the boxes. Now you would have to play around with the function call itself. The function has this ncolors argument which controls how many shades there are, if you set this to a low enough number it will start using the first shade in the colors vector for the lowest values in the dataset. For example if I do the same changes to my local copy of the function, and run the following

start_date <- as.Date('2015-01-01') 
end_date <- as.Date('2017-01-01')
dates <- as.Date(sample( as.numeric(start_date): as.numeric(end_date), 56, replace = T), origin = '1970-01-01')
calendarHeat(dates,c(0:55), color='r2r', ncolors = 14)

I get the following plot, I only have one zero in the data I'm sending it, and it has only one blue box, so it's rendering it correctly.

Hackish but working plot

You would have to play around with the ncolors argument, lowering it until you get the expected number of blue boxes, it's not ideal but it would work.

As for having blue for the lowest, white for 0 and a range of colors for the others I haven't tried it out but it would not be easy. Levelplot takes a cuts argument which is the number of levels to divide the dataset in. Ie. here the default is 99 (and this is the argument we are actually changing when playing around with the ncolors argument), so it takes all the values you pass it and splits them into 99 color brackets. So if you had values from -20:35 you could for example make an array manually with 75 shades from red to red. then replace the first value with blue, and the value at position 20 (which would correspond to 0) with white, and pass that instead of the col.regions and colorkey, issue is that you would have to repeat this for each dataset and fine tune the ncolors argument, and check each time did it color in the correct number of white/blue fields, and at that point it would probably be easier to just make a calendar going from red to red and fill in the blue and white fields in InkScape.

Upvotes: 1

Related Questions