如何在R中的rgl窗口中创建3D极坐标树状图

kqlmhetl  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(85)

我想在R中的rgl窗口中创建一个3D极坐标树状图。
我采用了代码here,它最初用于创建2D树状图(非极坐标),以在3D rgl窗口中创建树状图:

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)

上面的代码(完全可复制)在rgl窗口中生成3D空间中的树状图。我想在rgl窗口中将此树状图转换为极性树状图。极性树状图仍然应该位于3D空间中的2D平面中。唯一的区别是它是一个极性树状图。对于2D图像,ggplot2中的coord_polar用于创建极性树状图。但我不知道如何在3D中做到这一点。
P.S.转换为3D极树图后,我想通过translate3d在指定位置添加3D网格。因此,我希望任何解决方案都可以通过添加新的3D网格来进一步编辑此3D极树图。谢谢。

zujrkrfu

zujrkrfu1#

下面是一些代码,它通过添加极坐标来转换计算中的nodes变量,然后使用它绘制树:

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)                     # show it

# 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

# Set the gap between the ends of the tree
gap <- 0
# Set the offset from the center.  
offset <- 0

radius <- with(nodes, max(c(y, yend)) + offset)
circ <- with(nodes, max(c(x, xend)) + gap)

# Convert to polar coordinates
nodes$theta <- with(nodes, 2*pi*x/circ)
nodes$thetaend <- with(nodes, 2*pi*xend/circ)
nodes$r     <- with(nodes, (radius - y)/radius)
nodes$rend  <- with(nodes, (radius - yend)/radius)

# Extract the horizontal and vertical segments
horiz <- subset(nodes, y == yend)
vert <- subset(nodes, x == xend)

library(rgl)

open3d()
#> glX 
#>   1

# Draw the vertical segments, which are still segments
x     <- with(vert, as.numeric(rbind(r*cos(theta), rend*cos(theta))))
y     <- with(vert, as.numeric(rbind(r*sin(theta), rend*sin(theta))))
segments3d(x, y, z = 0)

# Draw the horizontal segments, which are now arcs.  Zero
# radius arcs are dropped
horiz <- subset(horiz, r > 0)
with(horiz, arc3d(from = cbind(r*cos(theta), r*sin(theta), 0),
                  to = cbind(r*cos(thetaend), r*sin(thetaend), 0),
                  center = c(0, 0, 0)))

# Draw the labels
labels <- dend_data$labels
labels$theta <- with(labels, 2*pi*x/circ)
# Add a bit to the y so the label doesn't overlap the segment
labels$r     <- with(labels, (radius - y)/radius + 0.1)
with(labels, text3d(r*cos(theta), r*sin(theta), 0, label))

# Draw a circle around the whole thing
margin <- 0.25  # The gap below the leaves
theta <- seq(from = 0, to = 2*pi, length = 50)
r <- 1 + margin
lines3d(r*cos(theta), r*sin(theta), 0)

创建于2023-04-12带有reprex v2.0.2
标签的位置应该给予您如何定位网格。
顺便说一句,这段代码中有一个可能的bug:如果任何一个圆弧超过了圆的一半,它们就会画在错误的方向上。如果这是一个问题,把这些部分分成几个圆弧。

相关问题