0

I am trying to add the z=0 plane to a 3D plot. The code I use is

library(plot3D)
zero = matrix(0, 20, 20)
persp3D(x=seq(1,20), y=seq(1,20), z = Delta_B, theta = 20, xlab = "D", ylab = "IR", zlab = "B increment")
persp3D(x=seq(1,20), y=seq(1,20), z = zero, col = "black", add = T)

But the z=0 plane does not appear.enter image description here

If I jitter the plane with

zero = jitter(matrix(0, 20, 20))

Then I can see it properly.

enter image description here

In fact trying to plot the plane alone produces and empty graph.

persp3D(x=seq(1,20), y=seq(1,20), z = zero, col = "black")

enter image description here

EDIT

A partial solution would be to use

zero = jitter(matrix(0, 20, 20)) / 10000

which results in a plane that is indistinguishable from the intended one.

enter image description here

Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
Manfredo
  • 1,760
  • 4
  • 25
  • 53
  • What package does `persp3D` come from? There's `persp3d` in `rgl` but no `persp3D` in that or base R. Also your examples aren't reproducible. – Spacedman Oct 06 '19 at 13:34
  • The package is `plot3D`. The `Delta_B` surface is not reproducible but the second example where I plot the plane alone is. I guess once that is solved adding it to an existing plot should be trivial. – Manfredo Oct 06 '19 at 14:59

1 Answers1

1

Something is failing whenever persp3D is presented with a constant matrix. Usually this is because the software is trying to compute the scale of the Z range and divides by zero. I'd only expect to see this when plotting a constant plane on its own though, not when adding one.

For example, base::persp will error:

> persp(zero)
Error in persp.default(zero) : invalid 'z' limits

If you want a fully reproducible example, create a Delta_B thus:

> Delta_B = matrix(apply(expand.grid(1:20,1:20),1,function(r){sum(r)-20}),20,20)

Then:

> persp3D(x=seq(1,20), y=seq(1,20), z = Delta_B, theta = 20, xlab = "D", ylab = "IR", zlab = "B increment")
> zero = matrix(0, 20, 20)
> persp3D(x=seq(1,20), y=seq(1,20), z = zero, col = "black", add = TRUE)

Does not show the zero plane. But anything that makes the zero matrix non-constant will fix it:

> zero[1,1]=0.000001
> persp3D(x=seq(1,20), y=seq(1,20), z = zero, col = "black", add = TRUE)

That should be sufficient to submit a bug report to the maintainer. The code for persp3D is a lengthy bit of code which I'm not digging into now.

Spacedman
  • 92,590
  • 12
  • 140
  • 224