R rgl distance between axis axes and tick marks

I draw some point data using plot3d (). I would like my y-axis labels to come a little closer to my y-axis marks.

The best way I can do this is

1) first write down the data without drawing the axis

2) call axis3d () to draw the y axis and elevations, but not remove the labels.

3) request the current position of each mark in three-dimensional space. Save position in vector.

4) use mtext3d () to add labels in positions based on vector adjustment

I have a problem in step 3. I do not know how to request the position of each mark. par3d () allows you to request a series of graphical parameters, is there something similar that I can use to determine the position of each y axis?

Am I approaching this wrong? Maybe.

Here is an example code snippet, with no text added for y axis labels ....

require(rgl)
x <- rnorm(5)
y <- rnorm(5)
z <- rnorm(5)
open3d()
plot3d(x,y,z,axes=F,xlab="",ylab="",zlab="")
par3d(ignoreExtent=TRUE)
par3d(FOV=0)
par3d(userMatrix=rotationMatrix(0,1,0,0))
axis3d('y',nticks=5,labels = FALSE)
par3d(zoom=1)
par3d(windowRect=c(580,60,1380,900))
+5
source share
2 answers

One way to do this is to explicitly determine the location of the tick before drawing the axis, rather than requesting them after drawing the axis. Then you can use the option line= mtext3dto control the distance of the label labels from the axis as follows:

require(rgl)
rgl.close()
x <- rnorm(5)
y <- rnorm(5)
z <- rnorm(5)
open3d()
plot3d(x,y,z,axes=F,xlab="",ylab="",zlab="")
par3d(ignoreExtent=TRUE)
par3d(FOV=0)
par3d(userMatrix=rotationMatrix(0,1,0,0))
par3d(zoom=1)
par3d(windowRect=c(580,60,1380,900))

# and here is the trick:
my.ticks <- pretty(y, n=5)
axis3d('y', at=my.ticks, labels=rep("", 5))
mtext3d(paste(my.ticks), at=my.ticks, edge='y', line=.6)
+2
source

I found that the easiest way to control the position of the label and the tick length in axis3d is to rewrite the function with a few extra parameters ticksizeand lab_distthat can be used to overwrite the default values ​​built into the function. Default values ticksize = 0.05and lab_dist = 3reproduce the original behavior axis3d.

To get smaller ticks and closer marks, you can call them, for example.

axis3('y', nticks=5, labels = FALSE, ticksize = 0.03, lab_dist = 2)

The new function looks like this:

axis3 <- function (edge, at = NULL, labels = TRUE, tick = TRUE, line = 0, 
          pos = NULL, nticks = 5, ticksize = 0.05, lab_dist = 3, ...) 
{
  save <- par3d(skipRedraw = TRUE, ignoreExtent = TRUE)
  on.exit(par3d(save))
  ranges <- rgl:::.getRanges()
  edge <- c(strsplit(edge, "")[[1]], "-", "-")[1:3]
  coord <- match(toupper(edge[1]), c("X", "Y", "Z"))
  if (coord == 2) 
    edge[1] <- edge[2]
  else if (coord == 3) 
    edge[1:2] <- edge[2:3]
  range <- ranges[[coord]]
  if (is.null(at)) {
    at <- pretty(range, nticks)
    at <- at[at >= range[1] & at <= range[2]]
  }
  if (is.logical(labels)) {
    if (labels) 
      labels <- format(at)
    else labels <- NA
  }
  mpos <- matrix(NA, 3, length(at))
  if (edge[1] == "+") 
    mpos[1, ] <- ranges$x[2]
  else mpos[1, ] <- ranges$x[1]
  if (edge[2] == "+") 
    mpos[2, ] <- ranges$y[2]
  else mpos[2, ] <- ranges$y[1]
  if (edge[3] == "+") 
    mpos[3, ] <- ranges$z[2]
  else mpos[3, ] <- ranges$z[1]
  ticksize <- ticksize * (mpos[, 1] - c(mean(ranges$x), mean(ranges$y), 
                                    mean(ranges$z)))
  ticksize[coord] <- 0
  if (!is.null(pos)) 
    mpos <- matrix(pos, 3, length(at))
  mpos[coord, ] <- at
  x <- c(mpos[1, 1], mpos[1, length(at)])
  y <- c(mpos[2, 1], mpos[2, length(at)])
  z <- c(mpos[3, 1], mpos[3, length(at)])
  if (tick) {
    x <- c(x, as.double(rbind(mpos[1, ], mpos[1, ] + ticksize[1])))
    y <- c(y, as.double(rbind(mpos[2, ], mpos[2, ] + ticksize[2])))
    z <- c(z, as.double(rbind(mpos[3, ], mpos[3, ] + ticksize[3])))
  }
  result <- c(ticks = segments3d(x, y, z, ...))
  if (!all(is.na(labels))) 
    result <- c(result, labels = text3d(mpos[1, ] + lab_dist * ticksize[1], 
                                        mpos[2, ] + lab_dist * ticksize[2], 
                                        mpos[3, ] + lab_dist * ticksize[3], 
                                        labels, ...))
  lowlevel(result)
}
+1
source

All Articles