[R]: (Lattice): Overlaying more than one trend surface using contourplot() and wireframe()

Deepayan Sarkar deepayan at stat.wisc.edu
Fri Aug 6 19:06:38 CEST 2004


On Friday 06 August 2004 10:47, Peter Ho wrote:
> Hi,
>
> Is there a way to plot more than one trend surface using the
> functions contourplot() and wireframe(). I have found an add=T in
> contour(), but no equivalent argument in contourplot() and
> wireframe()?
>
> I have taken the example 11-2 (pages 441-451) from Design and
> analysis of experiments (Montgomery  2001, 5th edition) to see if
> this could be done in R. I have managed to plot individual  contours
> for each response after using the surf.ls() and trmat() functions
> from the Spatial package, but have been unable to overlay the
> contours.   Ideally, it would be nice to also choose a different set
> of colours for shading the surface, so that when combing surfaces, we
> can immediately identify the optimum conditions. The final overlaid
> plot is shown on page 451 (Figure 11-16) of the book.

I don't have access to that book, so a scan or a rough sketch of what 
that looks like would be helpful.

Ideally, this sort of thing should be done by using a groups= argument. 
This needs your data to be restructured into a single data frame, e.g.:

trsurfY$z <- as.vector(trsurfY$z)
trsurfV$z <- as.vector(trsurfV$z)
trsurfMW$z <- as.vector(trsurfMW$z)

trsurf.comb <- rbind(as.data.frame(trsurfY),
                     as.data.frame(trsurfV),
                     as.data.frame(trsurfMW))
trsurf.comb$g <- 
    factor(rep(c("Y", "V", "MW"), 
           each = length(trsurfY$x)))


The subsequent call should be like 

contourplot(z ~ x * y, trsurf.comb, groups = g)

Unfortunately, contourplot has no idea how to deal with groups, so you 
will have to tell it by writing your own panel function:


contourplot(z ~ x * y, trsurf.comb, 
            subset = g %in% c("V", "Y"), 
            groups = g, cuts = 30,
            panel = function(..., groups, subscripts) {
                gvals <- levels(groups)
                for (i in gvals)
                {
         panel.levelplot(...,
             subscripts = subscripts[groups[subscripts] == i])
         ## panel.contourplot should also work here,
         ## but doesn't because I forgot to export it
                }
            })

Note that the range of the z-values in your 3rd group (MV) is very 
different from the other 2, so trying to include all 3 won't work 
unless you specify an appropriate 'at' vector that covers the ranges of 
all 3 surfaces (by default it will try to put contours at evenly spaced 
locations over the whole range, almost all of which will miss the 
data).

I have no idea what you were hoping to do with levelplots (where the 
inter-contour regions are colored).

You can do something similar with wireframe to get the 3 surfaces 
together. It does know how to handle groups, so the call is simpler. 
(Note that this doesn't really work if your surfaces intersect, but you 
can usually get by with making the grid fine enough.):


wireframe(z ~ x * y, trsurf.comb, groups = g,
          subset = g %in% c("V", "Y"), shade = TRUE)


Hope that helps,

Deepayan




More information about the R-help mailing list