[R] 3d plotting alternatives. I like persp, but regret the lack of plotmath.

Duncan Murdoch murdoch.duncan at gmail.com
Thu Dec 29 21:56:59 CET 2011

On 11-12-29 1:01 PM, Paul Johnson wrote:
> I have been making simple functions to display regressions in a new
> package called "rockchalk".  For 3d illustrations, my functions use
> persp, and I've grown to like working with it.  As an example of the
> kind of things I like to do, you might consult my lecture on
> multicollinearity, which is by far the most detailed illustration I've
> prepared.
> http://pj.freefaculty.org/guides/stat/Regression/Multicollinearity/Multicollinearity-1-lecture.pdf
> I used persp mainly because I can understand it, and it can be made to
> work like plot in R, with additional tools like lines and points and
> such.  I don't want to interact with these plots, I just need to put
> them into lectures&  documents relatively easily.  And I've also
> succeeded in turning them into flash video with R2SWF, which works
> great!
> Last summer, I put together some lecture notes illustrating persp,
> scatterplot3d, and persp3d.
> http://pj.freefaculty.org/guides/Rcourse/plot-3d/plots-3d.pdf.  As I
> review that now, I see I did not make any progress on the lattice
> based plotters, or I did not write it down, anyway.  scatterplot3d did
> almost everything I needed to do, but not everything, and so I used
> persp in my newer effort.
> Then I put some plot math in an axis label and ran into the problem
> that everybody else who uses persp finds, eventually. persp doesn't
> allow expressions in axis labels.  From searching in r-help, I see
> that many people have run up against the same trouble. Most people say
> "too bad", I'll switch to some other tool.

It doesn't get nicely rotated like the default labels and it requires 
some manual work, but you can get labels on there.  For example, 
run(example(persp)) to create x, y, z values.  Then to display an 
expression for the z label:

M <- persp(x,y,z, zlab="", col="green", theta=-30, phi=30)
p <- trans3d(min(extendrange(x, f=0.1)), max(extendrange(y, f=0.1)), 
mean(range(z)), M)
text(x=p$x, y=p$y, label=expression(x*y^2), xpd=NA)

> Suggested alternatives.
> 1. Use wireframe or cloud in lattice. They can handle plotmath.
> I've been studying that, and it can handle plot math, but I just can't
> get the kinds of graphs I want from it.  In the persp framework, I can
> draw the 3d plot, and then add details to it one by one.  I can
> comprehend the little steps.  In wireframe and cloud, it *appears* to
> me i have to pack all of that work into one single command, and it is,
> well, either too difficult or impossible.  Or perhaps I'm just not
> understanding the documentation.  If I could make the sorts of plots I
> need with lattice tools, I would do it.  But I'm really blocked at the
> front door by the "write one giant function call" nature of it.
> I realize that's vague because I have not told you specifically what I
> want to do.  If there is a lattice expert reading, can you give me
> some HOWTO hints?  Take, for example, Slide 19 in this one:
> http://pj.freefaculty.org/guides/stat/Regression/Multicollinearity/Multicollinearity-1-lecture.pdf
> gray dots in the x1-x2 plane, blue points hovering above, red pointed
> arrows from gray to blue.
> And then later on, say slide 36-60, I have regression planes drawn in
> with the arrows from the plane to the points.
> 2. Use rgl based tools. These ones are especially neat if you want to
> interact with the graph--"spin" a 3d graph or get a display with
> lively colors.

But you can't do plotmath, and that's unlikely to change for a while. 
Eventually it might, but it's a lot of work.

> It works more like plot, in the sense that you can draw the figure and
> then add components with points3d and so forth.  And there are nice
> working examples of extensions in misc3d.  And also, the R CMDR
> interface has a working pull down menu system that can make rgl 3d
> graphs.  That's a big plus.
> After playing with some examples, I see these troubles. The only
> output device that runs well (finishes and does not generate massive
> files) is png.  The output quality on the screen is quite beautiful,
> but transition to black-and-white is not as nice looking as persp, in
> my opinion.  These graphs draw much more slowly.  They are more
> difficult to script out in an Sweave document, it seems to me.

You need to set your window as large as you possibly can, because the 
png is just a bitmap copy.  They are easier to put into Sweave documents 
as of R 2.14.0, just do something like

<<fig=TRUE, pdf=FALSE, grdevice=rgl.Sweave>>=

Setting windowRect to a large value using par3d() or by setting it in 
r3dDefaults before this chunk will make the bitmap quality much better. 
  Or you can try for pdf or eps output, but those tend not to work so well.

> If those criticisms are wrong, I would be glad to know.
> So I'm back wondering why persp can't be "updated".
> Nobody has explained why it is not possible to revise persp to allow
> expressions in axis labels.  Perhaps nobody has done it because people
> think that persp has no fans :)   But I'm a fan.

I doubt if it is impossible, it's just not going to be easy, because 
persp is mainly written in C, and is a fairly big piece of code.  But go 
ahead and do it (or hire someone to do it).

Duncan Murdoch

More information about the R-help mailing list