Peter Ho
2004-Aug-06 15:47 UTC
[R]: (Lattice): Overlaying more than one trend surface using contourplot() and wireframe()
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.
Are there alternatve ways to also do this using other pakages ?
The R scipts for this examples can be found at the end of this email.
Thanks
Peter
..........................
################################################################################
g<- c(80,80,90,90,85,85,85,85,85,92.07,77.93,85,85)
h<- c(170,180,170,180,175,175,175,175,175,175,175,182.07,167.93)
Yield <- c(76.5,77.0,78.0,79.5,79.9,80.3,80.0,79.7,79.8,78.4,75.6,78.5,77.0)
viscosity <- c(62,60,66,59,72,69,68,70,71,68,71,58,57)
molwt <-
c(2940,3470,3680,3890,3480,3200,3410,3290,3500,3360,3020,3630,3150)
###################################################################################
## Fit 2-order Polynomial trend for Yield
Yield.ls <- surf.ls(2, g, h, Yield)
trsurfY<- trmat(Yield.ls,min(g), max(g),min(h), max(h),100)
trsurfY[c("x","y")]<-
expand.grid(x=trsurfY$x,y=trsurfY$y)
## Fit 2-order Polynomial trend for viscosity
viscosity.ls <- surf.ls(2, g, h, viscosity)
trsurfV<- trmat(viscosity.ls,min(g), max(g),min(h), max(h),100)
trsurfV[c("x","y")]<-
expand.grid(x=trsurfV$x,y=trsurfV$y)
## Fit 1-order Polynomial trend for molecular weight
molwt.ls <- surf.ls(1, g, h, molwt)
trsurfMW<- trmat(molwt.ls,min(g), max(g),min(h), max(h),100)
trsurfMW[c("x","y")]<-
expand.grid(x=trsurfMW$x,y=trsurfMW$y)
####################################################################################
### Contourplot for yield
contourplot(z ~ x*y, data = trsurfY,
cuts = 10, region = TRUE,
xlab = "Time",
ylab = "Temperature",
main = "yield",
col.regions = trellis.par.get("regions")$col)
### Contourplot for viscosity
contourplot(z ~ x*y, data = trsurfV,
cuts = 10, region = F,
xlab = "Time",
ylab = "Temperature",
main = "yield",
col.regions = trellis.par.get("regions")$col)
### Contourplot for molecular weight
contourplot(z ~ x*y, data = trsurfMW,
cuts = 10, region = TRUE,
xlab = "Time",
ylab = "Temperature",
main = "yield",
col.regions = trellis.par.get("regions")$col)
##################################################################################
Deepayan Sarkar
2004-Aug-06 17:06 UTC
[R]: (Lattice): Overlaying more than one trend surface using contourplot() and wireframe()
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