在 R 中对不同的十六进制颜色进行排序的最佳实践
Best Practice for Sorting Distinct Hex Colors in R
更新: 根据答案,我正在研究 ColorBrewer
和 Colorspace
R 包。应要求,我正在重写我原来的问题。
问题:如何最好地对各个预定义的十六进制颜色进行排序,以便将相似的颜色组合在一起,但从最暖的颜色开始排序,从浅色到最冷的颜色。我希望将其合并到 RanglaPunjab
R 包
的未来版本中
例如 Cynthia Brewer's spectral colors(红色、蓝色、绿色等在单个流中)说明了这一点。
我要的是逻辑,而不是代码。
到目前为止,我尝试根据这篇文章按 Hue
、Hue + Value
、Hue * Value
排序,https://www.alanzucconi.com/2015/09/30/colour-sorting/
这个问题介于 R 和色彩理论之间。
此 post 末尾的代码按 Hue
、Hue + Value
、Hue * Value
对颜色进行排序(取决于您注释或取消注释的内容)。
请告诉我(或提示)如何获得与 Cynthia Brewer 的光谱颜色相似的结果
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 has an excellent package, 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
更新: 根据答案,我正在研究 ColorBrewer
和 Colorspace
R 包。应要求,我正在重写我原来的问题。
问题:如何最好地对各个预定义的十六进制颜色进行排序,以便将相似的颜色组合在一起,但从最暖的颜色开始排序,从浅色到最冷的颜色。我希望将其合并到 RanglaPunjab
R 包
例如 Cynthia Brewer's spectral colors(红色、蓝色、绿色等在单个流中)说明了这一点。
我要的是逻辑,而不是代码。
到目前为止,我尝试根据这篇文章按 Hue
、Hue + Value
、Hue * Value
排序,https://www.alanzucconi.com/2015/09/30/colour-sorting/
这个问题介于 R 和色彩理论之间。
此 post 末尾的代码按 Hue
、Hue + Value
、Hue * Value
对颜色进行排序(取决于您注释或取消注释的内容)。
请告诉我(或提示)如何获得与 Cynthia Brewer 的光谱颜色相似的结果
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 has an excellent package, 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