[R] Same color key for multiple lattice contour plots

joepvanderzanden joep_vd_zanden at hotmail.com
Mon Feb 21 00:59:38 CET 2011


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!

Thanks in advance,

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
heads
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.



More information about the R-help mailing list