Panos Hadjinicolaou
2010-Oct-11 14:09 UTC
[R] filled.contour: colour key decoupled from main plot?
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
Panos Hadjinicolaou
2010-Oct-12 08:29 UTC
[R] filled.contour: colour key decoupled from main plot?
I have solved it by modifying the filled.contour function: added another parameter (e.g. "key.levels") in the argument list (and accordingly in the function statements used to plot the legend) to decouple the colour key colour bars "length" from the main plot. key.levels is a vector with equally spaced numbers (using the previous example code) : key.levels <- c(1:(length(zprcp.colour)+1)) key.axes is set to key.axes = axis(4,at=klevels,labels=tlabels)) where tlabels <-as.character(zprcp.breaks) in order to draw the user-defined labels. _____ From: Panos Hadjinicolaou [mailto:p.hadjinicolaou at cyi.ac.cy] To: r-help at r-project.org Sent: Mon, 11 Oct 2010 17:09:29 +0300 Subject: filled.contour: colour key decoupled from main plot? 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
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