7

我想使用networkD3可视化一些深度嵌套的数据。在发送到radialNetwork.

以下是一些示例数据:

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

其中level表示嵌套的级别,并且value是节点的名称。通过使用这两个向量,我需要将数据转换为以下格式:

my_list <- list(
  name = "root",
  children = list(
    list(
      name = value[1], ## a
      children = list(list(
        name = value[2], ## b
        children = list(list(
          name = value[3], ## c
          children = list(
            list(name = value[4]), ## d
            list(name = value[5]) ## e
          )
        ),
        list(
          name = value[6], ## f
          children = list(
            list(name = value[7]), ## g
            list(name = value[8]) ## h
          )
        ))
      ))
    ),
    list(
      name = value[9], ## i
      children = list(list(
        name = value[10], ## j
        children = list(list(
          name = value[11] ## k
        ))
      ))
    )
  )
)

这是已解析的对象:

> dput(my_list)
# structure(list(name = "root",
#                children = list(
#                  structure(list(
#                    name = "a",
#                    children = list(structure(
#                      list(name = "b",
#                           children = list(
#                             structure(list(
#                               name = "c", children = list(
#                                 structure(list(name = "d"), .Names = "name"),
#                                 structure(list(name = "e"), .Names = "name")
#                               )
#                             ), .Names = c("name",
#                                           "children")), structure(list(
#                                             name = "f", children = list(
#                                               structure(list(name = "g"), .Names = "name"),
#                                               structure(list(name = "h"), .Names = "name")
#                                             )
#                                           ), .Names = c("name",
#                                                         "children"))
#                           )), .Names = c("name", "children")
#                    ))
#                  ), .Names = c("name",
#                                "children")), structure(list(
#                                  name = "i", children = list(structure(
#                                    list(name = "j", children = list(structure(
#                                      list(name = "k"), .Names = "name"
#                                    ))), .Names = c("name",
#                                                    "children")
#                                  ))
#                                ), .Names = c("name", "children"))
#                )),
#           .Names = c("name",
#                      "children"))

然后我可以将它传递给最终的绘图函数:

library(networkD3)
radialNetwork(List = my_list)

输出将类似于以下内容:

在此处输入图像描述


问题:如何创建嵌套列表?

注意:正如@zx8754 所指出的,这个SO 帖子中已经有一个解决方案,但这需要data.frame作为输入。由于我的不一致level,我看不到将其转换为data.frame.

4

2 回答 2

4

使用data.table-style 合并:

library(data.table)
dt = data.table(idx=1:length(value), level, parent=value)

dt = dt[dt[, .(i=idx, level=level-1, child=parent)], on=.(level, idx < i), mult='last']

dt[is.na(parent), parent:= 'root'][, c('idx','level'):= NULL]

> dt
#     parent child
#  1:   root     a
#  2:      a     b
#  3:      b     c
#  4:      c     d
#  5:      c     e
#  6:      b     f
#  7:      f     g
#  8:      f     h
#  9:   root     i
# 10:      i     j
# 11:      j     k

现在我们可以使用另一篇文章中的解决方案:

x = maketreelist(as.data.frame(dt))

> identical(x, my_list)
# [1] TRUE
于 2016-12-18T06:41:27.873 回答
3

作为前言,您的数据很难处理,因为关键信息是按level. 我不知道您如何按顺序获得这些值,但请考虑可能有更好的方法来首先构建该信息,这将使下一个任务更容易。

这是一种base将数据转换为具有 2 列的数据框的 -y 方法,parent然后child将其传递给data.tree可以轻松转换为所需 JSON 格式的函数……然后将其传递给radialNetwork……

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(data.tree)
library(networkD3)

parent_idx <- sapply(1:length(level), function(n) rev(which(level[1:n] < level[n]))[1])
df <- data.frame(parent = value[parent_idx], child = value, stringsAsFactors = F)
df$parent[is.na(df$parent)] <- ""

list <- ToListExplicit(FromDataFrameNetwork(df), unname = T)
radialNetwork(list)

这是tidyverse实现相同目标的一种方法...

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(tidyverse)
library(data.tree)
library(networkD3)

data.frame(level, value, stringsAsFactors = F) %>%
  mutate(row = row_number()) %>%
  mutate(level2 = level, value2 = value) %>%
  spread(level2, value2) %>%
  mutate(`0` = "") %>%
  arrange(row) %>%
  fill(-level, -value, -row) %>%
  gather(parent_level, parent, -level, -value, -row) %>%
  filter(parent_level == level - 1) %>%
  arrange(row) %>%
  select(parent, child = value) %>%
  data.tree::FromDataFrameNetwork() %>%
  data.tree::ToListExplicit(unname = TRUE) %>%
  radialNetwork()

并且作为奖励,networkD3(v0.4.9000)的当前开发版本具有一个新treeNetwork函数,该函数采用带有列/变量的数据框nodeIdparentId这消除了将函数data.tree转换为 JSON 的需要,所以这样的东西可以工作......

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(tidyverse)
library(networkD3)

data.frame(level, value, stringsAsFactors = F) %>%
  mutate(row = row_number()) %>%
  mutate(level2 = level, value2 = value) %>%
  spread(level2, value2) %>%
  mutate(`0` = "root") %>%
  arrange(row) %>%
  fill(-level, -value, -row) %>%
  gather(parent_level, parent, -level, -value, -row) %>%
  filter(parent_level == level - 1) %>%
  arrange(row) %>%
  select(nodeId = value, parentId = parent) %>%
  rbind(data.frame(nodeId = "root", parentId = NA)) %>% 
  mutate(name = nodeId) %>% 
  treeNetwork(direction = "radial")
于 2017-12-25T22:50:55.827 回答