[R] add a color band

Jim Lemon jim at bitwrit.com.au
Sun Oct 27 12:23:16 CET 2013


On 10/27/2013 08:39 AM, Alaios wrote:
> Hi Jim and thanks for your answer... I might be too tired with my new
> born or just exhausted.
>
> I am attaching for everyone a small data snipset that you can load
>
>
> load("DataToPlotAsImage.Rdata")
> require(plotrix)
> browser()
> test<-data
> # this transforms the values of "test" into red->yellow
> color2D.matplot(test,axes="F",xlab="",ylab="",main="color.scale",
> extremes=c("#FF0000","#FFFF00"),show.legend=FALSE)
>
> axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10))
> color.legend(104,30,112,70,seq(-110,-30,length=11),
> align="rb",rect.col=color.scale(1:30,1,c(0,1),0),gradient="y")
>
> as you can see I have problems where the legend appears. My par("usr"
> returned me
> par("usr")
> # [1] 0 351 0 200
>
> but I am not sure how to read that to place the legend at a useful place.
> second I am not sure why the image is so full with black rows..
>
> What I want is to have the legend visible
> and later on customize the x axis to write custom string of different
> size... First I need though to fix the more severe problems as I have
> described
>
Hi Alex,
I'm not sure why you have created a copy of "data" to plot it. I can get 
quite a sensible plot using this:

par(mar=c(5,4,4,5))
color2D.matplot(data,1,c(0,1),0,xlab="",ylab="",
  main="color.scale",xrange=c(-110,-50),border=NA)
color.legend(357,30,370,100,seq(-110,-50,length.out=6),
  align="rb",rect.col=color.scale(1:6,1,c(0,1),0),
  gradient="y")

Notice several things. First, when you have a large number of cells in a 
plot like this, setting the border to NA means that you don't get mostly 
borders (default = black) in the plot. The second thing is that your 
data range is -107.18150 to -54.07662. In order to get rounded numbers 
in your legend, I have set the xrange argument to -110 to -50. This 
gives a neat looking legend that spans your data, a bit like the
"pretty" function would do. It also means that the color mapping is to 
that range and is the same in the legend as in the plot. I have left 
enough space on the right of the plot to fit in the legend, as that was 
where you said you wanted it in your last email. What par("usr") tells 
you is the dimensions of the plot in user units. Here it is x=0 at the 
left, x=351 at the right, y=0 at the bottom and y=200 at the top.

Jim



More information about the R-help mailing list