[R] Setting user colors in barchart

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue Sep 16 16:49:23 CEST 2008


> I have a basis question regarding the use of color in the lattice 
package. I
> read the ?barchart help page and searched the R archives but could not
> understand how to do it.
> 
> I just need to plot a barchart using specific colors for my groups, e.g.
> green and red instead of the default lattice colors. How do I do that?
> 
> If I say:
>     barchart(x ~ a_factor, groups=my_groups, data=my_data, 
col=c('green',
> 'red'), auto.key=TRUE)
> 
> then my barplot has the correct colors, but the legend does not. What is 
the
> correct approach to set colors in barchart?

Two possibilities:
1. Manually specify the colours of the rectangles in the key by using the 
key argument instead of auto.key

my_cols <- c("green", "red", "blue")
barchart(yield ~ site, 
   groups=variety, 
   data = barley, 
   col=my_cols,
   key=list(text=list(levels(barley$variety)), 
rectangles=list(col=my_cols)))

2. Set superpose.polygon$col in the plot settings instead of using the col 
argument, e.g.

barchart(yield ~ site, 
   groups=variety, 
   data = barley, 
   auto.key=TRUE, 
   par.settings=list(superpose.polygon=list(col=my_cols)))

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list