如何用 R 中的值可视化 data.tree?

How do I visualize a data.tree with values in R?

如标题所述,如何绘制 data.tree 及其相关值?

提前感谢您的帮助。我已经束手无策:(

编辑:更多信息:

我试图可视化的数据是一项调查,向受访者询问主要问题,如果 h/she 回答是,将会有第一个问题的后续问题。我正在尝试可视化对每个问题回答是或否的受访者百分比,我的想法是为此使用决策树之类的图。

library(data.tree)
library(networkD3)

# create simple tree
tree <- Node$new("Primary Node")
tree1 <- tree$AddChild("Tree1")
tree2 <- tree$AddChild("Tree2")
tree3 <- tree1$AddChild("Tree3")
tree4 <- tree2$AddChild("Tree4")

# assign value

tree1$value <- 1
tree2$value <- 2
tree3$value <- 3
tree4$value <- 4

# plot tree ## No values reflected
plot(tree)
simpleNetwork(ToDataFrameNetwork(tree))

编辑:

尝试过你的解决方案,Gilean,效果很好,但是,我如何让 child 节点将相同的词识别为不同的树?以及如何通过字体大小或对齐方式调整单词,以免妨碍可视化?

library(igraph)

# requires the changing of No to No1, No2 and so forth to prevent it merging into one large node

df <- data.frame(parent = c("Have you ever had your cholesterol  fat levels in your blood  measured by a doctor or other health worker",
                            "Have you ever had your cholesterol  fat levels in your blood  measured by a doctor or other health worker",
                            "Have you ever been told by a doctor or other health worker that you have raised cholesterol",
                            "Have you ever been told by a doctor or other health worker that you have raised cholesterol",
                            "Were you first told in the past 12 months",
                            "Were you first told in the past 12 months",
                            "In the past two weeks have you taken any oral treatment medication for raised total cholesterol prescribed by a doctor or other health worker",
                            "In the past two weeks have you taken any oral treatment medication for raised total cholesterol prescribed by a doctor or other health worker",
                            "Have you ever seen a traditional healer for raised cholesterol",
                            "Have you ever seen a traditional healer for raised cholesterol",
                            "Are you currently taking any herbal or traditional remedy for your raised cholesterol",
                            "Are you currently taking any herbal or traditional remedy for your raised cholesterol"),

                          child = c("No", "Have you ever been told by a doctor or other health worker that you have raised cholesterol", 
                           "No1", "Were you first told in the past 12 months",
                           "No2", "In the past two weeks have you taken any oral treatment medication for raised total cholesterol prescribed by a doctor or other health worker",
                           "No3", "Have you ever seen a traditional healer for raised cholesterol",
                           "No4", "Are you currently taking any herbal or traditional remedy for your raised cholesterol",
                           "No5", "Yes"),

                 value = 1:12)


tree <- graph_from_data_frame(df, directed = TRUE)

plot(tree, vertex.label = V(tree)$name, edge.label = E(tree)$value, layout=layout_as_tree, vertex.size = c(10, E(tree)$value))

我不是很熟悉data.tree,所以我不知道有什么方法可以添加一个快速添加所有标签的方法,但是你可以为每条边单独设置一个标签。

library(data.tree)

# create simple tree
tree <- Node$new("Primary Node")
tree1 <- tree$AddChild("Tree1")
tree2 <- tree$AddChild("Tree2")
tree3 <- tree1$AddChild("Tree3")
tree4 <- tree2$AddChild("Tree4")

# add edges 
SetEdgeStyle(tree, label = 1)
SetEdgeStyle(tree2, label = 2)
SetEdgeStyle(tree3, label = 3)
SetEdgeStyle(tree4, label = 4)

# plot tree 
plot(tree)

igraph 中,您可以只创建一个包含您需要的所有信息的 data.frame,并根据此 data.frame 创建一个带有边标签的树结构图。但是,与 data.tree.

相比,还需要进行一些调整以使其看起来更漂亮
library(igraph)
df <- data.frame(parent = c("Primary Node", "Primary Node", "Tree1", "Tree2"),
                 child = c("Tree1", "Tree2", "Tree3", "Tree4"),
                 value = 1:4)

tree <- graph_from_data_frame(df, directed = TRUE)

plot(tree, vertex.label = V(tree)$name, edge.label = E(tree)$value, layout=layout_as_tree, vertex.size = c(20, E(tree)$value * 10))