R:降低调色板的颜色饱和度

标签 r colors

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

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颜色矢量,将饱和度降低一定量,或者允许改变亮度和对比度? (我正在尝试实现比标准调色板饱和度更低且过渡更平滑的彩虹调色板)

编辑:也刚刚发现功能 muted包装内scales或多或少是我想要的:
http://www.inside-r.org/packages/cran/scales/docs/muted

以及 rainbow_hcl包装内colorspace下面由 Josh O'Brien 提到,这是我正在寻找的那种更柔和且强度相等的彩虹:
http://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

最佳答案

这是一个将输入颜色向量按指定比例去饱和的函数:

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

关于R:降低调色板的颜色饱和度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314701/

相关文章:

r - 控制 R 中的内部图形边距

r - 使用 r 中的 ggplot2 更改 geom_bar 中的条形图颜色

R Shiny : Output warning messages to UI

c - 使用自定义颜色绘制而无需使用 Xlib 进行分配

html - 如何避免执行某个类层次

R:如果该组中的一个值低于特定阈值,则为整个组创建一个标记为 0 或 1

r - 了解 R 中的 `scale`

c# - 双线性插值配色方案

image - 在 MATLAB 中为 8 位灰度图像着色

r - splitLayout 中的多个颜色选择器,颜色框被隐藏