R: rgl 3D plots bounding box faces colors and strokes

Viewed 83

I am currently doing simple 3D plots with rgl and persp3d(). I tried to use bbox3d() but I cannot make the stroke around the box (the 3 planes in the back/bottom) appearing (similarly to axes3d()):

enter image description here

The problem with axes3d() is that the bounding box/planes are not filled with a color (at least I do not know how to achieve that). Thank for your time!

Here is the code to produce the Actual version:

library("rgl")
x=c(0,1,2,3,4,5,6)
y=x
z=outer(x,y,function(x,y){x+y})
persp3d(x,y,z,col="grey")
bbox3d(lwd=5,box=T,color=c("grey","black"))
1 Answers

You can get pretty close using segments3d with margin and floating properties. For example:

library("rgl")
x=c(0,1,2,3,4,5,6)
y=x
z=outer(x,y,function(x,y){x+y})
open3d()
#> glX 
#>   1
persp3d(x,y,z,col="grey", axes=FALSE)
limits <- par3d("bbox")
bbox3d(col=c("gray", "black"), polygon_offset = 1)
segments3d(x = c(-Inf, Inf),
           y=0, z=0,
           lwd=5, margin="x++",
           floating = TRUE)
segments3d(x = c(-Inf, Inf),
           y=0, z=0,
           lwd=5, margin="x--",
           floating = TRUE)
segments3d(x = c(-Inf, Inf),
           y=0, z=0,
           lwd=5, margin="y++",
           floating = TRUE)
segments3d(x = c(-Inf, Inf),
           y=0, z=0,
           lwd=5, margin="y--",
           floating = TRUE)
segments3d(x = c(-Inf, Inf),
           y=0, z=0,
           lwd=5, margin="z++",
           floating = TRUE)
segments3d(x = c(-Inf, Inf),
           y=0, z=0,
           lwd=5, margin="z--",
           floating = TRUE)

Created on 2022-03-18 by the reprex package (v2.0.1)

screenshot

It's not perfect, because the segments don't render nicely at the corners, and it looks like there's a bug in the initial display before you do any rotation. The funny coordinate system for margin objects is described in the ?mtext3d help topic.

Related