R包:TreeAndLeaf二分类树构建R包

介绍

树形图显示了二叉树,重点是表示树元素之间的层次关系。树状图包含节点、分支(边)、根和叶。根是分支和节点的来源,指示到叶的方向,即终端节点。

树形图布局的大部分空间用于排列分支和内部节点,留给叶子的空间有限。对于大的树形图,叶片标签通常被压缩到小的凹槽中。因此,当要显示的信息应该突出显示叶子时,树形图可能无法提供最佳布局。

TreeAndLeaf包旨在通过结合树形和力定向布局算法,将分析的重点转移到叶子上,从而提高树形图叶子的可视化效果。包的工作流程如图所示。

在这里插入图片描述

加载R包

library("TreeAndLeaf")
library("RedeR")
library("igraph")
library("RColorBrewer")

导入数据

data("USArrests")
head(USArrests)

在这里插入图片描述

数据处理

  • 构建一个树状图示例
hc <- hclust(dist(USArrests), "ave")
plot(hc, main="Dendrogram for the 'USArrests' dataset",
     xlab="", sub="")

在这里插入图片描述

  • 转换 hclust 数据对象 成 a tree-and-leaf 数据对象
tal <- treeAndLeaf(hc)
  • 设置图属性
tal <- att.mapv(g = tal, dat = USArrests, refcol = 0)
pal <- brewer.pal(9, "Reds")
tal <- att.setv(g = tal, from = "Murder", to = "nodeColor", 
                cols = pal, nquant = 5)
tal <- att.setv(g = tal, from = "UrbanPop", to = "nodeSize",
                xlim = c(10, 50, 5), nquant = 5)
tal <- att.addv(tal, "nodeFontSize", value = 15, index = V(tal)$isLeaf)
tal <- att.adde(tal, "edgeWidth", value = 3)

画图

#--- Call RedeR application
rdp <- RedPort()
calld(rdp)
resetd(rdp)

#--- Send the tree-and-leaf to the interactive R/Java interface
addGraph(obj = rdp, g = tal, gzoom=75)

#--- Call 'relax' to fine-tune the leaf nodes
relax(rdp, p1=25, p2=200, p3=5, p5=5, ps=TRUE)

#--- Add legends
addLegend.color(obj = rdp, tal, title = "Murder Rate", 
                position = "topright")
addLegend.size(obj = rdp, tal, title = "Urban Population Size",
               position = "bottomright")

在这里插入图片描述

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 06:52:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 06:52:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 06:52:04       58 阅读
  4. Python语言-面向对象

    2024-07-16 06:52:04       69 阅读

热门阅读

  1. STO SS1 SS2 SLS 安全释义

    2024-07-16 06:52:04       21 阅读
  2. spring事务 @Transactional

    2024-07-16 06:52:04       26 阅读
  3. @Component,@ComponentScan,@MapperScan注解

    2024-07-16 06:52:04       21 阅读
  4. 力扣第233题“数字1的个数”

    2024-07-16 06:52:04       27 阅读
  5. 设计模式-单例模式

    2024-07-16 06:52:04       23 阅读
  6. 【Android】使用广播进行两个APP之间的通信

    2024-07-16 06:52:04       27 阅读
  7. pyppeteer 鼠标点击拖动之后如何释放鼠标

    2024-07-16 06:52:04       31 阅读
  8. Map和Set

    Map和Set

    2024-07-16 06:52:04      18 阅读