[R] filled.contour: colour key decoupled from main plot?

Jim Lemon jim at bitwrit.com.au
Wed Oct 13 11:31:34 CEST 2010


On 10/12/2010 01:09 AM, Panos Hadjinicolaou wrote:
> Dear R colleagues,
>
> I am trying to plot some geophysical data as a filled contour on a continent map and so far the guidance from the R-help archives has been invaluable. The only bit that still eludes me is the colour key (legend) coming with filled.contour:
>
> I prefer to generate my own colour palette, mainly based on the quantiles of tenths of the data in order to capture the whole range (of rainfall for example), including the more extreme values both sides. In the colour key this results in uneven distribution of the colour bars (and I understood why). Here is the code with simplistic data:
>
> xlon<- seq(10, 60, len=10)
> ylat<- seq(20, 50, len=10)
> prcp<- abs(rnorm(length(xlon)*length(ylat)))*1000
> zprcp<- array(zprcp,c(length(xlon),length(ylat)))
>
> zprcp.colour<-c("#EDFFD2","#00FFD2","#00F0FF","#00B4FF","#0078FF","#003CFF","#0000FF","#3C00FF","#7800FF","#B400FF","#FF0096")
> zprcp.quants<- rev(quantile(zprcp,na.rm=T,probs=c(1,0.98,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1)))
> zprcp.breaks<-c(0,10*ceiling(zprcp.quants/10))
>
> filled.contour(xlon,ylat,zprcp,ylim=c(20,50),xlim=c(10,60), asp=1.0, plot.axes=map('worldHires',xlim=c(10,60),ylim=c(20,50), border=0.9, add =TRUE),levels=zprcp.breaks,col=zprcp.colour,key.axes = axis(4,zprcp.breaks))
>
> I would like the colour bars to be even (and the labels to represent the actual quantile values).
>
> I tried to modify the key.axes=axis(..) to force an evenly spaced colour key (and keeping the same colours) but it seems that this ultimately obeys the 'levels' and 'col' parameters already defined, which are also used for the main image. I have also tried to "decouple" the 'levels' and 'col' settings between the main plot and the legend by fiddling with the filled.contour function but without success yet.
>
> I would be grateful for any ideas, ideally based on the basic graphics package.
>
Hi Panos,
If I understand your request, the color.legend function in plotrix might 
do what you want. Check the examples for the barp function in that package.

Jim



More information about the R-help mailing list