简体   繁体   English

与 R 中的 plot3D 包重叠标签

[英]Overlapping labels with the plot3D package in R

I am currently creating a 3D scatterplot using the plot3D package in R, and I wanted to add data labels to my data points.我目前正在使用 R 中的 plot3D 包创建一个 3D 散点图,我想向我的数据点添加数据标签。 However, some of my data points have the same values with each other, and I wanted to find a solution similar to ggrepel that would offset the data labels from the points so that the labels for those points would be legible.但是,我的一些数据点彼此具有相同的值,我想找到一个类似于ggrepel的解决方案,该解决方案将数据标签从这些点偏移,以便这些点的标签清晰易读。 Sample code below:示例代码如下:

library(plot3D)
names <- c("A", "B", "C", "D", "E")
x <- c(1,1,2,3,4)
y <- c(1,1,3,4,5)
z <- c(1,1,4,5,6)

scatter3D(x, y, z)
text3D(x,y,z, names, add = TRUE, cex = 1)

The labels for A and B are currently superimposed on top of each other. A 和 B 的标签当前相互叠加。

I tried to use the directlabels package as well, and it doesn't seem to recognize the text3D or the plot3D objects.我也尝试使用directlabels包,但它似乎无法识别 text3D 或 plot3D 对象。 Any help would appreciated.任何帮助将不胜感激。

The plotrix::thigmophobe function works out directions to try to stop labels from overlapping in 2D plots. plotrix::thigmophobe函数计算方向以尝试阻止标签在 2D 图中重叠。 rgl doesn't have anything equivalent, and since you can rotate a plot, you'll always be able to rotate labels on top of each other. rgl没有任何等效的东西,并且由于您可以旋转绘图,因此您始终可以在彼此之上旋转标签。 However, the function below makes an attempt to place labels for one particular view so that they don't overlap.但是,下面的函数会尝试为一个特定视图放置标签,以免它们重叠。

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],], ...)
}

There are some problems with the function above: it is based on rgl::text3d rather than plot3D::text3D , so the optional arguments are different;上面的函数有一些问题:它基于rgl::text3d而不是plot3D::text3D ,所以可选参数不同; it plots labels one at a time, which can be inefficient if you have a lot of labels, it doesn't do error checking, etc.它一次绘制一个标签,如果你有很多标签,它可能会效率低下,它不进行错误检查等。

Edited to add:编辑添加:

The unreleased version 0.99.20 of rgl adds a thigmophobe3d function to do this.的未发行版本0.99.20 rgl增加了thigmophobe3d功能来做到这一点。 You'll have to get it from https://r-forge.r-project.org/R/?group_id=234 or the Github mirror https://github.com/rforge/rgl for now.您现在必须从https://r-forge.r-project.org/R/?group_id=234或 Github 镜像https://github.com/rforge/rgl获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM