Color Triangle and plot
3
0
Entering edit mode
7.6 years ago
mchimich ▴ 20

Dear biostars community ! After spending few days looking on the net I finally decide to post my question here.

I wanted to create a heatmap but using three color depending on the ratio between three values. This is an example :

A (0.2), B (0.5) and C (0.3)

for example the A is blue, the B is red and the C is green (see the triangle). for that particular point which is the resulting color (it must be more blue > > green > red). I really don't know where to start and if there is an automatic way to do it in R for example.

enter image description here

Thanks for advance for your help

R • 3.6k views
ADD COMMENT
1
Entering edit mode

From a purley data-viz point of view, this is probably not a good idea. True colour is not a three dimensional thing, it's only 1 dimension, you just happen to have 3 receptors in your face. This would be OK if it wasn't for the fact that the receptors overlap in what they pick up, so they are non-independant:

enter image description here

There is a second dimension your eyes can pick up, light intensity, although since the eyes have a non-linear response to the amount of light coming in and the amount of light perceived, depending on colour, this makes using light intensity quite difficult. For sure, if you use RGB, this non-linear correction will not be done. The HCL colour palette however is designed to make this correction, and it's built into R. (http://hclwizard.org/why-hcl/) (https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/hcl.html)

But even HCL provides you with 3 dials to fiddle with: Hue, Chroma and Luminosity. chroma and hue are not perceived independently, so set the chroma, then use only H and L, or set the hue and use only C and L.

If you really have three dimensions that are totally separate, you're better off using 3D space. If you were plotting these three colours in 2D space before, consider swapping everything around. Plot a scatter based on the data you're using for colour, then colour the scatter in using HCL with the data you previously used for X and Y.

ADD REPLY
1
Entering edit mode

There's always a bit too much made out of the overlap in photoreceptor sensitivity. The brain is able to handle that just fine. The bigger issue is red-green color blind individuals...

ADD REPLY
0
Entering edit mode

Oh yeah, I must admit, i'm pretty bad at remembering to take that into consideration. All the more reason to avoid colour if possible -_-;

ADD REPLY
2
Entering edit mode
7.6 years ago

In R, you want the command rgb(0.2, 0.5, 0.3), which would produce #33804D. Many of the plotting functions can take hex colors like that directly.

ADD COMMENT
0
Entering edit mode

Dear Ryan, thanks so much this is exactly what I was looking for ! However I don't know whether we can change the primary color code from red, green and blue to other color combination ? Anyway thanks again that help a lot

ADD REPLY
2
Entering edit mode

You're only going to get the normal combinations that exist in color specifications, such as rgb() and hsv(). Any other color only defined as a combination of these scores anyway.

ADD REPLY
2
Entering edit mode
7.6 years ago

Maybe you could play around scale_colour_gradient2 from ggplot2? The vertices of the triangle would be low, mid, high.

Example:

library(ggplot2)
dat<- data.frame(x= 1:20, y= 1:20, z= 1:20)
ggplot(data= dat, aes(x= x, y= y, colour= z)) +
    geom_text(label= dat$z) + 
    scale_colour_gradient2(low = "brown", 
                           mid = "green", 
                           high = "blue", midpoint= median(dat$z))

enter image description here

ADD COMMENT
1
Entering edit mode
7.6 years ago

What about pixmapRGB() ?

library(pixmap)

A <- runif(n = 400, min = 0, max = 1)
B <- runif(n = 400, min = 0, max = 1)
C <- runif(n = 400, min = 0, max = 1)

z <- pixmapRGB(c(B,C,A), 20, 20, bbox=c(-1,-1,1,1))
plot(z)
ADD COMMENT
0
Entering edit mode

Thanks Manu for your reply. sorry but I'm not sure to understand what is going on here. I try your code and I obtain a nice plot with many different colors but can't understand what does it mean. can you please explicit your answer ? Many thanks again !

ADD REPLY
0
Entering edit mode

Hi, well, here I've just randomly generated 400 values between 0 and 1 (such as your triangle example) for A, B and C. Instead, I believe you would make a matrix with B,C,A as columns (to be in the same order as RGB in your example), and your measurements as rows. And then you plot with pixmapRGB() the 400 triplets values converted to RGB colors in, arbitrary, 20 col and 20 rows. The idea is I guess that you would have several conditions (e.g. 20) and several (e.g. 20) {A,B,C} triplets (samples?) / condition which you would like to plot as colors, if not, you would not have thought about a heatmap, right?

ADD REPLY

Login before adding your answer.

Traffic: 2720 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6