在R中,是否可以在图中重叠2种颜色来制作第3种颜色(例如,使用加色或减色混合) [英] In R, is it possible to overlay 2 colours in a plot to make a 3rd (e.g. with additive or subtractive colour mixing)

查看:285
本文介绍了在R中,是否可以在图中重叠2种颜色来制作第3种颜色(例如,使用加色或减色混合)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个包含两类数据的散点图,一个用黄色绘制,一个用蓝色绘制,并且它们以绿色显示在叠加层(或其他任何组合)上。我发现,即使在透明度方面,情况也会更加困难。图表的颜色受到空间中绘制的最后一个点是蓝色还是黄色的影响。



举例如下:

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

你如何设置矢量 orders (它设置了绘制两个大点的圆周),你会得到不同的结果:



如果你交换你绘制两个圆圈的顺序一轮你得到:



无论哪种方式,我都在期待混合颜色来获得绿色。有没有办法做到这一点?谢谢!



编辑 - 这两个图是使用pdf设备制作的。我也尝试通过tikzDevice运行代码,看看是否有效,但没有。

解决方案

pdf()设备的帮助文件表示背景的默认设置是透明的,尽管看起来并不像在实践中是它的行为,也许是因为观众假定白色背景或者可能是因为剧情功能的默认值是白色。中间部分第一成为白色和绿色相等的颜色,然后是第二个第二,然后将其添加到蓝色。所以我猜测数学可以预测(0.5,0.5,(0.5 + 0.8)/ 2)的rgb值,然后将该混合物与覆盖的颜色混合。我认为白色+ col1以相等的比例添加到来自col2的使数学不可交换的值。无论如何,如果您可以强制背景为透明,最终结果可能会有所不同。默认行为更像是混合颜料,而不是像闪亮的剧院灯。



PDF颜色模型在 http://www.adobe.com/devnet/pdf/pdf_reference.html ,并将决定您对PDF设备的期望(但不一定是观众会提供什么)。



我测试了我的理论,认为白色背景是问题(并且似乎反驳了它),将背景更改为黑色希望分层rgb向量不会增加任何值:

  pdf(testfil12rev.pdf,bg = (红色= 0,绿色= 0,蓝色= 1,α= 0.5)
col2 <-rgb = 0,alpha = 0.5)
circle < - data.frame(x = c(0.2,0),
y = c(0,0),
col = c(col1,
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()

颠倒'订单'指标并不会产生与黑色背景相同的混合色。编辑:有一个事先因此,用混合颜色和alpha的规则发布:如何通过指定alpha混合量来计算RGB颜色?


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:

And if you swap the order in which you plot the two circles round you get:

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.

解决方案

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?

这篇关于在R中,是否可以在图中重叠2种颜色来制作第3种颜色(例如,使用加色或减色混合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆