1

如标题中所述,如何绘制具有相关值的 data.tree?

预先感谢您的帮助。已经不知所措了:(

编辑:更多信息:

我试图可视化的数据是一项调查,受访者被问到主要问题,如果他/她回答是,那么第一个问题就会有后续问题。我正在尝试可视化对每个问题回答是或否的受访者百分比,我的想法是使用类似图的决策树。

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 尝试了您的解决方案,效果很好,但是,如何让子节点将相同的单词识别为不同的树?以及如何通过字体大小或对齐方式调整单词,使其不会妨碍可视化?

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))

4

1 回答 1

0

我不是很熟悉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))
于 2020-03-06T09:18:19.210 回答