简体   繁体   English

在R中创建自定义调色板

[英]Create a custom color palette in R

I know that R is loaded with some color palettes automatically, such as palette , rainbow , heat.colors and gray . 我知道R会自动加载一些调色板,例如paletterainbowheat.colorsgray I'm also aware of RColorBrewer . 我也知道RColorBrewer But, what if I want to use a custom color palette and assign colors by name? 但是,如果我想使用自定义调色板并按名称指定颜色,该怎么办? Is that possible? 那可能吗?

My company's color palette is as follows: 我公司的调色板如下:

#1A73BA (R: 26 G: 115 B: 186) - this is a blue
#FFDB43 (R:255 G:219 B:67) - this is a yellow
#B54B05 (R:181 G:75 B:5) - this is an orange

My company's initials are AT. 我公司的名字缩写是AT。

I'd like to be able to call those colors via a name rather than by the HEX or RGB because I don't remember them. 我希望能够通过名称而不是HEX或RGB来调用这些颜色,因为我不记得它们。 Ideally, I could create a file that would load into R automatically that would initiate these colors. 理想情况下,我可以创建一个自动加载到R中的文件来启动这些颜色。

ATBlue <- #1A73BA
ATYellow <- #FFDB43
ATOrange <- #B54B05

Then, I could call the colors: 然后,我可以调用颜色:

plot(x,y, col = "ATBlue")

I could put the values into a dataframe, then call them like so: 我可以将值放入数据帧,然后像这样调用它们:

ATColors <- data.frame(name = c("ATBlue", "ATYellow", "ATOrange"), color= c("#1A73BA", "#F7D364", "#B54B05"))

plot(x,y, col = ATColors[3,2])

But I would need to know the location in the dataframe in order to call it correctly. 但我需要知道数据框中的位置才能正确调用它。

Can I create an element that will automatically load when R launches that would allow me call a custom color name into a plot? 我可以创建一个元素,当R启动时会自动加载,这样我就可以将自定义颜色名称调用到图中吗?

This answers (or at least is one possible answer to) your comments and edits: 这回答(或至少是一个可能的答案)您的评论和编辑:

ATblue <- rgb(26/255, 115/255, 186/255, 1)
 ATyellow <- rgb(255/255, 219/255, 67/255, 1)
 ATorange <- rgb(181/255, 75/255, 5/255, 1)

 plot(1:10, col= c(ATblue, ATyellow, ATorange), pch=20)

The definition method with rgb allows you to set an alpha level , thus allowing transparency on graphic devices that support it (at a minimum: 'pdf', 'windows', 'quartz' and 'X11'). 使用rgb的定义方法允许您设置alpha级别,从而允许支持它的图形设备的透明度(至少:'pdf','windows','quartz'和'X11')。

You can also name a 'palette-vector' 你也可以命名'palette-vector'

palvec <-  c(ATblue=ATblue, ATyellow=ATyellow, ATorange=ATorange)

That vector could be accessed with either numbers or names: 可以使用数字或名称访问该向量:

plot(1,1) # to set up plot window
abline(h=c(0.8,1,1.2), col= palvec[ c( 'ATblue', 'ATyellow', 'ATorange' ) ] , lwd=40)

In general I think you will find that if you use all lower case there will be good correspondence for the base and graphics packages (loaded by default so no renaming will be necessary) with that gif-list. 一般来说,我认为你会发现,如果你使用全部小写,那么基本和图形包(默认加载,因此不需要重命名)与gif-list会有很好的对应关系。 So it's already part of R. Let's say you wanted to find the R color name of "LavenderBlush". 所以它已经是R的一部分了。假设您想要找到“LavenderBlush”的R颜色名称。 The vector of assigned color names is returned from colors() but it's rather big, so you can whittle it down with: 分配的颜色名称的向量是从colors()返回的,但它相当大,所以你可以用以下方法减少它:

grep("lavender", colors(), ignore.case=TRUE, value=TRUE)
#[1] "lavender"       "lavenderblush"  "lavenderblush1" "lavenderblush2" 
#     "lavenderblush3" "lavenderblush4"

And say you wanted to see whether the Hex code for that color were the same as the one on your unreadable gif table: 并且说你想看看那个颜色的Hex代码是否与你不可读的gif表上的代码相同:

 ccodes <- as.hexmode( c(256^(2:0) %*% col2rgb("lavenderblush")) )
 ccodes
 #[1] "fff0f5"

Yep. 是的。 And for your example just use "seagreen": 而对于你的例子,只需使用“seagreen”:

> ccodes <- as.hexmode( c(256^(2:0) %*% col2rgb("seagreen")) )
> ccodes
[1] "2e8b57

If you have a hex-code value you can append "#" to it with paste0 : 如果你有一个十六进制代码值,你可以使用paste0为它添加“#”:

 paste0("#", ccodes)
#[1] "#2e8b57"
 plot(1,1, col=paste0("#", ccodes) )

If you have a vector of such values, paste0 is also vectorized: 如果你有这样的值的向量, paste0也被矢量化:

 ccodes <- as.hexmode( c(256^(2:0) %*% col2rgb(colors()[20:25])) )
 paste0("#", ccodes)
#[1] "#ffe4c4" "#eed5b7" "#cdb79e" "#8b7d6b" "#000000" "#ffebcd"

I would put the colours in a named vector like this: 我会把颜色放在这样的命名向量中:

ATcols <- c(blue = "#1A73BA", yellow = "#FFDB43", orange = "#B54B05")

Then you can get, say, blue like this: ATcols["blue"] . 然后你就可以得到像这样的蓝色: ATcols["blue"]

If you want to be a bit fancier, you could create a named vector and a function: 如果你想成为一个有点发烧友,你可以创建一个命名向量和一个函数:

AT_color_data <- c(blue = "#1A73BA", yellow = "#FFDB43", orange = "#B54B05")
ATcolor <- function(color="blue") {
         if (is.numeric(color)) AT_color_data[color]
         else AT_color_data[tolower(color)]
}

This gives you a few options for getting your colours: 这为您提供了一些获取颜色的选项:

ATcolor(2:3)
#    yellow    orange 
# "#FFDB43" "#B54B05"

ATcolor("orange")
#    orange 
# "#B54B05" 

ATcolor(c("orange", "blue"))
#    orange      blue 
# "#B54B05" "#1A73BA" 

Alternatively, you could make your function behave a bit more like rainbow when providing a numeric argument: 或者,在提供数字参数时,您可以使函数的行为更像rainbow

AT_color_data <- c(blue = "#1A73BA", yellow = "#FFDB43", orange = "#B54B05")
ATcolor <- function(color="blue") {
         if (is.numeric(color)) AT_color_data[1:color[1]]
         else AT_color_data[tolower(color)]
}

then, for example: 然后,例如:

ATcolor(2)
#      blue    yellow 
# "#1A73BA" "#FFDB43"
library("grDevices")
plot(1:20,pch=20,col=col)
col=colorRampPalette(c("white", "red"))(20) 
M <- matrix(runif(100),10,10)
M[lower.tri(M)] <- NA
image(M,col = col,frame=F,xaxt="n",yaxt="n")

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

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