[R] Chart colors

Peter Langfelder peter.langfelder at gmail.com
Mon Dec 9 23:57:53 CET 2013


On Mon, Dec 9, 2013 at 2:08 PM, Katharine Miller - NOAA Federal
<katharine.miller at noaa.gov> wrote:
> Hello,
>
> I am having difficulty obtaining the correct colors in my R charts.
>
>> colors()[c(552, 254, 26)]
> [1] "red"   "green" "blue"
>
> But, if I specify col=552 in my barplot, I get gray bars.  Likewise,
> col=254 gives bright pink, and col=26 is a red-orange.   I get accurate
> results when I spell out the names, but I am making a pallet with 20- 30
> colors and it is a real pain to have to do that.  Can anyone help me figure
> out what I am doing wrong?

I believe there are two different color specifications. When you write
col=<number>, you get one of 8 "basic" colors: black, red, green,
blue, turquoise, magenta, yellow, and grey. See this example:

plot(c(1:30), pch = 21, bg = c(1:30), col = c(1:30))

 If the number is higher than 8, you get the (number-1) mod 8 + 1
color. That would explain why you get grey for 552, "bright pink"
(really magenta) for 254, and red for 26.

If you want to get the colors listed by colors(), simply specify col =
colors()[c(552, 254, 26)].

Add as many numbers to the 552, 254, 26 as you need.

Hope this helps,

Peter



More information about the R-help mailing list