LeelaSella
LeelaSella

Reputation: 807

adding a second 3dplot

I have a 3d scatterplot produced as follows:

library(rgl)
N <- 10000

X <- rnorm(N,0,1)
Y <- rnorm(N,0,1)

Z <- X * Y

want <- Z >0 & X>0

palette <- colorRampPalette(c("blue", "green", "yellow", "red")) 
col.table <- palette(256)
col.index <- cut(Z, 256)
plot3d(X,Y,Z, col=col.table[col.index])
grid3d(c("x", "y", "z"))

This works fine. Now I want to overlay another plot, so I tried this:

par(new=F)
plot3d(X[want],Y[want],Z[want], col="black")

However this fails - it just overwrites the old plot. Is there a way to overlay the new plot ?

Upvotes: 4

Views: 3541

Answers (3)

ClimateUnboxed
ClimateUnboxed

Reputation: 8087

It is using a different package but with the scatterplot3d package, you can add points using the points3d attribute:

library(scatterplot3d)

# main scatterplot
s3d<-scatterplot3d(x1,y1,z1,color="black",
         type="l",box=FALSE,highlight.3d=F,
         xlab="x",ylab="y",zlab="z")

# add some points 
s3d$points3d(x2,y2,z2,col="red",pch=20)  

# add a line
s3d$points3d(x3,y3,z3,col="blue",type='l')  

Upvotes: 1

Lars Lau Raket
Lars Lau Raket

Reputation: 1974

A very simple solution is to use the add = TRUE argument:

plot3d(X[want], Y[want], Z[want], col = 'black', add = TRUE)

Upvotes: 4

Ben Bolker
Ben Bolker

Reputation: 226427

Although I haven't tested it, I think you should start by trying points3d instead of plot3d ... and FYI par(new=FALSE) doesn't have any effect on rgl plots at all, only base plots.

Upvotes: 4

Related Questions