[R] rgl: coordinating and saving viewpoints, zoom, scale for multiple images

Michael Friendly friendly at yorku.ca
Fri Dec 17 23:08:21 CET 2010

Context: I have two or more rgl-based views of a given data set, perhaps 
fitting different
models, or showing different things across views.  I want to be able to 
hand-rotate, zoom, scale
one view to something I like, and then show the other views with 
matching viewpoints and scaling.
so that one could flip back/forth among graphs and see only the relevant 

In 2D, all this usually requires is setting the same xlim, ylim and 
other constant aspects across
multiple graphs.

In 3D with rgl, I can do one graph, rotate, zoom, etc.  by hand
and capture the viewpoint with par3d("userMatrix"),
but I can't figure out how to save the zoom and scale values to 
reproduce that viewpoint in another
graph, in a way I could feed to view3d or rgl.viewpoint and get an 
equivalent view of the new data/model.

An example follows, but I don't know how to capture in code what I did 
manually to get graph 1 in the orientation,
zoom and scaling to re-create in another session or another graph....   
Which is why I'm asking for R-help.

HEC <- HairEyeColor[,c(1,3,4,2),]
# graph 1

# hand rotate, scale, then save orientation; make sure rgl window is not 
#snapshot3d("HEC3D-1a.png", top=TRUE)
M1a <- par3d("userMatrix")

# try to make this reproducible, across sessions
#> dput(M1a)
#structure(c(0.890118718147278, 0.105095967650414, -0.44344499707222,
#0, -0.454265862703323, 0.282511830329895, -0.84488445520401,
#0, 0.0364845432341099, 0.953489363193512, 0.299210667610168,
#0, 0, 0, 0, 1), .Dim = c(4L, 4L))
M1a <-
structure(c(0.890118718147278, 0.105095967650414, -0.44344499707222,
0, -0.454265862703323, 0.282511830329895, -0.84488445520401,
0, 0.0364845432341099, 0.953489363193512, 0.299210667610168,
0, 0, 0, 0, 1), .Dim = c(4L, 4L))

# graph 2
mosaic3d(HEC, type="expected")
# present this in the same orientation

Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

More information about the R-help mailing list