gpt4 book ai didi

r - 在 R 中对不同的十六进制颜色进行排序的最佳实践

转载 作者:行者123 更新时间:2023-12-02 01:51:17 36 4
gpt4 key购买 nike

更新:根据答案,我正在研究 ColorBrewerColorspace R 包。由于要求,我正在重写我原来的问题。

问题:如何对各个预定义的十六进制颜色进行最佳排序,以便将相似的颜色分组在一起,但从最暖色开始,到浅色到最冷色进行排序。我希望将其合并到 RanglaPunjab R 包的 future 版本中

例如Cynthia Brewer's spectral colors(红色、蓝色、绿色等在单个流中)说明了这一点。

我要求的是逻辑,而不是代码。

enter image description here

<小时/>

到目前为止,我尝试根据本文 https://www.alanzucconi.com/2015/09/30/colour-sorting/HueHue + ValueHue * Value 排序

这个问题介于 R 和颜色理论之间。

本文末尾的代码按 HueHue + ValueHue * Value 对颜色进行排序(取决于您评论或取消评论的内容)。

请告诉我(或给出提示)如何获得与 Cynthia Brewer 的光谱颜色相似的结果

Unordered Colors:

enter image description here

根据 Hue 排序:

enter image description here

根据 Hue + Value 排序:

enter image description here

根据 Hue * Value 排序:

enter image description here

作为引用,这里是原始的、无序的调色板

> 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

enter image description here

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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com