简体   繁体   English

什么是 R 中不同颜色的“好”调色板? (或者:绿色和岩浆可以结合在一起吗?)

[英]What is a “good” palette for divergent colors in R? (or: can viridis and magma be combined together?)

I am interested in having a "good" divergent color pallette.我有兴趣拥有一个“好”的发散调色板。 One could obviously use just red, white, and blue:显然可以只使用红色、白色和蓝色:

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")

在此处输入图片说明

Since I recently fell in love with the viridis color palettes , I was hoping to combine viridis and magma to form such divergent colors (of course, color blind people would only see the absolute value of the color, but that is sometimes ok).自从最近爱上了viridis调色板,就希望将viridis和岩浆结合起来,形成如此发散的颜色(当然色盲只会看到颜色的绝对值,但有时也可以)。

When I tried combining viridis and magma, I found that they don't "end" (or "start") at the same place, so I get something like this (I'm using R, but this would probably be the same for python users):当我尝试将 viridis 和 magma 结合起来时,我发现它们不会在同一个地方“结束”(或“开始”),所以我得到了这样的结果(我使用的是 R,但这可能是相同的蟒蛇用户):

library(viridis)
img(c(rev(viridis(100, begin = 0)), magma(100, begin = 0)), "magma-viridis")

在此处输入图片说明

We can see that when close to zero, viridis is purple, while magma is black.我们可以看到,当接近于零时,viridis 是紫色的,而岩浆是黑色的。 I would like for both of them to start in (more or less) the same spot, so I tried using 0.3 as a starting point:我希望他们都从(或多或少)同一个位置开始,所以我尝试使用 0.3 作为起点:

img(c(rev(viridis(100, begin = 0.3)), magma(100, begin = 0.3)), "-viridis-magma(0.3)")

在此处输入图片说明

This is indeed better, but I wonder if there is a better solution.这确实更好,但我想知道是否有更好的解决方案。

(I am also "tagging" python users, since viridis is originally from matplotlib , so someone using it may know of such a solution) (我也在“标记”python 用户,因为 viridis 最初来自matplotlib ,所以使用它的人可能知道这样的解决方案)

Thanks!谢谢!

There have been some good and useful suggestions already but let me add a few remarks:已经有一些好的和有用的建议,但让我补充几点:

  1. The viridis and magma palettes are sequential palettes with multiple hues.绿色和岩浆调色板是具有多种色调的连续调色板。 Thus, along the scale you increase from very light colors to rather dark colors.因此,沿着比例,您会从非常浅的颜色增加到较深的颜色。 Simultaneously the colorfulness is increased and the hue changes from yellow to blue (either via green or via red).同时,色彩增加,色调从黄色变为蓝色(通过绿色或通过红色)。
  2. Diverging palettes can be created by combining two sequential palettes.可以通过组合两个连续调色板来创建发散调色板。 Typically, you join them at the light colors and then let them diverge to different dark colors.通常,您将它们加入浅色,然后让它们发散为不同的深色。
  3. Usually, one uses single-hue sequential palettes that diverge from a neutral light gray to two different dark colors.通常,人们使用从中性浅灰色到两种不同深色的单色调连续调色板。 One should pay attention though that the different "arms" of the palette are balanced with respect to luminance (light-dark) and chroma (colorfuness).但是应该注意调色板的不同“臂”在亮度(明暗)和色度(色度)方面是平衡的。

Therefore, combining magma and viridis does not work well.因此,结合岩浆和绿藻效果不佳。 You could let them diverge from a similar yellowish color but you would diverge to similar blueish colors.你可以让它们从类似的黄色发散,但你会发散到类似的蓝色。 Also with the changing hues it would just become more difficult to judge in which arm of the palette you are.此外,随着色调的变化,判断您在调色板的哪个臂上会变得更加困难。

As mentioned by others, ColorBrewer.org provides good diverging palettes.正如其他人提到的,ColorBrewer.org 提供了很好的发散调色板。 Moreland's approach is also useful. Moreland 的方法也很有用。 Yet another general solution is our diverging_hcl() function in the colorspace package.另一个通用的解决方案是colorspace包中的diverging_hcl()函数。 In the accompanying paper at https://arxiv.org/abs/1903.06490 (forthcoming in JSS) the construction principles are described and also how the general HCL-based strategy can approximate numerous palettes from ColorBrewer.org, CARTO, etc. (Earlier references include our initial work in CSDA at http://dx.doi.org/10.1016/j.csda.2008.11.033 and further recommendations geared towards meteorology, but applicable beyond, in a BAMS paper at http://dx.doi.org/10.1175/BAMS-D-13-00155.1 .)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 .)

The advantage of our solution in HCL space (hue-chroma-luminance) is that you can interpret the coordinates relatively easily.我们在 HCL 空间(色调-色度-亮度)中的解决方案的优势在于您可以相对轻松地解释坐标。 It does take some practice but isn't as opaque as other solutions.它确实需要一些练习,但不像其他解决方案那样不透明。 Also we provide a GUI hclwizard() (see below) that helps understanding the importance of the different coordinates.我们还提供了一个 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. 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. Furthermore, one may have to tweak the power argument which controls how quickly chroma and luminance are increased, respectively.此外,人们可能不得不分别调整控制色度和亮度增加速度的power参数。 Typically, chroma is added rather quickly ( power[1] < 1 ) whereas luminance is increased more slowly ( power[2] > 1 ).通常,色度增加得相当快( power[1] < 1 ),而亮度增加得更慢( power[2] > 1 )。

Moreland's "cool-warm" palette for example uses a blue ( h = 250 ) and red ( h = 10 ) hue but with a relatively small luminance contrast( l = 37 vs. l = 88 ):例如,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))

which looks rather similar (see below) to:看起来很相似(见下文):

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])

In contrast, ColorBrewer.org's BrBG palette a much higher luminance contrast ( l = 20 vs. l = 95 ):相比之下,ColorBrewer.org 的 BrBG 调色板具有更高的亮度对比度( l = 20l = 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))

The resulting palettes are compared below with the HCL-based version below the original.下面将生成的调色板与原始调色板下方的基于 HCL 的版本进行比较。 You see that these are not identical but rather close.你会看到这些并不相同,而是相当接近。 On the right-hand side I've also matched viridis and plasma with HCL-based palettes.在右侧,我还将 viridis 和等离子与基于 HCL 的调色板进行了匹配。

调色板

Whether you prefer the cool-warm or BrBG palette may depend on your personal taste but also - more importantly - what you want to bring out in your visualization.您是否喜欢冷暖色调或 BrBG 调色板可能取决于您的个人品味,但更重要的是 - 您想在可视化中展现什么。 The low luminance contrast in cool-warm will be more useful if the sign of the deviation matters most.如果偏差的符号最重要,则冷暖色中的低亮度对比度将更有用。 A high luminance contrast will be more useful if you want to bring out the size of the (extreme) deviations.如果您想显示(极端)偏差的大小,高亮度对比度会更有用。 More practical guidance is provided in the papers above.上面的论文提供了更实用的指导。

The rest of the replication code for the figure above is:上图的其余复制代码为:

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)

Update: These HCL-based approximations of colors from other tools (ColorBrewer.org, viridis, scico, CARTO, ...) are now also available as named palettes in both the colorspace package and the hcl.colors() function from the basic grDevices package (starting from 3.6.0).更新:来自其他工具(ColorBrewer.org、viridis、scico、CARTO 等)的这些基于 HCL 的颜色近似值现在也可用作colorspace包和基本的hcl.colors()函数中的命名调色板grDevices包(从 3.6.0 开始)。 Thus, you can now also say easily:因此,您现在也可以轻松地说:

colorspace::sequential_hcl(11, "viridis")
grDevices::hcl.colors(11, "viridis")

Finally, you can explore our proposed colors interactively in a shiny app: http://hclwizard.org:64230/hclwizard/ .最后,您可以在闪亮的应用程序中以交互方式探索我们建议的颜色: http : //hclwizard.org : 64230/hclwizard/ For users of R, you can also start the shiny app locally on your computer (which runs somewhat faster than from our server) or you can run a Tcl/Tk version of it (which is even faster):对于 R 用户,您还可以在您的计算机上本地启动闪亮的应用程序(它的运行速度比我们的服务器要快一些),或者您可以运行它的 Tcl/Tk 版本(甚至更快):

colorspace::hclwizard(gui = "shiny")
colorspace::hclwizard(gui = "tcltk")

If you want to understand what the paths of the palettes look like in RGB and HCL coordinates, the colorspace::specplot() is useful.如果您想了解调色板路径在 RGB 和 HCL 坐标中的样子, colorspace::specplot()很有用。 See for example colorspace::specplot(coolwarm) .参见例如colorspace::specplot(coolwarm)

The scico package (Palettes for R based on the Scientific Colour-Maps ) has several good diverging palettes that are perceptually uniform and colorblind safe (eg, vik , roma , berlin ). scico包(基于 Scientific Colour-Maps 的 R 调色板)有几个很好的发散调色板,这些调色板在感知上是一致的并且色盲安全(例如vikromaberlin )。

Also available for Python, MatLab, GMT, QGIS, Plotly, Paraview, VisIt, Mathematica, Surfer, d3, etc. here也可用于 Python、MatLab、GMT、QGIS、Plotly、Paraview、VisIt、Mathematica、Surfer、d3 等在这里

Paper : Crameri, F. (2018), Geodynamic diagnostics, scientific visualisation and StagLab 3.0, Geosci.论文:Crameri, F. (2018),地球动力学诊断,科学可视化和 StagLab 3.0,Geosci。 Model Dev., 11, 2541-2562, doi:10.5194/gmd-11-2541-2018 Model Dev., 11, 2541-2562, doi:10.5194/gmd-11-2541-2018

Blog : The Rainbow Colour Map (repeatedly) considered harmful博客: 彩虹色图(反复)被认为是有害的

# 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"))

德曼等人。 (2019),粘性变形岩石中的应变定位和弱化过程,JGR,doi:10.1029/2018JB016917

Thieulot (2018), GHOST: Geoscientific Hollow Sphere Tessellation, Solid Earth, doi:10.5194/se-9-1169-2018

Another great package is cmocean .另一个很棒的软件包是cmocean Its colormaps are available in R via the pals package or the oce package.它的颜色图可以通过pals包或oce包在 R 中获得。

Paper : Thyng, KM, Greene, CA, Hetland, RD, Zimmerle, HM, & DiMarco, SF (2016).论文:Thyng, KM, Greene, CA, Hetland, RD, Zimmerle, HM, & DiMarco, SF (2016)。 True colors of oceanography.海洋学的本色。 Oceanography, 29(3), 10, http://dx.doi.org/10.5670/oceanog.2016.66 .海洋学,29(3), 10, http://dx.doi.org/10.5670/oceanog.2016.66

Talk : PLOTCON 2016: Kristen Thyng, Custom Colormaps for Your Field .谈话PLOTCON 2016:Kristen Thyng,为您的领域定制色彩图

### install.packages("devtools")    
### devtools::install_github("kwstat/pals")   
library(pals)   
pal.bands(ocean.balance, ocean.delta, ocean.curl, main = "cmocean")   

德克萨斯和路易斯安那大陆架的数值模拟

时空海洋查询系统 (STOQS)


Edit : add seven levels max colorblind-friendly palettes from the rcartocolor package编辑:从rcartocolor包中添加七个级别的最大色盲友好调色板

library(rcartocolor)
display_carto_all(type = 'diverging', colorblind_friendly = TRUE)

I find Kenneth Moreland's proposal quite useful.我发现Kenneth Moreland 的提议非常有用。 It has now been implemented as cool_warm in heatmaply :现在已经实现cool_warmheatmaply

# install.packages("heatmaply")
img(heatmaply::cool_warm(500), "Cool-warm, (Moreland 2009)")

冷暖色图 This it how it looks like in action compared to an interpolated RColorBrewer "RdBu":与内插的 RColorBrewer "RdBu" 相比,它在运行时的样子: 比较

Library RColorBrewer provides beautiful palettes for =<13 colors.RColorBrewer为 =<13 种颜色提供漂亮的调色板。 For example, palette BrBG shows diverging colors from brown to green.例如,调色板BrBG显示从棕色到绿色的不同颜色。

library(RColorBrewer)
display.brewer.pal(11, "BrBG")

在此处输入图片说明

Which can be expanded to a less informative palette by creating palettes to and from a mid-point color.通过创建与中点颜色之间的调色板,可以将其扩展为信息较少的调色板。

brbg <- brewer.pal(11, "BrBG")
cols <- c(colorRampPalette(c(brbg[1], brbg[6]))(51), 
    colorRampPalette(c(brbg[6], brbg[11]))(51)[-1])

在此处输入图片说明

Analogically, using your choice of viridis and magma palettes, you can try finding a similarity between them.类似地,使用您选择的viridismagma调色板,您可以尝试找到它们之间的相似性。 This could be a point, where to join the palettes back to back.这可能是一个点,在何处将调色板背靠背连接。

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 now provides the cividis color ramp, which is basically a diverging color ramp. Viridis 现在提供了cividis 色带,它基本上是一个发散色带。 It's also their recommended color ramp .这也是他们推荐的色带 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM