I want to create a polar dendrogram in 3D in rgl window in R.
I adpated the code here, which was orginally intended for creating a 2D dendrogram (not polar), to create the dendrogram in 3D rgl window:
a <- list() # initialize empty object
# define merging pattern:
# negative numbers are leaves,
# positive are merged clusters (defined by row number in $merge)
a$merge <- matrix(c(-1, -2,
-3, -4,
1, 2), nc=2, byrow=TRUE )
a$height <- c(1, 1.5, 3) # define merge heights
a$order <- 1:4 # order of leaves(trivial if hand-entered)
a$labels <- LETTERS[1:4] # labels of leaves
class(a) <- "hclust" # make it an hclust object
plot(a) # look at the result
# Convert to a dendrogram object.
ad <- as.dendrogram(a)
# dend_data contains segment information
library(ggdendro)
dend_data <- dendro_data(ad, type = "rectangle")
nodes <- dend_data$segments
# Append z value of 0 so that the dendrogram lies in a 2D plane embedded in 3D space.
nodes_3d <- cbind(nodes, z = 0, zend = 0)
nodes_3d <- nodes_3d[,c(1, 2, 5, 3, 4, 6)]
# Convert nodes_3d to nodes_3dLong, which is used by segments3d function to draw lines.
colnames(nodes_3d) <- NULL
nodes_3da <- nodes_3d[,1:3]
nodes_3db <- nodes_3d[,4:6]
nodes_3dLong <- do.call(rbind, lapply(1:nrow(nodes_3d),
function(i) rbind(unlist(c(nodes_3da[i,])),
unlist(c(nodes_3db[i,])))))
# Plot the dendrogram in 3D.
library(rgl)
open3d()
segments3d(nodes_3dLong)
The above code (fully reproducible) produces dendrogram in 3D space in rgl window. I want to convert this dendrogram to polar dendrogram in rgl window. The polar dendrogram should still lie in a 2D plane in 3D space. The only difference is that it is a polar dendrogram. For 2D images, coord_polar in ggplot2 is used to create polar dendrogram. But I do not know how to do this in 3D.
P.S. After converting to 3D polar dendrograms, I want to add 3D meshes at specified position through translate3d. So I wish any solution open to the possibility of further editing this 3D polar dendrogram by adding new 3D meshes. Thank you.