[R] create a '3D line plot'

Duncan Murdoch murdoch.duncan at gmail.com
Sun Sep 12 16:50:00 CEST 2010


On 12/09/2010 10:12 AM, Karl Brand wrote:
> Esteemed useRs and developeRs,
> 
> I need to create a  '3D line plot' (proper name?) of which an excellent 
> example can be viewed here:
> 
> http://cococubed.asu.edu/images/87a/images/unknown_pleasures.jpg
> 
> I have some experience using the rgl package to create 3D PCA plots, but 
> have no idea where to start for an image like this.
> 
> I'd really appreciate suggestions & help on how might achieve this using R,

I wouldn't use rgl for that:  it's really a flat 2D plot, without 
perspective, shading or anything else that 3D plots have other than 
foreground objects hiding background ones.  You can draw it by using 
polygon() to hide the background then lines() to draw the lines.

Here's an example using regular 2d graphics:


n <- 60
m <- 50
x <- seq(-4,4, len=m)

# Make up some fake y data

y <- matrix(NA, n, m)
for (i in 1:n) y[i,] <- dnorm(x)*runif(m, 0.5,1)

par(bg="black")
yrange <- range(c(y, y+n/20))

plot(x, x, type="n", axes=FALSE, bg="black", ylim=yrange)

for (i in n:1) {
   y1 <- c(y[i,] + i/20, 0, 0)
   x1 <- c(x, x[m], x[1])
   polygon(x1,y1,col="black")

   lines(x, y[i,] + i/20, col="white")

}

The problem with this kind of plot is that it's almost impossible to 
display a usable vertical scale.

Duncan Murdoch



More information about the R-help mailing list