简体   繁体   中英

Label plot3d points with their coordinates

I have a number of points in three dimensions that I am plotting using plot3d from the rgl library. The point coordinates are stored in three vectors x , y and z . I would like to annotate each point in the plot with its coordinates, ie (x, y, z)

Using text3d I would have to create a vector of label strings from the coordinate vectors. The only solution I found so far is looping over the coordinate vectors:

library(rgl);

x = c(1, 2, 3)
y = c(4, 5, 6)
z = c(7, 8, 9)
label_vector = 1:3

for (i in 1:3) {
  l = paste("(", x[i], y[i], z[i], ")", collapse=" ")
  label_vector[i] = l
}

plot3d(x, y, z)
text3d(x, y, z, label_vector)

Is there a more elegant way to do this? Ideally, but not necessarily, in the (x, y, z) instead of the ( xyz ) format from my example.

paste works on vectors. from ?paste

If the arguments are vectors, they are concatenated term-by-term to give a character vector result.

So, you can generate the label_vector by running just one line

label_vector = paste( "("  , x,  ", " , y ,  ", " , z , ")", sep = "")

I think this will give you a lot cleaner output

library(rgl);
x = c(1, 2, 3)
y = c(4, 5, 6)
z = c(7, 8, 9)

label_vector <- paste0("[",x,",",y,",",z,"]")
plot3d(x, y, z, col= "red" , type ="s", radius=0.02)
text3d(x, y, z, label_vector,adj=c(-0.25,0))

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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