这个
plotrix::thigmophobe
函数计算方向,以尝试阻止二维绘图中的标签重叠。
rgl
没有任何等价物,而且由于可以旋转绘图,因此始终可以旋转彼此上方的标签。但是,下面的函数尝试为一个特定视图放置标签,以便它们不会重叠。
thigmophobe.text3d <- function(x, y = NULL, z = NULL, texts, ...) {
xyz <- xyz.coords(x, y, z)
# Get the coordinates as columns in a matrix in
# homogeneous coordinates
pts3d <- rbind(xyz$x, xyz$y, xyz$z, 1)
# Apply the viewing transformations and convert
# back to Euclidean
pts2d <- asEuclidean(t(par3d("projMatrix") %*%
par3d("modelMatrix") %*%
pts3d))
# Find directions so that the projections don't overlap
pos <- plotrix::thigmophobe(pts2d)
# Set adjustments for the 4 possible directions
adjs <- matrix(c(0.5, 1.2,
1.2, 0.5,
0.5, -0.2,
-0.2, 0.5),
4, 2, byrow = TRUE)
# Plot labels one at a time in appropriate directions.
for (i in seq_along(xyz$x))
text3d(pts3d[1:3, i], texts = texts[i],
adj = adjs[pos[i],], ...)
}
上面的函数有一些问题:它基于
rgl::text3d
而不是
plot3D::text3D
,因此可选参数是不同的;它一次打印一个标签,如果有很多标签,它不执行错误检查等,则效率可能很低。
编辑以添加:
未发行版本0.99.20
rgl公司
添加
thigmophobe3d
函数来执行此操作。你得从
https://r-forge.r-project.org/R/?group_id=234
或Github镜像
https://github.com/rforge/rgl
现在。