簡體   English   中英

什么是 R 中不同顏色的“好”調色板? (或者:綠色和岩漿可以結合在一起嗎?)

[英]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 ,所以使用它的人可能知道這樣的解決方案)

謝謝!

已經有一些好的和有用的建議,但讓我補充幾點:

  1. 綠色和岩漿調色板是具有多種色調的連續調色板。 因此,沿着比例,您會從非常淺的顏色增加到較深的顏色。 同時,色彩增加,色調從黃色變為藍色(通過綠色或通過紅色)。
  2. 可以通過組合兩個連續調色板來創建發散調色板。 通常,您將它們加入淺色,然后讓它們發散為不同的深色。
  3. 通常,人們使用從中性淺灰色到兩種不同深色的單色調連續調色板。 但是應該注意調色板的不同“臂”在亮度(明暗)和色度(色度)方面是平衡的。

因此,結合岩漿和綠藻效果不佳。 你可以讓它們從類似的黃色發散,但你會發散到類似的藍色。 此外,隨着色調的變化,判斷您在調色板的哪個臂上會變得更加困難。

正如其他人提到的,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 = 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))

下面將生成的調色板與原始調色板下方的基於 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 調色板)有幾個很好的發散調色板,這些調色板在感知上是一致的並且色盲安全(例如vikromaberlin )。

也可用於 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"))

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

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

另一個很棒的軟件包是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")   

德克薩斯和路易斯安那大陸架的數值模擬

時空海洋查詢系統 (STOQS)


編輯:從rcartocolor包中添加七個級別的最大色盲友好調色板

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

我發現Kenneth Moreland 的提議非常有用。 現在已經實現cool_warmheatmaply

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

冷暖色圖 與內插的 RColorBrewer "RdBu" 相比,它在運行時的樣子: 比較

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

在此處輸入圖片說明

類似地,使用您選擇的viridismagma調色板,您可以嘗試找到它們之間的相似性。 這可能是一個點,在何處將調色板背靠背連接。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM