[R] Add lines (contours) to lattice wireframe plot

Andrej andrej.kastrin at gmail.com
Sat Feb 20 10:26:23 CET 2010


I do it myself. Here is the "solution".

require(lattice)

# Bivariate density function
dnorm2d <- function(x, y, rho = 0) {
  xoy = (x^2 - 2 * rho * x * y + y^2)/(2 * (1 - rho^2))
  density = exp(-xoy)/(2 * pi * sqrt(1 - rho^2))
  density
}

# Surface data
surface.data <- expand.grid(x = seq(from = -3, to = 3, length = 40), y
= seq(from = -3, to = 3,length = 40))
surface.data$z <- dnorm2d(surface.data$x, surface.data$y)

# Contour coordinates
phi <- seq(0, 2*pi, length=200)
r <- 2
x <- r * cos(phi)
y <- r * sin(phi)
z <- dnorm2d(xr,yr)
contour.data <- data.frame(x, y, z)

# Panel function
my.panel <- function(x, y, z, xlim, ylim, zlim, xlim.scaled,
ylim.scaled, zlim.scaled, contour.data,...) {
  panel.3dwire(x = x,
    y = y,
    z = z,
    xlim = xlim,
    ylim = ylim,
    zlim = zlim,
    xlim.scaled = xlim.scaled,
    ylim.scaled = ylim.scaled,
    zlim.scaled = zlim.scaled,
    ...
  )
  xx <- xlim.scaled[1] + diff(xlim.scaled) * (contour.data$x -
xlim[1]) / diff(xlim)
  yy <- ylim.scaled[1] + diff(ylim.scaled) * (contour.data$y -
ylim[1]) / diff(ylim)
  zz <- zlim.scaled[1] + diff(zlim.scaled) * (contour.data$z -
zlim[1]) / diff(zlim)
  panel.3dscatter(x = xx,
    y = yy,
    z = zz,
    xlim = xlim,
    ylim = ylim,
    zlim = zlim,
    xlim.scaled = xlim.scaled,
    ylim.scaled = ylim.scaled,
    zlim.scaled = zlim.scaled,
    type="l",
    col.line = "red",
    lwd = 2,
    ...
  )
}

# Call wireframe plot
wireframe(z~x*y, data = surface.data, contour.data = contour.data,
scales = list(arrows = FALSE), aspect = c(1, 0.5), panel.3d.wireframe
= my.panel)

On Feb 20, 1:24 am, Andrej <andrej.kast... at gmail.com> wrote:
> Hi,
>
> I draw a surface corresponding to bivariate density of independent
> variables (rho=0) using persp(). Then I add a contour line (i.e.,
> circle in my case) at a particular density. Below is a minimal example
> of what I have so far.
>
> # Bivariate density
> dnorm2d <- function(x, y, rho = 0) {
>   xoy = (x^2 - 2 * rho * x * y + y^2)/(2 * (1 - rho^2))
>   density = exp(-xoy)/(2 * pi * sqrt(1 - rho^2))
>   density}
>
> # Variables
> x <- seq(from = -3, to = 3, length = 40)
> y <- seq(from = -3, to = 3, length = 40)
> z <- outer(X = x, Y = y, FUN = dnorm2d)
> # Perspective plot
> persp(x = x, y = y, z = z) -> res
> # Add contour
> phi <- seq(0, 2*pi, length=200)
> r <- 2
> xr <- r * cos(phi)
> yr <- r * sin(phi)
> lines(trans3d(xr, yr, dnorm2d(xr, yr), res))
>
> Unfortunately, it's difficult to customize perspective plot, so I try
> wireframe in lattice instead:
>
> library(lattice)
> g <- expand.grid(x=seq(from=-3, to=3, length=40), y=seq(from=-3, to=3,
> length=40))
> g$z <- dnorm2d(g$x, g$y)
> wireframe(z~x*y, data=g)
>
> Could you give me some advice how to add lines (i.e., circle) on
> lattice graphics.
>
> Thanks for any suggestion.
>
> Best, Andrej
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list