Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom color palette in R

Tags:

r

colors

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?

like image 323
mikebmassey Avatar asked Aug 11 '12 02:08

mikebmassey


People also ask

How do I specify a color palette in ggplot2?

A custom color palettes can be specified using the functions : scale_fill_manual() for box plot, bar plot, violin plot, etc. scale_color_manual() for lines and points.

What color code does R use?

R uses hexadecimal colors. These are represented as strings of six characters. Red, green, and blue components are specified as two hexadecimal digits (0–9, A–F), in the form #rrggbb.


2 Answers

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"
like image 162
IRTFM Avatar answered Oct 25 '22 08:10

IRTFM


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"
like image 33
seancarmody Avatar answered Oct 25 '22 07:10

seancarmody