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

2023-12-13

我想在 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 中做到这一点。

附:转换为3D极树图后,我想通过translate3d在指定位置添加3D网格。因此,我希望任何解决方案都能够通过添加新的 3D 网格来进一步编辑此 3D 极树图。谢谢。


这是一些转换的代码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

标签的位置应该为您提供有关如何定位网格物体的提示。

顺便说一句,这段代码中可能存在一个错误:如果任何弧线绕圆超过一半,它们就会被绘制在错误的方向上。如果这是一个问题,请将这些部分分成几个弧线。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 R 的 rgl 窗口中创建 3D 极树图 的相关文章

  • 在函数中调用其他列的控制流程

    我正在尝试在给定条件的情况下连接到函数中的其他列 本质上 我想让数据框在给定条件的情况下从长到宽 其中一列中的这些值是NA相对于同一行中具有值的另一列 转动NAs转化为特定的数字 尽管分配的值必须是特定于列的 因此 如果2010 has N
  • R data.table:在当前测量之前对出现次数进行计数

    我有一组在几天内进行的测量结果 测量次数通常为 4 任何测量中可以捕获的数字范围为 1 5 在现实生活中 给定测试集 范围可能高达 100 或低至 20 我想每天计算每个值在当天之前发生的次数 让我用一些示例数据来解释 test data
  • 将整数“20160119”转换为“日”“年”“月”的不同列

    如何将一列整数转换为日期 DATE PRCP 1 19490101 25 2 19490102 5 3 19490118 18 4 19490119 386 5 19490202 38 到这样的表 days month years PRCP
  • 计算任意网格的平面 UV 坐标

    我有从二维形状生成三角形网格的代码 因为在大多数情况下 这些形状在表面上的顶点分布不均匀 所以我在生成 UV 时遇到了问题 这样就不会导致纹理扭曲 任何人都可以推荐一些讨论平面网格上 UV 计算技术的文章 书籍 代码示例吗 谢谢 好的 让我
  • R 如何按行值进行分组、拆分或子集

    这是上一个问题的延续R 如何按行值分组 分裂 https stackoverflow com questions 64602607 r how to group by row value split 输入数据帧的变化是 id str c x
  • 使用 R 中“rpart”包中的生存树来预测新的观察结果

    我正在尝试使用 R 中的 rpart 包来构建生存树 并且我希望使用这棵树来对其他观察结果进行预测 我知道有很多涉及 rpart 和预测的问题 但是 我还没有找到任何解决 我认为 特定于将 rpart 与 Surv 对象一起使用的问题的方法
  • Quanteda 包,朴素贝叶斯:如何预测不同特征的测试数据?

    I used quanteda textmodel NB创建一个模型 将文本分类为两个类别之一 我将模型拟合到去年夏天的训练数据集上 现在 我今年夏天尝试使用它对我们在工作中收到的新文本进行分类 我尝试这样做并收到以下错误 Error in
  • 无法将 bigrquery 与标准 sql 一起使用

    我正在 R ipython 笔记本 相对 R 新手 中工作 并尝试使用 bigrquery 从 Google Big Query 中提取数据 我被告知这应该很简单 但使用标准 sql 提取不起作用 这是我的代码 require bigrqu
  • 在 matlab 中求 3d 峰的体积

    现在我有一个带有峰值的 3D 散点图 我需要找到其体积 我的数据来自图像 因此 x 和 y 值表示 xy 平面上的像素位置 z 值是每个像素的像素值 这是我的散点图 scatter3 x y z 20 z filled 我试图找到数据峰值的
  • 滚动最小值,固定起点[重复]

    这个问题在这里已经有答案了 好的 我想计算数据框中的滚动最小值 向下滚动列 到目前为止 我无法确定该系列的起点并滚动到结尾 我努力了 mins lt c 10 5 6 10 6 6 7 8 2 12 roll min expected lt
  • R 版本 4.0.0 上的 ROracle

    当尝试使用 ROracle 时 我收到以下错误消息 gt library ROracle Error package or namespace load failed for ROracle package ROracle was inst
  • 获得各州的边界

    编辑7 经过相当多的帮助后 我已经能够得到一张接近我需要的结果的地图 但我仍然需要在地图上显示州边界 但我无法弄清楚 为了制作一个合适的可重现示例 我需要链接到数据集 因为输出太大 为了使事情变得简单 我只对三个状态进行子集化 但边界线不显
  • 在R中重新排序字母数字年龄组

    假设这就是 R 给我的 df1 data frame grp c lt 2 2 5 21 26 27 32 6 10 val rep 0 5 grp val 1 lt 2 0 2 2 5 0 3 21 26 0 4 27 32 0 5 6
  • R 中 bquote 中的拼接

    假设我正在使用 R 的反引号运算符构建一个表达式bquote 并且我想在列表中的特定位置 拼接 即丢失列表的外括号 例如 我有表达式 5 4 我想在其开头添加 6 而不使用字符串操作 即完全对符号结构进行操作 So gt b quote 5
  • 如何编辑 R 函数的源代码?

    我正在与earlywarnings包 并想编辑其中编写的函数之一qda ews功能 我可以fix 但我想编辑的功能由于某种原因在我使用时没有列出fix 该函数称为generic RShiny 这是 github 的链接 https gith
  • 如何在 ifelse 中使用示例

    我有以下清单 x rep a 100 如果我使用下表 ifelse x a sample c 1 100 1 0 当我第一次运行时 我得到以下输出 1 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22
  • 如何定义 Flexdashboard 上社交网络图标的链接?

    我向 Flexdashboard 的导航栏添加了社交图标 但无法为每个图标添加适当的链接 在 R Markdown 文件中 我添加了 output flexdashboard flex dashboard social twitter fa
  • dplyr:评估错误:对象“。”在 gamlss 中找不到,但在 lm、gam、glm 方法中都很好

    语境 tidyverse and dplyr环境 工作流程 我希望了解如何解决以下问题 这是我在尝试处理回归结果集合时遇到的 这个最小的可重复性显示了问题 mtcars gt gamlss mpg hp wt disp data gt mo
  • 如何使用 dplyr 将 2 个列集的内连接的列名称作为变量传递

    我一直在研究各种将列名作为变量传递的建议方法 例如使用 bang bang xvar as name xvar 和其他各种方法 但我无法让它工作 有谁知道如何传递使用的列名mtcars在下面的管道中作为变量 i e xvar lt mpg
  • 在 R 中,如何估算右删失缺失数据以遵循假设的分布?

    早些时候 已经解释了左删失数据遵循假设分布的随机插补here https stackoverflow com questions 76346589 in r how to impute left censored missing data

随机推荐