[R] Legend with fill = gray ?

David Winsemius dwinsemius at comcast.net
Wed Sep 1 22:38:53 CEST 2010


On Sep 1, 2010, at 4:17 PM, Jens Oldeland wrote:

> Hi,
>
> I am facing a problem with the legend, I don´t know how to use the  
> fill
> option in the legend in order to achieve the same standard gray levels
> that are plotted.
>
> Sorry for this easy one, but I really did not find anything so far.
>
> It works fine with color:
>
> ###### C O L O R
> mat <- matrix(2,3,rep(2,6))
> rownames(mat)<-c("A","B","C")
> par(xpd=T, mar=par()$mar+c(2,0,0,0))
> barplot(mat,col=rainbow(3))
> legend(1,-2,legend=rownames(mat),fill=rainbow(3))
>
> however
>
> ### G R A Y  ??
>
> par(xpd=T, mar=par()$mar+c(2,0,0,0))
> barplot(mat)  # now without color
> legend(1,-2,legend=rownames(mat),fill=gray(0.1:0.3)) # this gray does
> not work

It might be the grey/gray "not working", or it could be your  
assumption that 0.1:0.3 means what you you think.

 > 0.1:0.3
[1] 0.1

I think it's safer to have the col= argument "lined up" with the fill  
argument. This "works" for me:

barplot(mat, col=gray(seq(0.1, 0.3, by=0.1)))  # now without color
legend(1,0,legend=rownames(mat),fill=gray(seq(0.1, 0.3, by=0.1))  )

This is a "counter-example" of what can go wrong otherwise:

barplot(mat, )  # now without color
  legend(1,0,legend=rownames(mat),fill=gray(1:3/3)) )

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list