Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, is it possible to overlay 2 colours in a plot to make a 3rd (e.g. with additive or subtractive colour mixing)

Tags:

I'd like to do a scatter plot with 2 categories of the data, one plotted in yellow and one in blue, and them showing up in green where they overlay (or any other combination). I've found that things are a little more difficult even with transparency. The colour of the graph is influenced by whether the last point plotted in a space was blue or yellow.

For example with the following code:

col1 <- rgb(red = .0, green = 0, blue = 0.8, alpha = 0.8)
col2 <- rgb(red = .8, green = 0.8, blue = 0, alpha = 0.8)
circle <- data.frame(x = c(0.2,0), 
                    y = c(0,0),
                    col = c(col1, col2),
                    stringsAsFactors = F)
orders <- c(1,2)
plot(x = circle$x[orders], y = circle$y[orders],
     col = circle$col[orders], pch = 19, cex = 100, xlab = "", ylab = "")

Depending on how you set the vector orders (which sets up which way round the two large points are drawn) you get different results: enter image description here

And if you swap the order in which you plot the two circles round you get: enter image description here

Either way, I was anticipating mixing the colours to get a green. Is there a way to do this? Thanks!

Edit - the two plots were made using the pdf device. I've also tried running the code through tikzDevice to see if that worked but it didn't.

like image 728
Andrew Avatar asked Nov 25 '11 13:11

Andrew


People also ask

Is mixing paint considered additive or subtractive color mixing?

Subtractive color mixing is creating a new color by the removal of wavelengths from a light with a broad spectrum of wavelengths. Subtractive color mixing occurs when we mix paints, dyes, or pigments.

What is subtractive color mixture?

Subtractive colour mixing involves the absorption and selective transmission or reflection of light. It occurs when colorants (such as pigments or dyes) are mixed or when several coloured filters are inserted into a single beam of white light.

How do subtractive colors work?

Subtractive color begins with white (paper) and ends with black; as color is added, the result is darker. Printers use cyan, magenta and yellow inks in various percentages to control the amount of red, green and blue light reflected from white paper.

What are the main colours give an account of different types of colour mixture?

By convention, the three primary colors in additive mixing are red, green, and blue. In the absence of light of any color, the result is black. If all three primary colors of light are mixed in equal proportions, the result is neutral (gray or white). When the red and green lights mix, the result is yellow.


1 Answers

The help file for the pdf() device says the default setting for background is "transparent", although that does not seem to be its behavior in practice, perhaps because viewers assume a white background or perhaps because the default for the plot function is "white". The middle section first becomes a color that is an equal blend of white and green, and second second you then add to it the blue. So I'm guessing that the math would predict an rgb value of (0.5, 0.5, (0.5+0.8)/2 ) followed by blending that mixture with the overlayed color And I think the white+col1 gets added in equal proportions to the values from col2 which makes the math non-commutative. At any rate the end result will probably be different if you can force the background to "transparent". The default behavior is more like mixing paints than like shining theater lights.

The PDF color model is described in http://www.adobe.com/devnet/pdf/pdf_reference.html and will determine what you can expect from a pdf device (but not necessarily what a viewer will provide).

I tested my theory that the white background was the problem (and appear to have disproved it) by changing the background to "black" in hopes that there would be no added values to the layered rgb vectors:

pdf("testfil12rev.pdf", bg="black")
 col1 <- rgb(red = .0, green = 0, blue = 1, alpha = 0.5)
 col2 <- rgb(red = 1, green = 0, blue = 0, alpha = 0.5)
 circle <- data.frame(x = c(0.2,0), 
                     y = c(0,0),
                     col = c(col1, col2),
                     stringsAsFactors = F)
 orders <- c(1,2)
plot(x = circle$x[orders], y = circle$y[orders], bg="black",
      col = circle$col[orders], pch = 19, cex = 100, xlab = "", ylab = "")

 dev.off()

Reversing the 'orders' indicator does NOT result in the same blended color with a black background, either.

EDIT: There is a prior SO posting with the rules for blending colors and alphas: How to calculate an RGB colour by specifying an alpha blending amount?

like image 80
IRTFM Avatar answered Nov 25 '22 13:11

IRTFM