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

Panos Hadjinicolaou p.hadjinicolaou at cyi.ac.cy
Mon Oct 11 16:09:29 CEST 2010


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.

Thanks,

Panos



-------------------------------------------------------
Dr Panos Hadjinicolaou

Energy Environment& Water Research Center (EEWRC)
The Cyprus Institute



More information about the R-help mailing list