[R] how to create contour lines on a 3D surface plot by persp3d from rgl package

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 21 00:30:44 CEST 2015


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 20/08/2015 5:20 PM, Aki Hoji wrote:
> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512
> 
> HI,
> 
> I created a 3D surface plot of 2D surface data (generated by kde2d)
> by using persp3d from rlg as follows;
> 
> 
> persp3d(dens3d, theta=50, phi=25, expand=0.75, col=color[zcol],
> ticktype="detailed",axes=TRUE)
> 
> I would like to add contour lines to this 3D surface plot and I’d
> appreciate if any help on this.
> 
> Thanks in advance.

Use the contourLines() function to compute them, then plot them using
lines3d.

The difficulty is that they may be hidden by the surface.  The
simplest way around that is to make the surface have partial
transparency.  If you don't want to do that, plotting the lines first
will

For example:

f <- function(x, y) x^2 + y^2
x <- seq(-2, 2, len=50)
y <- seq(-2, 2, len=50)
z <- outer(x,y,f)
persp3d(x,y,z, col="red",alpha=0.5)
lines <- contourLines(x, y, z)
for (i in seq_along(lines)) {
  x <- lines[[i]]$x
  y <- lines[[i]]$y
  z <- rep(lines[[i]]$level, length(x))
  lines3d(x, y, z)
}


Duncan Murdoch
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2
Comment: GPGTools - https://gpgtools.org

iQEcBAEBCgAGBQJV1lUUAAoJEHE2Kz23YMZyVxUH/jmOwREkGy045EcbnkXCfARE
bSS7H2/kzbU5jY+vavrIDRveCQo2eYJr7yigF8HMiiRZqRqF7nEg+tt5yuQHwc4L
fYYldU4ZHohS+AQg/u21qD8Skln7+1d1D5yZj6DQqY8hU5duWdcErdNgoDt268+i
u0hsBAZDhDEBNh6+5IIGI5CF3utqS7ufJb8aEKrSDfZV5fDqSYGxRpNlYcI1Ou+n
SRoQi5jJ2FOBlSlYC3wg5ZjT5FiV7QqoymKFiUMKYz0eUWqoPkPiQDVKi054Qlim
xRyHUsaGN31Jc2817+kmjWnL7K5MFFbg/BLlLtijg5bmM9dZo/wYJ0MU6Pu8CVA=
=Sn3R
-----END PGP SIGNATURE-----



More information about the R-help mailing list