Re: [R] Same color key for multiple lattice contour plots

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Mon, 07 Mar 2011 14:46:24 +0530

On Mon, Feb 21, 2011 at 5:29 AM, joepvanderzanden <joep_vd_zanden_at_hotmail.com> wrote:
>
> Hi all,
>
> I'm trying to make multiple lattice contour plots which have the same color
> key, to allow good comparisons. However, I run into some problems when
> fitting the plots to the color key. Basically my strategy to tackle this
> problem was:
> 1) define a color key for all plots;
> 2) calculate the variable range for each plot;
> 3) calculate the range of colors from the color key that correspond with the
> variable range in each plot.
>
> This works fine for one plot but gives problems for an other. The code is
> shown below. Any ideas of how to solve this would be very welcome!

I think you are misunderstanding what 'cuts' does.

SInce you are plotting on the same x-y grid, why not do it together in a single plot, as is more natural? If I understand your attempts, that's what you seem to be trying to do anyway in a roundabout way. For example,

x.marginal <- seq(min(x), max(x), length.out = 50) y.marginal <- seq(min(y), max(y), length.out = 50) xy.marginal <- list(x = x.marginal, y = y.marginal)

zz1 <- loess((z1) ~ x * y)
zz2 <- loess((z2) ~ x * y)

```grid <- expand.grid(xy.marginal)
grid[, "fit1"] <- c(predict(zz1, grid))
grid[, "fit2"] <- c(predict(zz2, grid))

contourplot(fit1 + fit2 ~ x * y, data = grid,
layout = c(1, 2), as.table = TRUE,
at = deltaseq,
region = TRUE,
col.regions = rev(rainbow(n = numbcolstotal - 2)),
colorkey = list(at = deltaseq, labels=list(at=deltaseq)),
xlab="distance to river (m)",
ylab="lambda (/d)",
strip = strip.custom(factor.levels = c("Mean Z, L Med Clay, Grass",
"Mean Z, L Med
```
Clay, Grass")))

-Deepayan

>
>
> Joep van der Zanden
> MSc student Hydrology and Water Quality
> Wageningen University, The Netherlands / University of Sydney, Australia
>
>
> # variable to plot: groundwater heads for 2 situations (L Med Clay Grass; S
> Clay Loam Trees): z2 and z1
> # x: range of distances to river
> # y: range of lambda values
>
> ## define input data
> x <- vector(mode="numeric", length=64)
> for(i in 1:8){x[((i-1)*8+1):(i*8)]<- c(0,25,65,140,240,390,590,840)}
> y <- vector(mode="numeric", length=64)
> for(i in 1:8){y[((i-1)*8+1):(i*8)]<- c(.1,.2,.3,.4,.5,.6,.7,.8)[i]}
>
> #Groundwater heads L Med Clay Grass:
> z1<-c(-2.500000,-2.505624,-2.518618,-2.545173,-2.573554,-2.602523,-2.618828,-2.626615,
>
> -2.500000,-2.605805,-2.753376,-2.975772,-3.176528,-3.384182,-3.522155,-3.600701,
>
> -2.500000,-2.665630,-2.899457,-3.264895,-3.622645,-4.027824,-4.344277,-4.552091,
>
> -2.500000,-2.681250,-2.937271,-3.338646,-3.736416,-4.193448,-4.559524,-4.805040,
>
> -2.500000,-2.704198,-2.995056,-3.458425,-3.932201,-4.493279,-4.964342,-5.291613,
>
> -2.500000,-2.714743,-3.021087,-3.511101,-4.015649,-4.616914,-5.126386,-5.482788,
>
> -2.500000,-2.714741,-3.021320,-3.512125,-4.018124,-4.621621,-5.133251,-5.491187,
>
> -2.500000,-2.714734,-3.021383,-3.512441,-4.018981,-4.623628,-5.137014,-5.496542)
>
> #Groundwater heads S Clay Loam Trees:
> z2<-c(-3.000098,-3.097126,-3.239426,-3.473582,-3.721368,-4.020646,-4.277272,-4.457723,
>
> -3.000100,-3.107293,-3.263420,-3.518309,-3.786358,-4.109567,-4.386844,-4.582058,
>
> -3.000100,-3.110692,-3.271133,-3.531996,-3.805475,-4.134907,-4.417481,-4.616469,
>
> -3.000100,-3.110448,-3.270550,-3.531059,-3.804453,-4.134001,-4.416834,-4.616037,
>
> -3.000100,-3.111242,-3.272157,-3.533380,-3.806871,-4.136071,-4.418223,-4.616824,
>
> -3.000100,-3.110467,-3.270084,-3.528994,-3.799932,-4.125979,-4.405361,-4.601977,
>
> -3.000100,-3.110983,-3.271263,-3.531276,-3.803556,-4.131419,-4.412594,-4.610585,
>
> -3.000100,-3.111398,-3.272309,-3.533291,-3.806408,-4.135115,-4.416826,-4.615104)
>
>
> ##step 1 define color key
> bynumb <- .2 # delta
> deltaseq <- seq(-5.8,-2,by=bynumb) # define color key
> numbcolstotal <- length(deltaseq)-1 # number of colors in color key
>
>
> #define plot for L Med Clay Grass:
> x.marginal <- seq(min(x), max(x), length.out = 50)
> y.marginal <- seq(min(y), max(y), length.out = 50)
> xy.marginal <- list(x = x.marginal, y = y.marginal)
> zz <- loess((z1) ~ x * y)
> grid <- expand.grid(xy.marginal)
> grid[, "fit"] <- c(predict(zz, grid))
>
>  # step 2 calculate the range of groundwater heads for this plot:
> rangehere<-seq(floor(min(z1)/bynumb)*bynumb,ceiling(max(z1)/bynumb)*bynumb,by=bynumb)
>  # rangehere appears to be seq(-5.6, -2.4, by = -.2, length = 17)
>  # step 3 calculate the range of colors that fit to the range of groundwater
> colorrange<-round(seq(((rangehere[1]-deltaseq[1])/bynumb)+1,((max(rangehere)-deltaseq[1])/bynumb),1))
>  # colorrange is 1 to 16 --> should be correct.
>
> plot1 <- contourplot(fit~x*y, data = grid,cuts =
> length(colorrange)-2,region=TRUE,
>    col.regions=rev(rainbow(n=numbcolstotal))[colorrange],
>
> colorkey=list(col=rev(rainbow(n=numbcolstotal)),at=deltaseq,labels=list(at=deltaseq)),
>    xlab="distance to river (m)", ylab="lambda (/d)", main="Mean Z, L Med
> Clay, Grass")
>
> ## do the same for S Clay Loam Trees.
> x.marginal <- seq(min(x), max(x), length.out = 50)
> y.marginal <- seq(min(y), max(y), length.out = 50)
> xy.marginal <- list(x = x.marginal, y = y.marginal)
> zz <- loess((z2) ~ x * y)
> grid <- expand.grid(xy.marginal)
> grid[, "fit"] <- c(predict(zz, grid))
>
> # step 2
> rangehere<-seq(floor(min(z2)/bynumb)*bynumb,ceiling(max(z2)/bynumb)*bynumb,by=bynumb)
> # step 3
> colorrange<-round(seq(((rangehere[1]-deltaseq[1])/bynumb)+1,((max(rangehere)-deltaseq[1])/bynumb),1))
>
> plot2 <- contourplot(fit~x*y, data = grid,cuts =
> length(colorrange)-2,region=TRUE,
>    col.regions=rev(rainbow(n=numbcolstotal))[colorrange],
>
> colorkey=list(col=rev(rainbow(n=numbcolstotal)),at=deltaseq,labels=list(at=deltaseq)),
>    xlab="distance to river (m)", ylab="lambda (/d)", main="Mean Z, S Clay
> Loam, Trees")
>
> print(plot1, position=c(0, 0.5, 1, 1), more=TRUE)
> print(plot2, position=c(0,0,1,0.5))
>
> # --> second plot is OK, first one not...
> --
> View this message in context: http://r.789695.n4.nabble.com/Same-color-key-for-multiple-lattice-contour-plots-tp3316654p3316654.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help