Question: Color Triangle and plot
0
mchimich10 wrote:

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. R • 1.7k views
modified 3.2 years ago by dariober10k • written 3.2 years ago by mchimich10
1

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: 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.

1

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...

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 -_-;

2
Devon Ryan92k wrote:

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.

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

2

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.

2
dariober10k wrote:

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) +
mid = "green",
high = "blue", midpoint= median(dat\$z))
`````` 1
Manu Prestat3.9k wrote:

``````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)
``````

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 !