1

我们一直在使用数据collapsibleTree()框来制作交互式、漂亮的分层系统树状图。但是我们遇到的问题是,在使用数据框时很难获得我们想要的自定义,并且从collapsibleTree文档看来,使用 data.tree 结构提供了更大的灵活性。例如,如果我们想用数据框为特定节点着色,我们需要做这样的事情:

library(dplyr)
library(data.tree)
library(collapsibleTree)

colors <- rep('gray', 15)
colors[1] <- 'red' # root
colors[3] <- 'red' # child we want to emphasize
colors[15] <- 'red' # grand child we want to emphasize

original_data_frame %>% 
  collapsibleTree(
    hierarchy = c("level_2",
                  "level_3",
                  "level_4",
                  "level_5",
                  "level_6",
                  "level_7"
    ),
    root = "level_1",
    fill = colors
  )

这并不是一个真正好的解决方案,尤其是当生成的树有数百个或更多节点或更高级别的层次结构时。

我在data.tree中看到,我应该可以添加任意属性,但这似乎不起作用,至少它没有达到我想要的效果:

original_as_tree <- original_data_frame %>% 
    mutate(pathString =
           paste(
             level_1,
             level_2,
             level_3,
             level_4,
             level_5,
             level_6,
             level_7,
             sep = "/"),
           pathString = str_remove_all(pathString, "/NA")) %>% 
  as.Node()

original_as_tree$color <- "gray"
original_as_tree$Boat$color <- "red"

collapsibleTree(original_as_tree,
                fill = original_as_tree$color)

这只是将每个节点着色为“灰色”(如果我这样做了,则为红色original_as_tree$Boat$color)。理想情况下,我可以通过某种case_whenifelse调用以编程方式分配颜色。

这是我的示例数据的输出:

structure(list(level_1 = c("Sea System", "Sea System", "Sea System", 
"Sea System", "Sea System", "Sea System"), level_2 = c("System Engineering", 
"Boat", "Boat", "Boat", "Boat", "Boat"), level_3 = c("Core Systems Engineering", 
"Auxiliary Systems", "Total Ship Integration/Engineering", "Electric Plant", 
"Hull Structure", "Hull Structure"), level_4 = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), level_5 = c(NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_), level_6 = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    level_7 = c("GREENLAND ENTERPRISES, INC.", "3M PURIFICATION INC.", 
    "5TH AXIS, LLC", "901 D, LLC", "A. M. CASTLE & CO.", "A.M. CASTLE & CO."
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
4

0 回答 0