2014-10-11 165 views
11

我正在寻找一种可以将给定调色板的饱和度降低一定量的功能。例如。想象我有调色板R:减少调色板的颜色饱和度

library(colorRamps)  
col.palette=colorRampPalette(rainbow(13),interpolate ="spline")(1000) 
pie(rep(1,1000), col=col.palette,lty=0,labels=NA) 

enter image description here

有什么功能,在那里,可以在此col.palette颜色向量工作,并通过一定量的降低饱和度,或允许亮度和对比度改变? (我想实现用更少的饱和度和比标准的一个平滑的过渡彩虹调色板)

编辑:在包scales也刚刚发现功能muted或多或少做什么,我想: http://www.inside-r.org/packages/cran/scales/docs/muted

为以及rainbow_hcl在由下面乔希奥布莱恩,这是我一直在寻找的那种更静音和相等的强度彩虹提到包colorspacehttp://www.inside-r.org/packages/cran/colorspace/docs/rainbow_hcl

library(colorspace) 
pie(rep(1,1000), col=rainbow_hcl(1000,c=100,l=60),lty=0,labels=NA) 

enter image description here

回答

14

这里将由指定的比例饱和度的输入颜色的向量函数:

library(colorspace) ## hsv colorspace manipulations 
library(RColorBrewer) ## For some example colors 

## Function for desaturating colors by specified proportion 
desat <- function(cols, sat=0.5) { 
    X <- diag(c(1, sat, 1)) %*% rgb2hsv(col2rgb(cols)) 
    hsv(X[1,], X[2,], X[3,]) 
} 

而且这里有什么例子看起来像在行动:

## Utility function for plotting color palettes, 
## (from vignette("hcl-colors")) 
pal <- function(col, border = "light gray", ...) { 
    n <- length(col) 
    plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1), 
    axes = FALSE, xlab = "", ylab = "", ...) 
    rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border) 
} 

## Some example colors 
cc <- brewer.pal(9, 'Set1') 
cc75 <- desat(cc, 0.75) 
cc50 <- desat(cc, 0.50) 
cc25 <- desat(cc, 0.25) 

## Plot 'em 
par(mfcol = c(4,1), mar = c(0,0,0,0)) 
pal(cc) 
pal(cc75) 
pal(cc50) 
pal(cc25) 

enter image description here

+0

非常感谢 - 这就是我一直在寻找!似乎我将不得不操纵V组件,尽管为了避免颜色看起来会被冲淡... – 2014-10-12 08:37:02

+1

@TomWenseleers - 哦,好。如果你想出一些聪明的东西,请考虑把它留在这里作为更好的答案。如果您还没有发现它,可能会发现运行'colortools :: choose_palette(rainbow_hcl)'有趣,然后在生成的GUI中将plot类型设置为“Pie”并以这种方式探索一些选项。干杯。 – 2014-10-15 15:51:52

8

这似乎很好地工作:

col2 <- adjustcolor(col.palette,alpha.f=0.5) 
pie(rep(1,1000), col=col2,lty=0,labels=NA) 

adjustcolor是基础R功能,可以让你在很多方面调整颜色,但我发现alpha.f旋钮最有用的。

+0

感谢您指出我该功能!在我的情况下,我不想改变alpha /透明度,因为它用于3D rgl图。尽管adjustcolor很好,但它似乎不允许轻松操纵诸如亮度,对比度和饱和度(除非这些会由自定义变形指定)。 – 2014-10-11 13:18:05

2

这是另一种方法。在rainbow()中,可以使用另外两个参数(即s和v)。 s是为了饱和。如果你玩这两个参数,你将能够得到你想要的。

col.palette=colorRampPalette(rainbow(13, s = 1, v = 0.8),interpolate = "spline")(1000) 
pie(rep(1,1000), col=col.palette,lty=0,labels=NA) 

enter image description here

col.palette=colorRampPalette(rainbow(13, s = 0.6, v = 1),interpolate = "spline")(1000) 
pie(rep(1,1000), col=col.palette,lty=0,labels=NA) 

enter image description here

+0

非常感谢!这只适用于特定的彩虹调色板,而不适用于任何调色板? – 2014-10-11 13:14:50

+0

@TomWenseleers我做了一些搜索。你可能想看一看'?hsv'。根据R文档,如果使用'hsv()'并创建颜色,函数返回的值可以与col =规范一起使用。所以,你可以在绘制数字之前控制饱和度。 – jazzurro 2014-10-11 13:23:40

+0

哈好我明白了 - 所以基本上转换为HSV,然后改变饱和度值,对吧?我只是试着用cols = colorRampPalette(rainbow(13),interpolate =“spline”)(1000); cols = apply(sapply(cols,col2rgb),2,rgb2hsv); cols [2,] = cols [2,]/2; cols = apply(cols,2,hsv); pie(rep(1,1000),col = cols,lty = 0,labels = NA)似乎并没有工作 - 任何想法我做错了什么? – 2014-10-11 13:29:48