mikebmassey
mikebmassey

Reputation: 8584

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. I'm also aware of 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.

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. Ideally, I could create a file that would load into R automatically that would initiate these colors.

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?

Upvotes: 12

Views: 30766

Answers (3)

Shicheng Guo
Shicheng Guo

Reputation: 1293

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

Upvotes: -2

IRTFM
IRTFM

Reputation: 263342

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').

You can also name a '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. So it's already part of R. Let's say you wanted to find the R color name of "LavenderBlush". The vector of assigned color names is returned from colors() but it's rather big, so you can whittle it down with:

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:

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

Yep. And for your example just use "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("#", ccodes)
#[1] "#2e8b57"
 plot(1,1, col=paste0("#", ccodes) )

If you have a vector of such values, paste0 is also vectorized:

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

Upvotes: 14

seancarmody
seancarmody

Reputation: 6290

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"].

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:

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"

Upvotes: 2

Related Questions