按级别绘制 data.tree 着色和标记

Plot data.tree coloring and labelling by level

我有以下 data.tree 结构。

d <- structure(list(SUBZONE = c("A1", "A2", "A3", "A4", "A8", "B10",  "B11", "B2", "B3", "B4"), 
                    ZONE = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"), 
                    ID = c(1L, 2L, 3L, 4L, 5L, 7L, 8L, 9L, 10L, 11L)), 
               .Names = c("SUBZONE", "ZONE", "ID"), 
               row.names = c(NA, 10L), 
               class = "data.frame")

d$pathString <- paste("all", d$ZONE,d$SUBZONE, sep = "/")
alltree <-as.Node(d)
plot(alltree)

根据图表和alltree$Get(function(x) c(level = x$level)):

这棵树有三个不同的层次

格式化此图时我想实现两件事:

  1. 按级别为框着色,
  2. 逐个标记。

虽然我试过了,但我不知道如何访问关卡。在这种情况下,我有 "named" 个节点,但我拥有的所有树都不是这种情况,所以我想通过其级别编号访问它们。

您可以使用 Traverse:

获取关卡中所有节点的集合
level1 <- Traverse(alltree, filterFun = function(x) x$level == 1)
level2 <- Traverse(alltree, filterFun = function(x) x$level == 2)
level3 <- Traverse(alltree, filterFun = function(x) x$level == 3)

这允许您根据需要为节点着色:

Do(level1, SetNodeStyle, style = "filled", fillcolor = "#fff200", 
   fontcolor = "black", inherit = FALSE)
Do(level2, SetNodeStyle, style = "filled", fillcolor = "#feadc9", 
   fontcolor = "black", inherit = FALSE)
Do(level3, SetNodeStyle, style = "filled", fillcolor = "#b5e51a", 
   fontcolor = "black", inherit = FALSE)

结果如下:

plot(alltree)

就绘制关卡而言,我无法在包本身中找到任何本地方法来执行此操作,但大概如果您导出为 DiagrammeR 格式,这将是可能的。