更新:基于答案,我正在研究ColorBrewer
和Colorspace
R软件包。根据要求,我正在重新写我的原始问题。
问题:如何最好地对单个预定义的十六进制颜色进行排序,以便将相似的颜色分组在一起,但是从最暖色开始,从浅色到最冷的颜色排序。我希望将其合并到RanglaPunjab
R软件包的将来版本中
例如,Cynthia Brewer's spectral colors(红色,蓝色,绿色等一起在单个流中)说明了这一点。
我要的是逻辑,而不是代码。
到目前为止,我已根据本文https://www.alanzucconi.com/2015/09/30/colour-sorting/尝试按Hue
,Hue + Value
,Hue * Value
进行排序
这个问题在R和色彩理论之间是有界限的。
这篇文章结尾的代码按Hue
,Hue + Value
,Hue * Value
对颜色进行排序(取决于您评论或取消评论的内容)。
请告诉我(或提示)如何获得与辛西娅·布鲁尔的光谱色相似的结果Unordered Colors
:
根据Hue
排序:
根据Hue + Value
排序:
根据Hue * Value
排序:
供参考,这是原始的,无序的调色板
> oldPal
[1] "#22325f" "#88ce64" "#fbd234" "#b8091f" "#682f4e" "#fdea6e" "#4aec6a" "#fb7894" "#f13111" "#2584a0"
[11] "#6fa42c" "#db3717" "#051a8d" "#ef38a7" "#202c3d"
转换为HSV,然后转置
> tHSVcol
h s v
[1,] 0.62295082 0.6421053 0.3725490
[2,] 0.27672956 0.5145631 0.8078431
[3,] 0.13232831 0.7928287 0.9843137
[4,] 0.97904762 0.9510870 0.7215686
[5,] 0.90935673 0.5480769 0.4078431
[6,] 0.14452214 0.5652174 0.9921569
[7,] 0.36625514 0.6864407 0.9254902
[8,] 0.96437659 0.5219124 0.9843137
[9,] 0.02380952 0.9294606 0.9450980
[10,] 0.53794038 0.7687500 0.6274510
[11,] 0.24027778 0.7317073 0.6431373
[12,] 0.02721088 0.8949772 0.8588235
[13,] 0.64093137 0.9645390 0.5529412
[14,] 0.89890710 0.7656904 0.9372549
[15,] 0.59770115 0.4754098 0.2392157
R代码
library(RanglaPunjab)
RenderPal <- function(x,name){
if ((missing(x)) || (missing(name))){
stop("Internal error, please troubleshoot")
}
n <- length(x)
old <- graphics::par(mar = c(0.5, 0.5, 0.5, 0.5))
on.exit(graphics::par(old))
graphics::image(1:n, 1, as.matrix(1:n), col = x,
ylab = "", xaxt = "n", yaxt = "n", bty = "n")
graphics::rect(0, 0.9, n + 1, 1.1, col = grDevices::rgb(1, 1, 1, 0.8), border = NA)
graphics::text((n + 1) / 2, 1, labels = name, cex = 2, family = "serif")
}
i <- NULL
oldPal <- NULL
rankorder <- NULL
orderedPal<- NULL
RGBcol <- NULL
HSVcol <- NULL
tHSVcol <- NULL
orderType <- NULL
# Paint the colors
PaintPalette("Teej","Gidha","Jutti3")
# Store the hex values
oldPal <- MergePalette("Teej","Gidha","Jutti3")
# Print hex values
oldPal
# Convert Hex to RGB
RGBcol <- col2rgb(oldPal)
# Print RGB values
RGBcol
# Convert RGB to HSV
HSVcol <- rgb2hsv(RGBcol)
# Print matrix
HSVcol
# Transpose matrix
tHSVcol <- t(HSVcol)
# Print matrix
tHSVcol
# Uncomment following to order by Hue, then Saturation, then Value
rankorder <- order(tHSVcol[,1],tHSVcol[,2],tHSVcol[,3])
orderType <- "Hue Ordering"
# Uncomment following to order by hANDv = Hue + Value
# hANDv <- apply(tHSVcol[,c(1,3)],1,sum)
# rankorder <- order(hANDv)
# orderType <- "Hue + Value Ordering"
# Uncomment following to order by hPRODv = Hue * Value
# hPRODv <- apply(tHSVcol[,c(1,3)],1,prod)
# rankorder <- order(hPRODv)
# orderType <- "Hue * Value Ordering"
rankorder
for (i in 1:length(rankorder)){
orderedPal[i] <- oldPal[rankorder[i]]
}
# Print old, unordered palette
oldPal
# Print new, ordered palette
orderedPal
RenderPal(oldPal, "Unordered Palette")
RenderPal(orderedPal, orderType)
最佳答案
通常,您将必须使用一个或多个RGBA组合的顺序,创建自己的对象并建立order
方法,或者使用预先存在的包。
Cynthia Brewer具有出色的包装,RColorBrewer使用有序,发散或无序的不同调色板。其中一些针对常见的色盲条件进行了优化。
library(RColorBrewer)
display.brewer.all()# to see all of them
brewer.pal.info
是一个数据框,其中包含有关每个调色板的信息head(brewer.pal.info)
# maxcolors category colorblind
# BrBG 11 div TRUE
# PiYG 11 div TRUE
# PRGn 11 div TRUE
# PuOr 11 div TRUE
# RdBu 11 div TRUE
# RdGy 11 div FALSE
关于r - 在R中区分不同的十六进制颜色的最佳实践,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51050287/