【发布时间】:2020-06-18 21:24:21
【问题描述】:
如标题中所述,如何绘制具有相关值的 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))
【问题讨论】:
-
是
data.tree是必需的还是像igraph这样的其他软件包也可以? -
请。我使用 data.tree 只是因为它是谷歌弹出的第一个选项。
-
如果您在控制台中输入
?igraph.plotting,您可以看到您可以在绘图中调整的所有参数。对于字体大小,您要使用vertex.label.cex,对于对齐,请使用vertex.label.dist和vertex.label.angle的组合。对于你的其他问题。现在,第二个示例中的节点标签和节点名称相同。如果您创建具有唯一节点名称的 data.frame,则可以在之后使用vertex.label分配非唯一节点标签。 -
将来最好将进一步的问题作为一个单独的问题提出,而不是编辑您的原始问题,这样更多的用户可以帮助您,而其他用户可以在他们寻找特定问题时找到您的问题.
标签: r plot decision-tree networkd3 data.tree