[R] plotting a marginal distribution on the plane behind a persp() plot

Duncan Murdoch murdoch.duncan at gmail.com
Tue Oct 15 18:59:46 CEST 2013


On 15/10/2013 11:38 AM, Colin Rowat wrote:
> R'istas:
>
> I am trying to plot a marginal distribution on the plane behind a persp() plot.  My existing code is:
>
> library(MASS)
>
> X <- mvrnorm(1000,mu=c(0,0),Sigma=matrix(c(1,0,0,1),2))
>
> X.kde <- kde2d(X[,1],X[,2],n=25) # X.kde is list: $x 1*n, $y 1*n, $z n*n
>
> persp(X.kde,phi=30,theta=60,xlab="x_b",ylab="x_a",zlab="f") ->res
>
> Any suggestions are very appreciated.

I would suggest not using persp() (use rgl::persp3d instead), but you 
can do it in persp using the same technique as in the 2nd example in the 
?persp help page.   The difficulty with doing this is that persp() uses 
the painter's algorithm for hiding things, so if you want something 
hidden, you need to draw it first.  That's not always easy....

rgl::persp3d maintains a depth buffer so the order in which you draw 
things usually doesn't matter.  (The exception is with semi-transparent 
objects.)

Duncan Murdoch



More information about the R-help mailing list