我發現,同時控制標籤位置和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)
}