[R] Variable as color in a barplot

Octave Julien ktawo at free.fr
Fri Sep 22 15:17:23 CEST 2006


Dear wise ones,

I have a problem assigning different colors to bars in a barplot.
The data I'm using is the following dataframe (truncated) :

 > L0
       r n p   t
[...]
18   19 1 1 RFM
19   20 1 1 RFM
20   21 2 1 RFM
21   23 6 1 RIH
22   24 2 1 ROC
23   25 1 1 ROC
24   26 1 1 ROC
25   27 2 1 ROC
26   28 2 1 RFT
27   29 1 1 RFT
28   30 2 1 RFT
29   31 1 1 ROH
[...]

My barplot should display ascending bars according to L0$n. Their width 
depends on L0$p, which can be greater than 1. Last, I want a different 
color for each of the five values of L0$t.
I use the following array to assign a colour for each type given in 
L0$t :

 > coul <- array()
 > coul['ROC'] <- 'khaki1'
 > coul['RFM'] <- 'palegreen'
 > coul['RFT'] <- 'lightorange'
 > coul['RIH'] <- 'blue'
 > coul['ROH'] <- 'lightblue'

And here's the barplot command I'm using :

 > barplot(sort(L0$n), ylim=c(0,10), width=L0$p[order(L0$n)], 
col=coul[L0$t[order(L0$n)]], main="Nombre de genres", sub="Livrets")

The barplot uses the sort() and order() functions to display ascending 
bars with the right values. The width parameter works fine, but the 
color doesn't. I get :

Erreur dans rect(y1, x1, y2, x2, ...) : nom de couleur incorrect
(error in rect(y1, x1, y2, x2, ...) : incorrect color name)

I tried to fiddle with coul[] or its indice but it didn't help.
Does anybody know what's wrong ?
Many thanks,

Octave



More information about the R-help mailing list