Jim Lemon's thigmophobe function in the plotrix package computes good directions for labels in a 2D plot. This function does the same for a particular projection in a 3D plot by projecting down to 2D and calling his function.

thigmophobe3d(x, y = NULL, z = NULL,
P = par3d("projMatrix"),
M = par3d("modelMatrix"),
windowRect = par3d("windowRect"))

## Arguments

x, y, z

point coordinates. Any reasonable way of defining the coordinates is acceptable. See the function xyz.coords for details.

P, M, windowRect

The projection and modelview matrices, and the size and position of the display in pixels.

## Details

Since thigmophobe3d projects using fixed P and M, it will not necessarily choose good directions if the user rotates the display or makes any other change to the projection.

## Note

The example below shows how to update the directions during an animation; I find that the moving labels are distracting, and prefer to live with fixed ones.

## Value

A vector of values from 1 to 4 to be used as the pos argument in text3d.

plotrix

## Author

Duncan Murdoch

text3d

## Examples

if (requireNamespace("plotrix", quietly = TRUE)) {
# Simulate some data
xyz <- matrix(rnorm(30), ncol = 3)

# Plot the data first, to establish the projection
plot3d(xyz)

# Now thigmophobe3d can choose directions
textid <- text3d(xyz, texts = 1:10, pos = thigmophobe3d(xyz))

# Update the label positions during an animation
if (interactive() && !rgl.useNULL()) {
spin <- spin3d(rpm = 5)
f <- function(time) {
par3d(skipRedraw = TRUE)
on.exit(par3d(skipRedraw = FALSE))
pop3d(id = textid)
# Need to rotate before thigmophobe3d is called
result <- spin(time)
par3d(userMatrix = result\$userMatrix)
textid <<- text3d(xyz, texts = 1:10, pos = thigmophobe3d(xyz))
result
}
play3d(f, duration = 5)
} else
textid   # just print the static display
}

{"x":{"material":{"color":"#000000","alpha":1,"lit":true,"ambient":"#000000","specular":"#FFFFFF","emission":"#000000","shininess":50,"smooth":true,"front":"filled","back":"filled","size":3,"lwd":1,"fog":true,"point_antialias":false,"line_antialias":false,"texture":null,"textype":"rgb","texmipmap":false,"texminfilter":"linear","texmagfilter":"linear","texenvmap":false,"depth_mask":true,"depth_test":"less","isTransparent":false,"polygon_offset":[0,0],"margin":"","floating":false,"tag":""},"rootSubscene":1852,"objects":{"1859":{"id":1859,"type":"points","material":{"lit":false},"vertices":"0","colors":"1","centers":"2","ignoreExtent":false,"flags":34816},"1861":{"id":1861,"type":"text","material":{"lit":false,"margin":0,"floating":true,"edge":[0,1,1]},"vertices":"3","colors":"4","texts":[["xyz[,1]"]],"cex":[[1]],"adj":[[0.5,0.5,0.5]],"centers":"5","family":[["sans"]],"font":[[1]],"ignoreExtent":true,"flags":33808},"1862":{"id":1862,"type":"text","material":{"lit":false,"margin":1,"floating":true,"edge":[1,1,1]},"vertices":"6","colors":"7","texts":[["xyz[,2]"]],"cex":[[1]],"adj":[[0.5,0.5,0.5]],"centers":"8","family":[["sans"]],"font":[[1]],"ignoreExtent":true,"flags":33808},"1863":{"id":1863,"type":"text","material":{"lit":false,"margin":2,"floating":true,"edge":[1,1,1]},"vertices":"9","colors":"10","texts":[["xyz[,3]"]],"cex":[[1]],"adj":[[0.5,0.5,0.5]],"centers":"11","family":[["sans"]],"font":[[1]],"ignoreExtent":true,"flags":33808},"1864":{"id":1864,"type":"text","material":{"lit":false},"vertices":"12","colors":"13","texts":[["1"],["2"],["3"],["4"],["5"],["6"],["7"],["8"],["9"],["10"]],"cex":[[1]],"adj":[[0.5,0.5,0.5]],"centers":"14","family":[["sans"]],"font":[[1]],"pos":[[2],[1],[3],[2],[2],[3],[4],[4],[4],[3]],"ignoreExtent":false,"flags":33808},"1856":{"id":1856,"type":"light","vertices":[[0,0,1]],"colors":[[1,1,1,1],[1,1,1,1],[1,1,1,1]],"viewpoint":true,"finite":false},"1857":{"id":1857,"type":"background","material":{"lit":false,"back":"lines"},"colors":"15","centers":"16","sphere":false,"fogtype":"none","fogscale":1,"flags":32768},"1860":{"id":1860,"type":"bboxdeco","material":{"front":"lines","back":"lines"},"vertices":"17","colors":"18","axes":{"mode":["pretty","pretty","pretty"],"step":[0.5,0.5,0.5],"nticks":[5,5,5],"marklen":[15,15,15],"expand":[1.02999997138977,1.02999997138977,1.02999997138977]},"draw_front":true,"flags":32769},"1852":{"id":1852,"type":"subscene","par3d":{"antialias":8,"FOV":30,"ignoreExtent":false,"listeners":1852,"mouseMode":{"none":"none","left":"trackball","right":"zoom","middle":"fov","wheel":"pull"},"observer":[0,0,13.7717294692993],"modelMatrix":[[1.11302042007446,0,0,-0.0866113379597664],[0,0.330434381961823,0.887362658977509,-0.559649109840393],[0,-0.907860934734344,0.322973608970642,-13.6616134643555],[0,0,0,1]],"projMatrix":[[3.73205065727234,0,0,0],[0,3.73205065727234,0,0],[0,0,-3.86370301246643,-49.6454887390137],[0,0,-1,0]],"skipRedraw":false,"userMatrix":[[1,0,0,0],[0,0.342020143325668,0.939692620785909,0],[0,-0.939692620785909,0.342020143325668,0],[0,0,0,1]],"userProjection":[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],"scale":[1.11302042007446,0.966125428676605,0.944311618804932],"viewport":{"x":0,"y":0,"width":1,"height":1},"zoom":1,"bbox":[-1.3818666934967,1.53749966621399,-1.37639570236206,1.9868471622467,-1.20343840122223,2.23749613761902],"windowRect":[0,0,256,256],"family":"sans","font":1,"cex":1,"useFreeType":true,"fontname":"NULL","maxClipPlanes":2147483647,"glVersion":"NA","activeSubscene":0},"embeddings":{"viewport":"replace","projection":"replace","model":"replace","mouse":"replace"},"objects":[1857,1860,1859,1861,1862,1863,1864,1856],"subscenes":[],"flags":36113}},"crosstalk":{"key":[],"group":[],"id":[],"options":[]},"width":480,"height":480,"context":{"shiny":false,"rmarkdown":null},"buffer":{"accessors":[{"bufferView":0,"componentType":5126,"count":10,"type":"VEC3"},{"bufferView":1,"componentType":5121,"count":1,"type":"VEC4"},{"bufferView":2,"componentType":5126,"count":10,"type":"VEC3"},{"bufferView":3,"componentType":5126,"count":1,"type":"VEC3"},{"bufferView":4,"componentType":5121,"count":1,"type":"VEC4"},{"bufferView":5,"componentType":5126,"count":1,"type":"VEC3"},{"bufferView":6,"componentType":5126,"count":1,"type":"VEC3"},{"bufferView":7,"componentType":5121,"count":1,"type":"VEC4"},{"bufferView":8,"componentType":5126,"count":1,"type":"VEC3"},{"bufferView":9,"componentType":5126,"count":1,"type":"VEC3"},{"bufferView":10,"componentType":5121,"count":1,"type":"VEC4"},{"bufferView":11,"componentType":5126,"count":1,"type":"VEC3"},{"bufferView":12,"componentType":5126,"count":10,"type":"VEC3"},{"bufferView":13,"componentType":5121,"count":1,"type":"VEC4"},{"bufferView":14,"componentType":5126,"count":10,"type":"VEC3"},{"bufferView":15,"componentType":5121,"count":1,"type":"VEC4"},{"bufferView":16,"componentType":5121,"count":1,"type":"VEC3"},{"bufferView":17,"componentType":5126,"count":19,"type":"VEC3"},{"bufferView":18,"componentType":5121,"count":1,"type":"VEC4"}],"bufferViews":[{"buffer":0,"byteLength":120,"byteOffset":0},{"buffer":0,"byteLength":4,"byteOffset":120},{"buffer":0,"byteLength":120,"byteOffset":124},{"buffer":0,"byteLength":12,"byteOffset":244},{"buffer":0,"byteLength":4,"byteOffset":256},{"buffer":0,"byteLength":12,"byteOffset":260},{"buffer":0,"byteLength":12,"byteOffset":272},{"buffer":0,"byteLength":4,"byteOffset":284},{"buffer":0,"byteLength":12,"byteOffset":288},{"buffer":0,"byteLength":12,"byteOffset":300},{"buffer":0,"byteLength":4,"byteOffset":312},{"buffer":0,"byteLength":12,"byteOffset":316},{"buffer":0,"byteLength":120,"byteOffset":328},{"buffer":0,"byteLength":4,"byteOffset":448},{"buffer":0,"byteLength":120,"byteOffset":452},{"buffer":0,"byteLength":4,"byteOffset":572},{"buffer":0,"byteLength":3,"byteOffset":576},{"buffer":0,"byteLength":228,"byteOffset":580},{"buffer":0,"byteLength":4,"byteOffset":808}],"buffers":[{"byteLength":812,"bytes":"M49JP/12Lb53YCA+IRa6vrwtsL/DdoG/AuGwvxQG4j0AncQ+HTEfPwJR/j+tdFS+oz6oPtgn\njT6hqG4/yhJGv1Ns6b1FCpq/D15nPyrbYD8zuvY+ssvJPjHILj+E1VU/oIyWP2sDuL0/Of69\nyszEP+TOST4jMw9AAAAAATOPST/9di2+d2AgPiEWur68LbC/w3aBvwLhsL8UBu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