# [R] overlaying points and lines on a surface3d rgl plot with axes

Vivek Sutradhara viveksutra at gmail.com
Thu Oct 19 20:52:07 CEST 2017

```Hi R-users,
I think that I have figured out what I should do. But I would welcome any
comments clarifying any of the questions that I have raised.  (I am showing
my revised code below). The questions which are still unresolved for me are
the following :
1. With the points3d or the plot3D function, the points that are plotted
land on the backside of the surface. How do I bring them to the front. I
have partially solved the problem for points and lines by making the
surface transparent with alpha=0.5. Is there a better way?
2. For a different data set, I have to rotate the surface to arrive at the
optimum theta and phi angles. Is there a way of interactively finding out
what these angles are (i.e. from the initial position). After arriving at
this optimum angle, is there then a method of plotting the points on top of
the surface?
Thanks, Vivek

The code which worked for me :
library(rgl)
vol2 <- 2*volcano # Exaggerate the relief
library(reshape)
mvol2 <- melt(vol2)
str(mvol2)
x <- 1:nrow(vol2)
y <- 1:ncol(vol2)
zlim <- range(vol2)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen)
open3d()
persp3d(x, y, vol2, col = colorlut)
grid3d(c("x", "y+", "z"))
surface3d(x, y, vol2, color = col, back = "lines")
grid3d(c("x", "y+", "z"))
surface3d(x, y, vol2, color = col, back = "lines")

2017-10-19 16:47 GMT+02:00 Vivek Sutradhara <viveksutra at gmail.com>:

> Hi R users and experts,
> I am interested in learning more about the use of 3D plots. Specifically,
> I want to add points and lines to a surface plot. And get the axes and
> labels plotted also. Here is what I have tried with an example data set :
>
> library(rgl)
> vol2 <- 2*volcano # Exaggerate the relief
> library(reshape)
> mvol2 <- melt(vol2)
> str(mvol2)
> # First, persp and persp3d plots do not succeed
> #persp(mvol2\$X1,mvol2\$X2,vol2,xlab="X2",ylab="X1",zlab="
> value",axes=TRUE,ticktype="detailed",cex.lab=1)
> persp3d(mvol2\$X2,mvol2\$X1,vol2,axes=TRUE)
> # Error - Increasing 'x' and 'y' values expected ??
> # Next, tried to get the axes with the plot3d command
> plot3d(mvol2\$X1,mvol2\$X2,mvol2\$value,type="n",xlab="x1"
> ,ylab="x2",zlab="value")
> # then get the surface - why the x,z, y  sequence?
> surface3d(mvol2\$X1,vol2,mvol2\$X2)
>
> I get the following error message for the surface3d command :
> Error in rgl.surface(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
> :
>   Bad dimension for cols
>
> Additionally, I want points and lines plotted. For example, the points
> (1,1,200) and (51,2,232).
> I don't seem to have succeeded at any single step. I would appreciate any
> help that I can get.
> Vivek
>

[[alternative HTML version deleted]]

```

More information about the R-help mailing list