我发现在axis3d中控制标签位置和刻度长度的最简单方法是用几个额外的参数重写函数ticksize
并lab_dist
添加可用于覆盖函数中嵌入的默认值。的默认值ticksize = 0.05
,并 lab_dist = 3
重现原来的行为axis3d
。
要获得更小的刻度和更接近的标签,您可以使用例如调用它
axis3('y', nticks=5, labels = FALSE, ticksize = 0.03, lab_dist = 2)
新函数如下所示:
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)
}