这是一些转换的代码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)
Created on 2023-04-12 with reprex v2.0.2
标签的位置应该为您提供有关如何定位网格物体的提示。
顺便说一句,这段代码中可能存在一个错误:如果任何弧线绕圆超过一半,它们就会被绘制在错误的方向上。如果这是一个问题,请将这些部分分成几个弧线。