2

I'm doing Person-Environment fit research, and hope to add the P = E and P = -E lines on the xy plane of response surface drawn by Lattice wireframe. Currently, I've got the code are as follows:

PersonIV <- seq(-3, 3, length = 30)
EnvironmentIV <- seq(-3, 3, length = 30)
g.test.data <- expand.grid(PersonIV = PersonIV, EnvironmentIV = EnvironmentIV)
g.test.data$DV <- .5 + .4 * PersonIV + .6 * EnvironmentIV + .9 * PersonIV^2 + .7 * PersonIV * EnvironmentIV - .3 * EnvironmentIV^2

library(lattice)
trellis.par.set("axis.line", list(col = NA))
wireframe(DV ~ PersonIV * EnvironmentIV, g.test.data, 
          drape = T, screen = list(z = 30, x = -75), 
          main = "Person - Environment Fit Response Surface", 
          xlab = list(xlim = c(-3:3), label = "Personal IV", col = "black", font = 1, cex = 1, rot = 15), 
          ylab = list(label = "Environment IV", ylim = c(-3: 3), font = 1, cex = 1, rot = -42), 
          zlab = list(label = "DV", zlim = c(0:5), font = 1, cex = 1, rot = 90, lines = T), 
          scale = list(arrows = F, cex = 1, col = "black", tck = 1), 
          par.settings = list(box.3d = list(col=c(1,1,NA,NA,1,NA,1,1,1))), 

          )

Thanks a lot!

  • 5
    Please make your example reproducible by supplying a sample data set or using one of those that ship with **lattice** or some other package. – Josh O'Brien Dec 02 '13 at 21:30
  • 1
    Check out [the code for Figure 13.7](http://lmdvr.r-forge.r-project.org/figures/figures.html?chapter=13;figure=13_07) in Deepayan Sarkar's **lattice** book (beginning with his definition of `panel.3d.contour()`). It's not *exactly* what you want, but could be pretty easily adapted. The things you'd need to do would be set up the two lines (in the place where he sets up contours), and substitute `zlim.scaled[1]` for his `zlim.scaled[2]` (to plot the lines on the bottom rather than the top of the box. – Josh O'Brien Dec 03 '13 at 04:33
  • The results will probably be (unavoidably) unsatisfactory, because **grid** / **lattice** employ the "painters algorithm". As [Duncan Murdoch does here](https://stat.ethz.ch/pipermail/r-help/2011-May/277759.html), I might suggest instead using **rgl** which *will* deal properly with lines that fall behind surfaces. Best of luck! – Josh O'Brien Dec 03 '13 at 04:36

1 Answers1

0

Making a custom panel function and passing it to the lattice plotting function (here, wireframe) with the panel argument is a common way to do this. To recreate the existing plot, panel.xyplot is usually used, and to add lines, panel.abline is usually used. Perhaps something like this.

panel=function(...) {panel.xyplot(...); panel.abline(0,1); panel.abline(0,-1)}
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • I tried but it looks it does not work. What I hope to add is the y=x line and y = -x line on the floor of the bounding box of the wireframe response surface. It's kind of like adding contour to the floor; the only difference is I do not need contour but the y=x and y= -x lines. Tons of thanks! – user3058902 Dec 03 '13 at 03:51
  • Since it's a 3d plot, it looks like you need the `ltransform3dto3d` function and to use the endpoints of the lines instead of just the intercept/slope, as in the link Josh O'Brien provides. Shouldn't be too hard to write a new panel function similar to what I give here, or with a reproducible example, someone here might be able to help. – Aaron left Stack Overflow Dec 05 '13 at 02:47