[R] Rgl and plotmath symbols (via sprites): a trial
murdoch.duncan at gmail.com
Fri Sep 9 20:03:05 CEST 2011
On 09/09/2011 12:19 PM, Marius Hofert wrote:
> Dear Duncan,
> attached my next trial. It seems to work :-) Unfortunately, not the printing to pdf. Not getting a high-quality pdf is certainly one of the major drawbacks. If anybody knows how to fix that, please let me know. Even when generating the labels "by hand" (with grid), the code is less than setting up a nice lattice wireframe plot... so that makes rgl a great tool.
1. remember the par3d(ignoreExtent=FALSE) call to restore the default
behaviour, or you'll be unhappy when you draw your next plot.
2. Take a look at the mouseOneAxis mouse callback in
demo(mouseCallbacks). It would make sense in your example to keep the z
axis always pointing up; that callback will do it.
And one todo item for me: it appears that the sprites aren't being
recognized as partly transparent, so are being drawn in the wrong
order. Got to fix that.
> s<- seq(0, 1, length.out=21)
> M<- function(u) apply(u, 1, min)
> u<- s
> v<- s
> z<- outer(u, v, function(u,v) M(cbind(u,v)))
> ## create z-axis label
> png("zlabel.png", bg="transparent", width=500, height=500)
> print(grid.text(expression(W(u,u)==c), rot=90, gp=gpar(fontsize=85)))
> ## plot
> persp3d(u, v, z, aspect="iso", front="line", lit=FALSE, axes=FALSE, xlab="",
> ylab="", zlab="")
sprites3d(1.4,-0.2,0.5, radius=0.5, lit=FALSE, textype="alpha", texture="zlabel.png")
> sprites3d(1.4,-0.2,0.5, radius=0.5, lit=FALSE, textype="alpha", texture="zlabel.png")
> par3d(windowRect=c(0,0,600,600), zoom=1.6)
> rgl.snapshot("W.png", fmt="png") # print to file
> On 2011-09-09, at 17:26 , Duncan Murdoch wrote:
> > On 09/09/2011 11:10 AM, Marius Hofert wrote:
> >> Dear all,
> >> Below is some code where I try to get plotmath symbols in an rgl plot. Duncan
> >> Murdoch kindly suggested to use a "sprite" for this. As you can see, on can get
> >> it to work, but my knowledge about grid and rgl is too limited to perfectly
> >> solve the problem.
> >> 1) As you can see (please rotate the plot a little bit so that (0,0,0) is "in front"),
> >> the quality of the .png seems poor. Can this be improved?
> > Generally plotting much larger and shrinking is the way to do this.
> >> 2) When I print the file to a .pdf, the label is just a black square... I assume
> >> one then has to print to png again... hmmm... not perfect.
> > rgl.postscript has a lot of limitations. This one may be unavoidable.
> >> 3) Some parts of the z-axis are covered by the label. How can this be fixed?
> >> The problem seems to be that sprites have a radius. Specifying a rectangle would
> >> be better here, but I am not sure how this works (or even if it does).
> > I think I don't see this in Windows, which shows the black text for the label on a transparent background. Do you see a solid background? Or is it just the positioning? The tick marks go out at an angle, whereas your sprite seems to be in line with the zy plane. I would increase the x value to something like 1.4 to place it properly. (Just tried that, and I think I see the problem you had: the new sprite is obscured by the old one.
> > You may need to play with the depth_test or depth_mask material properties. Not sure if those are visible on CRAN; if not, try the R-forge version.)
> > The other thing I'd recommend is that before plotting the sprite, you run par3d(ignoreExtent=TRUE) (and then set it back to the original value afterwards). This will stop the label from affecting the bounding box of the plot.
> > Duncan Murdoch
> >> Cheers,
> >> Marius
> >> require(rgl)
> >> require(grid)
> >> s<- seq(0, 1, length.out=21)
> >> M<- function(u) apply(u, 1, min)
> >> u<- s
> >> v<- s
> >> z<- outer(u, v, function(u,v) M(cbind(u,v)))
> >> ## create z-axis label
> >> png("zlabel.png", bg="transparent", width=70, height=70)
> >> grid.newpage()
> >> pushViewport(plotViewport(c(1,1,1,1)))
> >> print(grid.text(expression(W(u,u)==c),rot=90))
> >> popViewport()
> >> dev.off()
> >> ## plot
> >> persp3d(u, v, z, aspect="iso", front="line", lit=FALSE, axes=FALSE, xlab="",
> >> ylab="", zlab="")
> >> axes3d(edges=c('x--','y--','z+-'))
> >> sprites3d(1,-0.4,0.65, radius=0.5, lit=FALSE, textype="alpha", texture="zlabel.png")
> >> rgl.postscript("W.pdf", fmt="pdf") # print to file
> >> # par3d(c("userMatrix", "zoom", "FOV")):
> >> #
> >> # $userMatrix
> >> # [,1] [,2] [,3] [,4]
> >> # [1,] 0.7262191 -0.6867201 0.03195775 0
> >> # [2,] 0.2750087 0.3328032 0.90200466 0
> >> # [3,] -0.6300603 -0.6462642 0.43054202 0
> >> # [4,] 0.0000000 0.0000000 0.00000000 1
> >> #
> >> # $zoom
> >> #  1
> >> #
> >> # $FOV
> >> #  30
More information about the R-help