1
votes

I'm trying to put multiple background legends on an rgl plot (in my real-world example, one for lines and one for points), and I would like them to be in different corners of the screen. It seems that the default behavior of rgl is to replace an older legend when a new one is called. The following code, modified from the rgl legend3d example, illustrates this:

library(rgl)
x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
open3d()
par3d(windowRect = c(100, 100, 612, 612))
plot3d(x, y, z)
legend3d(x = 0, y = 0, xjust = 0, yjust = 0, legend = c("2D", "3D"), pch = c(1, 16))
legend3d(x = 1, y = 0, xjust = 1, yjust = 0, legend = c("2D", "3D"), pch = c(1, 16))

What can I do to work around this behavior and get multiple 2D graphics to appear?

1

1 Answers

2
votes

legend3d() makes a background using legend() after plot(). So it can't make multiple legends. It would be better to use bgplot3d().

open3d()
par3d(windowRect = c(100, 100, 612, 612))

plot3d(x, y, z)

bgplot3d({
  par(mar = c(0, 0, 0, 0))
  plot(0, 0, type = "n", xlim = 0:1, ylim = 0:1, xaxs = "i", 
       yaxs = "i", axes = FALSE, bty = "n")
  legend(x = 0, y = 0, xjust = 0, yjust = 0, legend = c("2D", "3D"), pch = c(1, 16))
  legend(x = 1, y = 0, xjust = 1, yjust = 0, legend = c("2D", "3D"), pch = c(1, 16))
})

enter image description here