[R] Default colors for barplot() XXXX

Marc Schwartz marc_schwartz at me.com
Wed Jun 12 19:54:19 CEST 2013


On Jun 12, 2013, at 12:37 PM, Dan Abner <dan.abner99 at gmail.com> wrote:

> Hi everyone,
> 
> I have the following call to the barplot() function which produces the
> desired stacked bar chart. HOWEVER, barplot() chooses 4 different shades of
> gray for the stacks. If I want to use the legend=NULL argument in
> combination with a separate call to legend() to customize the legend, how
> do I figure out exactly what shades of gray barplot() has choosen? Can
> these color names be extracted from the barplot after saving it as an
> object?
> 
> barplot(table(credit_rating_num,clu_),
> xlab="Cluster Label",
> ylab="Frequency",
> ylim=c(0,7000),
> legend=c("C2","C3","C4","C5"))
> 
> Thank you,
> 
> Dan



The help file defines the 'col' argument as:


col	a vector of colors for the bars or bar components. By default, grey is used if height is a vector, and a gamma-corrected grey palette if height is a matrix.


However, that is lacking a bit of detail in the latter case, albeit one of the examples on the page uses the gray.colors() function. 

The easiest way to get that detail (in this case or for any function more generally) is to look at the source code for the function, within which you would see:

...
    else if (is.matrix(height)) {
        if (is.null(col)) 
            col <- gray.colors(nrow(height))
...


A better approach would be to simply take proactive control of the colors when you call barplot() and define the 'col' argument to colors of your choosing, which you can then use in the call to legend().

Regards,

Marc Schwartz



More information about the R-help mailing list