介绍
树形图显示了二叉树,重点是表示树元素之间的层次关系。树状图包含节点、分支(边)、根和叶。根是分支和节点的来源,指示到叶的方向,即终端节点。
树形图布局的大部分空间用于排列分支和内部节点,留给叶子的空间有限。对于大的树形图,叶片标签通常被压缩到小的凹槽中。因此,当要显示的信息应该突出显示叶子时,树形图可能无法提供最佳布局。
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")