26

I have a 3D plot using RGL. I would like to make identical plots using color to highlight the distribution of some variable. To do this I would like to have identical plots, how do I find and set the orientation of a plot?

Once I make a preliminary plot, I move it around to find a nice display angle and I would like to save that angle and incorporate it into future plotting scripts. Anyone have a suggestion on how to do this?

library(rgl)
plot3d(iris) 
#play with the plot to find a good angle
#save the angle for future plots
BookOfGreg
  • 3,550
  • 2
  • 42
  • 56
zach
  • 29,475
  • 16
  • 67
  • 88
  • 9
    try `pp <- par3d(no.readonly=TRUE); ...; par3d(pp)` – Ben Bolker May 03 '13 at 15:24
  • 1
    also - is there a good way to hardcode it - i,e, save `pp` as a variable that I can incorporate into a future script without recalculating? – zach May 03 '13 at 15:30
  • Also check out `?rgl.viewpoint` – James May 03 '13 at 16:05
  • Thanks James. I could imagine taking the output of the par3D() call and hardcoding the view into my script using rgl.viewpoint - great tip. – zach May 03 '13 at 16:06

1 Answers1

22

Ben's comment basically answers your question; this just applies expand.dots to what he wrote ;)

## In an inital session:

library(rgl)
plot3d(iris) 

## Now move the image around to an orientation you like

## Save RGL parameters to a list object
pp <- par3d(no.readonly=TRUE)

## Save the list to a text file
dput(pp, file="irisView.R", control = "all")

.......

## Then, in a later session, to recreate the plot just as you had it:

library(rgl)
pp <- dget("irisView.R")
plot3d(iris)
par3d(pp)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 1
    It's safer to use `control="all"` in the `dput()` line, as that saves all of the deparsing information and ensures that the result of dget works. – Assad Ebrahim Apr 23 '14 at 17:31
  • Without `control="all"` I got an error *incorrect number of dimensions*, because dget() incorrectly flattened the userMatrix to a list. – Assad Ebrahim Apr 23 '14 at 17:32
  • @AssadEbrahim -- Thanks for noting that! I just edited the answer incorporate your suggested improvement. – Josh O'Brien Apr 23 '14 at 17:35