如何混合 2 个或多个调色板以显示组合颜色值 [英] How can one mix 2 or more color palettes to show a combined color value

查看:25
本文介绍了如何混合 2 个或多个调色板以显示组合颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个图,其中颜色代表多个值的组合.在下面的示例中,我对与 x 坐标关联的红色应用递增值,对与 y 坐标关联的蓝色应用递增值.

I'm trying to create a plot where color represents the combination of several values. In the example below, I am applying increasing values for red associated with the x-coordinate and increasing values for blue in associated with the y-coordinate.

#required function 'val2col' from: http://www.menugget.blogspot.de/2011/09/converting-values-to-color-levels.html

val2col<-function(z, zlim, col = heat.colors(12), breaks){
 if(!missing(breaks)){
  if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")}
 }
 if(missing(breaks) & !missing(zlim)){
  zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
  zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3)
  breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1)) 
 }
 if(missing(breaks) & missing(zlim)){
  zlim <- range(z, na.rm=TRUE)
  zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
  zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3)
  breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1))
 }
 colorlevels <- col[((as.vector(z)-breaks[1])/(range(breaks)[2]-range(breaks)[1]))*(length(breaks)-1)+1] # assign colors to heights for each point
 colorlevels
}


#data
x <- seq(100)
y <- seq(100)
grd <- expand.grid(x=x,y=y)

#assign colors to grd levels
pal1 <- colorRampPalette(c("white", rgb(1,0,0)), space = "rgb")
col1 <- val2col(x, col=pal1(10))
pal2 <- colorRampPalette(c("white", rgb(0,0,1)), space = "rgb")
col2 <- val2col(y, col=pal2(10))
col3 <- NA*seq(nrow(grd))
for(i in seq(nrow(grd))){
    xpos <- grd$x[i]
    ypos <- grd$y[i]
    coltmp <- (col2rgb(col1[xpos])/2) + (col2rgb(col2[ypos])/2)
    col3[i] <- rgb(coltmp[1], coltmp[2], coltmp[3], maxColorValue = 255)
}

    #plot
png("2_color_scales.png", width=6, height=4, units="in", res=200)
layout(matrix(c(1,2,3), nrow=1, ncol=3), widths=c(4,1,1), heights=4, respect=T)
par(mar=c(4,4,2,2))
plot(grd,col=col3, pch=19)
par(mar=c(4,0,2,5))
image(x=1, y=x, z=t(as.matrix(x)), col=pal1(10), xaxt="n", yaxt="n", xlab="", ylab="")
box()
axis(4)
mtext("x", side=4, line=3, cex=0.7)
par(mar=c(4,0,2,5))
image(x=1, y=y, z=t(as.matrix(y)), col=pal2(10), xaxt="n", yaxt="n", xlab="", ylab="")
box()
axis(4)
mtext("y", side=4, line=3, cex=0.7)
dev.off()

结果在技术上是正确的,因为当 x = 1 和 y = 10 时,颜色白色"和蓝色"的混合分别返回较浅的蓝色阴影.但是,我更希望这个位置看起来像 y 颜色条中最深的蓝色一样蓝色".我想这将需要对较低的值使用透明度而不是白色.有没有人对如何实现这一点有建议?添加两种颜色,包括它们的透明度,超出了我的能力......我想一个人可能可以在混合步骤中使用透明度值作为权重?

The result is technically correct in that when x = 1 and y = 10, the mixing of colors "white" and "blue", respectively, returns the lighter shade of blue. However, I would rather like this position to look as "blue" as the darkest blue of the y color bar. I imagine this would require one to use transparency for lower values rather than the color white. Does anyone have suggestions on how this might be accomplished? Adding two colors, including their transparency, is beyond me... I thought one might be able to use the transparency value as a weighting in the mixing step?

感谢您的帮助.

推荐答案

由于我对 ggplot 比较熟悉,我将展示使用 ggplot 的解决方案.这样做的附带好处是,由于 ggplot 代码非常简单,我们可以将讨论重点放在色彩管理的主题上,而不是 R 代码.

Since I'm more familiar with ggplot, I'll show a solution using ggplot. This has the side benefit that, since the ggplot code is very simple, we can focus the discussion on the topic of colour management, rather than R code.

  1. 首先使用 expand.grid 创建一个数据框 dat,其中包含红色和蓝色输入值的网格.
  2. 使用函数rgb()创建颜色混合,并将其分配给dat$mix
  3. 情节.
  1. Start by using expand.grid to create a data frame dat containing the grid of red and blue input values.
  2. Use the function rgb() to create the colour mix, and assign it to dat$mix
  3. Plot.

代码:

dat <- expand.grid(blue=seq(0, 100, by=10), red=seq(0, 100, by=10))
dat <- within(dat, mix <- rgb(green=0, red=red, blue=blue, maxColorValue=100))

library(ggplot2)
ggplot(dat, aes(x=red, y=blue)) + 
  geom_tile(aes(fill=mix), color="white") + 
  scale_fill_identity()

您会注意到色阶与您建议的不同,但可能更直观.

You will notice that the colour scale is different from what you suggested, but possibly more intuitive.

混合光线时,没有任何颜色会产生黑色,所有颜色都会产生白色.

When mixing light, absence of any colour yields black, and presence of all colours yields white.

这在情节中清楚地表明,我觉得解释起来相当直观.

This is clearly indicated by the plot, which I find rather intuitive to interpret.

这篇关于如何混合 2 个或多个调色板以显示组合颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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