Shiny RGL Plot3D: Keep Plot View Orientation On Replot

我怕爱的太早我们不能终老 提交于 2019-12-01 18:09:56

Problem

The problem with the rgl plot is that it does not change the par3d rotation matrix as you rotate the plot (same for zoom). Additionally, I can't find any documentation that hints to where the rotation/zoom state is stored. Thus, we have no information on the current state of the plot, and saving/loading that state is impossible.

Solution

My solution is to use sliders to manually control the rotation/zoom of the plot; and when those sliders change, we manually update the par3d rotation matrix.

While I really don't like this solution because it takes away a lot of the convenience of plot rotation/zoom, it's the only way I was able to maintain the rotation/zoom state after updates.

  1. I defined 3 sliders representing the rotation around the X, Y, Z axes, and an additional slider representing the zoom ZOOM.
  2. Instead of rotating/zooming on the plot, use the sliders.
  3. on Slider Change: update the rotations directly in par3d
  4. When updating the plot (e.g. new data), the method I described in the original Question to save/load the state will now work because par3d contains the rotation/zoom state.

I don't know whether this changed in recent rgl versions, but for me it is working. I am using rgl version 0.95.1441.

Define a function that loads data and calls plot3d() (here it uses random data):

library(rgl)

newRGLrandom <- function(){
  n <- 10
  x <- sort(rnorm(n))
  y <- rnorm(n)
  z <- rnorm(n) + atan2(x, y)
  plot3d(x, y, z, col = rainbow(n), size=10)
}

The plot starts in the standard orientation:

newRGLrandom()

Then I rotate it by hand in the rgl window:

When I call the newRGLrandom() function again it shows the new plot in the same rotation:

newRGLrandom()

I am also able to save the user rotation:

uM <- par3d()$userMatrix

and move the plot to the stored rotation later on:

par3d(userMatrix = uM)

Saving the whole par3d also works for me:

pp <- par3d(no.readonly=TRUE)
par3d(pp)

I don't know why it didn't work when you tried it. Maybe it is OS specific? My sessionInfo() is the following:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rgl_0.95.1441

loaded via a namespace (and not attached):
[1] tools_3.2.2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!