[英]What is a “good” palette for divergent colors in R? (or: can viridis and magma be combined together?)
我有兴趣拥有一个“好”的发散调色板。 显然可以只使用红色、白色和蓝色:
img <- function(obj, nam) {
image(1:length(obj), 1, as.matrix(1:length(obj)), col=obj,
main = nam, ylab = "", xaxt = "n", yaxt = "n", bty = "n")
}
rwb <- colorRampPalette(colors = c("red", "white", "blue"))
img(rwb(100), "red-white-blue")
自从最近爱上了viridis调色板,就希望将viridis和岩浆结合起来,形成如此发散的颜色(当然色盲只会看到颜色的绝对值,但有时也可以)。
当我尝试将 viridis 和 magma 结合起来时,我发现它们不会在同一个地方“结束”(或“开始”),所以我得到了这样的结果(我使用的是 R,但这可能是相同的蟒蛇用户):
library(viridis)
img(c(rev(viridis(100, begin = 0)), magma(100, begin = 0)), "magma-viridis")
我们可以看到,当接近于零时,viridis 是紫色的,而岩浆是黑色的。 我希望他们都从(或多或少)同一个位置开始,所以我尝试使用 0.3 作为起点:
img(c(rev(viridis(100, begin = 0.3)), magma(100, begin = 0.3)), "-viridis-magma(0.3)")
这确实更好,但我想知道是否有更好的解决方案。
(我也在“标记”python 用户,因为 viridis 最初来自matplotlib
,所以使用它的人可能知道这样的解决方案)
谢谢!
已经有一些好的和有用的建议,但让我补充几点:
因此,结合岩浆和绿藻效果不佳。 你可以让它们从类似的黄色发散,但你会发散到类似的蓝色。 此外,随着色调的变化,判断您在调色板的哪个臂上会变得更加困难。
正如其他人提到的,ColorBrewer.org 提供了很好的发散调色板。 Moreland 的方法也很有用。 另一个通用的解决方案是colorspace
包中的diverging_hcl()
函数。 在https://arxiv.org/abs/1903.06490 (即将在 JSS 中发布)随附的论文中,描述了构建原则以及基于 HCL 的一般策略如何近似来自 ColorBrewer.org、CARTO 等的众多调色板(较早参考资料包括我们在http://dx.doi.org/10.1016/j.csda.2008.11.033 上在 CSDA 中的初步工作,以及在http://dx.doi 上的 BAMS 论文中针对气象学但适用于其他领域的进一步建议.org/10.1175/BAMS-D-13-00155.1 .)
我们在 HCL 空间(色调-色度-亮度)中的解决方案的优势在于您可以相对轻松地解释坐标。 它确实需要一些练习,但不像其他解决方案那样不透明。 我们还提供了一个 GUI hclwizard()
(见下文),帮助理解不同坐标的重要性。
Most of the palettes in the question and the other answers can be matched rather closely by diverging_hcl()
provided that the two hues (argument h
), the maximum chroma ( c
), and minimal/maximal luminance ( l
) are chosen appropriately. 此外,人们可能不得不分别调整控制色度和亮度增加速度的power
参数。 通常,色度增加得相当快( power[1] < 1
),而亮度增加得更慢( power[2] > 1
)。
例如,Moreland 的“冷暖”调色板使用蓝色( h = 250
)和红色( h = 10
)色调,但亮度对比度相对较小( l = 37
vs. l = 88
):
coolwarm_hcl <- colorspace::diverging_hcl(11,
h = c(250, 10), c = 100, l = c(37, 88), power = c(0.7, 1.7))
看起来很相似(见下文):
coolwarm <- Rgnuplot:::GpdivergingColormap(seq(0, 1, length.out = 11),
rgb1 = colorspace::sRGB( 0.230, 0.299, 0.754),
rgb2 = colorspace::sRGB( 0.706, 0.016, 0.150),
outColorspace = "sRGB")
coolwarm[coolwarm > 1] <- 1
coolwarm <- rgb(coolwarm[, 1], coolwarm[, 2], coolwarm[, 3])
相比之下,ColorBrewer.org 的 BrBG 调色板具有更高的亮度对比度( l = 20
与l = 95
):
brbg <- rev(RColorBrewer::brewer.pal(11, "BrBG"))
brbg_hcl <- colorspace::diverging_hcl(11,
h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3))
下面将生成的调色板与原始调色板下方的基于 HCL 的版本进行比较。 你会看到这些并不相同,而是相当接近。 在右侧,我还将 viridis 和等离子与基于 HCL 的调色板进行了匹配。
您是否喜欢冷暖色调或 BrBG 调色板可能取决于您的个人品味,但更重要的是 - 您想在可视化中展现什么。 如果偏差的符号最重要,则冷暖色中的低亮度对比度将更有用。 如果您想显示(极端)偏差的大小,高亮度对比度会更有用。 上面的论文提供了更实用的指导。
上图的其余复制代码为:
viridis <- viridis::viridis(11)
viridis_hcl <- colorspace::sequential_hcl(11,
h = c(300, 75), c = c(35, 95), l = c(15, 90), power = c(0.8, 1.2))
plasma <- viridis::plasma(11)
plasma_hcl <- colorspace::sequential_hcl(11,
h = c(-100, 100), c = c(60, 100), l = c(15, 95), power = c(2, 0.9))
pal <- function(col, border = "transparent") {
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)
}
par(mar = rep(0, 4), mfrow = c(4, 2))
pal(coolwarm)
pal(viridis)
pal(coolwarm_hcl)
pal(viridis_hcl)
pal(brbg)
pal(plasma)
pal(brbg_hcl)
pal(plasma_hcl)
更新:来自其他工具(ColorBrewer.org、viridis、scico、CARTO 等)的这些基于 HCL 的颜色近似值现在也可用作colorspace
包和基本的hcl.colors()
函数中的命名调色板grDevices
包(从 3.6.0 开始)。 因此,您现在也可以轻松地说:
colorspace::sequential_hcl(11, "viridis")
grDevices::hcl.colors(11, "viridis")
最后,您可以在闪亮的应用程序中以交互方式探索我们建议的颜色: http : //hclwizard.org : 64230/hclwizard/ 。 对于 R 用户,您还可以在您的计算机上本地启动闪亮的应用程序(它的运行速度比我们的服务器要快一些),或者您可以运行它的 Tcl/Tk 版本(甚至更快):
colorspace::hclwizard(gui = "shiny")
colorspace::hclwizard(gui = "tcltk")
如果您想了解调色板路径在 RGB 和 HCL 坐标中的样子, colorspace::specplot()
很有用。 参见例如colorspace::specplot(coolwarm)
。
scico
包(基于 Scientific Colour-Maps 的 R 调色板)有几个很好的发散调色板,这些调色板在感知上是一致的并且色盲安全(例如vik
、 roma
、 berlin
)。
也可用于 Python、MatLab、GMT、QGIS、Plotly、Paraview、VisIt、Mathematica、Surfer、d3 等在这里
论文:Crameri, F. (2018),地球动力学诊断,科学可视化和 StagLab 3.0,Geosci。 Model Dev., 11, 2541-2562, doi:10.5194/gmd-11-2541-2018
博客: 彩虹色图(反复)被认为是有害的
# install.packages('scico')
# or
# install.packages("devtools")
# devtools::install_github("thomasp85/scico")
library(scico)
scico_palette_show(palettes = c("broc", "cork", "vik",
"lisbon", "tofino", "berlin",
"batlow", "roma"))
另一个很棒的软件包是cmocean 。 它的颜色图可以通过pals
包或oce包在 R 中获得。
论文:Thyng, KM, Greene, CA, Hetland, RD, Zimmerle, HM, & DiMarco, SF (2016)。 海洋学的本色。 海洋学,29(3), 10, http://dx.doi.org/10.5670/oceanog.2016.66 。
谈话: PLOTCON 2016:Kristen Thyng,为您的领域定制色彩图。
### install.packages("devtools")
### devtools::install_github("kwstat/pals")
library(pals)
pal.bands(ocean.balance, ocean.delta, ocean.curl, main = "cmocean")
编辑:从rcartocolor包中添加七个级别的最大色盲友好调色板
library(rcartocolor)
display_carto_all(type = 'diverging', colorblind_friendly = TRUE)
我发现Kenneth Moreland 的提议非常有用。 现在已经实现cool_warm
在heatmaply
:
# install.packages("heatmaply")
img(heatmaply::cool_warm(500), "Cool-warm, (Moreland 2009)")
库RColorBrewer
为 =<13 种颜色提供漂亮的调色板。 例如,调色板BrBG
显示从棕色到绿色的不同颜色。
library(RColorBrewer)
display.brewer.pal(11, "BrBG")
通过创建与中点颜色之间的调色板,可以将其扩展为信息较少的调色板。
brbg <- brewer.pal(11, "BrBG")
cols <- c(colorRampPalette(c(brbg[1], brbg[6]))(51),
colorRampPalette(c(brbg[6], brbg[11]))(51)[-1])
类似地,使用您选择的viridis
和magma
调色板,您可以尝试找到它们之间的相似性。 这可能是一个点,在何处将调色板背靠背连接。
select.col <- function(cols1, cols2){
x <- col2rgb(cols1)
y <- col2rgb(cols2)
sim <- which.min(colSums(abs(x[,ncol(x)] - y)))
message(paste("Your palette will be", sim, "colors shorter."))
cols.x <- apply(x, 2, function(temp) rgb(t(temp)/255))
cols.y <- apply(y[,sim:ncol(y)], 2, function(temp) rgb(t(temp)/255))
return(c(cols.x,cols.y))
}
img(select.col(rev(viridis(100,0)),magma(100,0)), "")
# Your palette will be 16 colors shorter.
Viridis 现在提供了cividis 色带,它基本上是一个发散色带。 这也是他们推荐的色带。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.