Introduction to data.tree
the data.tree package lets you create hierarchies,called data.tree structures.
traversal search sort operations, recursive tree programming
decorate Nodes with own fields and methods, extend the package to your nodes
neatly printing and plotting trees
conversion from and to data.frames lists and other tree structures such as dendrogram phylo objects from ape packages igraph and other
data.tree structures are bi-directional,ordered trees.
Node
#attribute: active,field,method. Get
#active: (property) a field on a node like node$position(node本身有的字段)
#field: a named value on a Node like node$cost <- 2500(用户自定义的字段)
#method: a function acting on an node like node$Revert() (OO) or Revert(node) (traditional)
#inheritance: a child Node inherits attribute from one of its ancestors. Get,SetNodeStyle```
#Tree creation
建造树的时候需要注意的是,只能使用$AddChild建造下一层的树叶子,不能跨层建立
建造的时候可以指定此次建造的叶子的变量,可以使用这个变量用$AddChild建造下一层的叶子
library(data.tree)
acme <- Node$new("Acme")
accounting <- acme$AddChild("Accounting")
software <- accounting$AddChild("software")
print(acme)
each Node is identified by its name
the name needs to be unique among siblings,such that paths to Nodes are unambiguous(清楚的)
如果不指定每个Node的name,那么将会使用AddChild()里面的默认字符串作为name
acme$AddChild("data")
acme$Get("name")
另外一种获取name的方法
acme$children[[1]]$name
输出字符串 Acme Accounting software data
向量 "Acme" "Accounting" "software" "data"
那我就可以写一个算法,将所有name存在一样的先改过来,不改变字符串的值,只是改变name
已经搞定
从data.frame建造树
要点是要确定数据框数据的pathString
library(data.tree)
library(treemap)
data(GNI2014)
head(GNI2014)
这一步是关键
GNI2014$pathString <- paste("world",GNI2014$continent,GNI2014$country,sep="/")
population <- as.Node(GNI2014)
print的打印的时候可以选择的参数
print(population,"iso3","population","GNI",limit=30)
Node exhibits reference semantics.R中所有的变量都是引用,直接修改当地的值
OO-style actives: Node$isRoot
OO-style methods: Node$Prune(pruneFun)
Classical R methods: Clone(node)
对,可以先复制过来树,然后建造删除函数,把每层位置大于10的节点都删除掉
然后给每一层添加一个省略号节点,然后再绘图
paste(rep(".",3),sep = "")
只显示15个项目的内容
print(population, limit = 15)```
Actives
#population$isRoot
#population$height
#children of node 可以用这个进行判断,如果children大于10就开始进行调用那个删除函数
population$count
#total node in the tree
population$totalCount
#population$fields
#population$fieldsAll
#population$averageBranchFactor```
#OO-style Methods
population$Get("population",filterFun = isLeaf)
population$Prune(pruneFun = function(x) !x$isLeaf || x$population>10000)
那我要定义的删除函数就是删除每个节点下位置大于10的节点
population$Get("population",filterFun = isLeaf)```
Traditional R methods
popClone <- Clone(acme)```
#tree navigation
by path 一层一层的索引
acme$IT$outsource
by position通过位置
acme$children[[1]]$children[[2]]$name
by fields
navigate by name, also by other fields with Climb method.
levelName
1 Acme Inc.
2 ¦--Accounting
3 ¦ ¦--New Software
4 ¦ °--New Accounting Standards
5 ¦--Research
6 ¦ ¦--New Product Line
7 ¦ °--New Labs
8 °--IT
9 ¦--Outsource
10 ¦--Go agile
11 °--Switch to R
acme$Climb(position=1,name="software")$path
and as a shortcut you can climb mutiple levels with a single argument
tree <- CreateRegularTree(5,5)
tree$Climb(position=c(2,3,4))$name
下面是一个比较增深理解的例子,先找到根节点的2位置的child,然后找到child的3位置的child
然后找到名字是1.2.3.4的child,继续最后寻找到名字是1.2.3.4.5的child
tree$Climb(position=c(2,3),name=c("1.2.3.4","1.2.3.4.5"))$path```
Custom fields
#we can add any custom field to any Node in a data.frame structure
software$cost <- 1000000
software$p <- 0.5
print(acme, "cost", "p")
#and there is a list of reserved names you cannot use as Node fields
#custom fields in constructor
#assign custom fields in the constructor or in the Node$AddChild method
birds <- Node$new("Aves", vulgo = "Bird")
birds$AddChild("Neognathae", vulgo = "New Jaws", species = 10000)
birds$AddChild("Palaeognathae", vulgo = "Old Jaws", species = 60)
print(birds, "vulgo", "species")
#custom fields as function
#setting a function as a field
#计算子节点这个字段的总和
birds$species <- function(self) sum(sapply(self$children, function(x) x$species))
print(birds, "species")
#and data.tree maps the self argument to the Node at hand
#this and Set method and recursion(递归), becomes a very powerful tool```
#Printing
on the left, you have the hierachy,then you have a column per variable you want to print
print(acme,"cost","p")
Formatters
you can use formatters to output a variable in a certain way
first you can set them on a Node using the SetFormat
and the formatter will be picked up as a default formatter
So you can overwrite a formatter for a sub-tree
SetFormat(acme,"p",formatFun = FormatPercent)
SetFormat(acme,"cost",formatFun = function(x) FormatFixedDecimal(x,digits=2))
print(acme,"p","cost")
Printing using Get
Formatting with the Get method overwrites any formatters found along the path
data.frame(cost=acme$Get("cost",format=function(x) FormatFixedDecimal(x,2)),
p=acme$Get("p",format=FormatPercent))```
Plotting
plot(acme)
#现在plot的当务之急是要把这个叶子分开,要不挤到那里还是非常难看的
#igraph
library(igraph)
plot(as.igraph(acme,directed=T,direction=T))```