[R] Rgl and plotmath symbols (via sprites): a trial
murdoch.duncan at gmail.com
Fri Sep 9 17:26:37 CEST 2011
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
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.
> 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)
> ## plot
> persp3d(u, v, z, aspect="iso", front="line", lit=FALSE, axes=FALSE, xlab="",
> ylab="", zlab="")
> 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