在R

时间:2016-12-16 18:38:23

标签: r plot 3d margins

长期读者;这是第一次发布的海报。我发帖是因为我正在尝试使用自定义轴创建多面板三维图,我似乎无法在不切断轴标签的情况下进行。

更具体地说,我正在尝试使用persp3D()包中的plot3D创建一个情节。这是一个使用persp()的函数。设置persp(..., axes = F)以关闭轴很容易。从那里开始,就像persp()一样,我可以使用trans3d()添加行,文本等,如其他地方所示(例如,link)。添加轴和轴标签的工作正常,但我无法弄清楚如何增加边距以适应我添加到绘图中的轴标签。

这是一个可重复的例子:

par(mfrow = c(1,3))
# fake data
test.mat <- matrix(runif(100)*1:10, nrow = 10)

# describe axis parameters
x.axis <- seq(from = 0, to = 1, by = .1)
min.x <- min(x.axis); max.x <- max(x.axis)
y.axis <- seq(from = 0, to = 1, by = .1)
min.y <- min(y.axis); max.y <- max(y.axis)
z.axis <- pretty(seq(from = min(test.mat), to = max(test.mat), by = 0.1))
min.z <- min(z.axis); max.z <- max(z.axis)

# 3d plot without axes
pmatt <- persp(test.mat, box = F, theta = 35, axes = F, xaxs = "i")

# just a little function for plotting the axes
bbox <- function(min.x, max.x, min.y, max.y, min.z, max.z, pmat, ...){
    lines(trans3d(min.x, min.y, c(min.z, max.z), pmat), ...)
    lines(trans3d(min.x, max.y, c(min.z, max.z), pmat), ...)
    lines(trans3d(max.x, max.y, c(min.z, max.z), pmat), ...)
    lines(trans3d(c(min.x, max.x), max.y, max.z, pmat), ...)
    lines(trans3d(min.x, c(min.y, max.y), max.z, pmat), ...)
}

# print axes
bbox(min.x, max.x, min.y, max.y, min.z, max.z, pmat = pmatt)

# create ticks and labels, and place them with trans3d
tick.start <- trans3d(x.axis, min.y, min.z, pmatt)
tick.end <- trans3d(x.axis, (min.y - 0.10), min.z, pmatt)
segments(tick.start$x, tick.start$y, tick.end$x, tick.end$y)
label.pos <- trans3d(x.axis[], (min.y - 0.15), min.z, pmatt)
text(label.pos$x, label.pos$y, labels = c(x.axis), adj = c(1, 0.95), srt = 0, cex = 1)

tick.start <- trans3d(max.x, y.axis, min.z, pmatt)
tick.end <- trans3d((max.x + 0.1), y.axis, min.z, pmatt)
segments(tick.start$x, tick.start$y, tick.end$x, tick.end$y)
label.pos <- trans3d((max.x + 0.15), y.axis, min.z, pmatt)
text(label.pos$x, label.pos$y, labels = c(x.axis), adj = c(0, 0.95), srt = 0, cex = 1)

tick.start <- trans3d(min.x, min.y, z.axis, pmatt)
tick.end <- trans3d(min.x, (min.y - 0.1), z.axis, pmatt)
segments(tick.start$x, tick.start$y, tick.end$x, tick.end$y)
label.pos <- trans3d((min.x - 0.15), min.y, z.axis[-1], pmatt)
text(label.pos$x, label.pos$y, labels = z.axis[-1], adj = c(1.5, NA), srt = 0, cex = 1)

而且,总的来说,标签是以边际截止的。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我在寻找同一问题的答案时找到了您的问题。最后,我通过将参数xpd = T添加到text()的调用中来解决它。 E.g,

text(label.pos$x, label.pos$y, labels = c(x.axis), adj = c(1, 0.95), srt = 0, cex = 1, xpd = T)

这会扩展x轴整个长度的标签,而不是将其剪掉。