简体   繁体   English

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

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

As mentioned in title, how do I plot a data.tree with its relevant values?正如标题中提到的,我如何绘制一个 data.tree 及其相关值?

Thank you in advance for the help.预先感谢您的帮助。 Already at my wits end :(已经在我的智慧结束:(

Edit: More information:编辑:更多信息:

The data I am trying to visualize is a survey, which the respondent is asked the primary question, in which if h/she answered yes and there will be follow up questions to the first one.我试图可视化的数据是一项调查,受访者被问到主要问题,如果他/她回答是,那么第一个问题将有后续问题。 I am trying to visualize the percentage of respondents who answered yes or no to each question to which my idea is to use a decision tree like plot for that.我试图可视化对每个问题回答是或否的受访者的百分比,我的想法是为此使用决策树之类的绘图。

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

Edit:编辑:

Tried your solution, Gilean, works pretty good, however, how do I get the child node to recognize same words as different trees?尝试了您的解决方案 Gilean,效果很好,但是,如何让子节点将相同的单词识别为不同的树? And how do I adjust the words by font size or alignment so it won't impede visualization?以及如何通过字体大小或对齐方式调整单词,以免妨碍可视化?

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

I am not very familiar with data.tree , so I do not know a way to add a fast way to add all labels, but you can set a label for each edge separately.我对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)

In igraph you can just create a data.frame with all the information you need and create a graph with a tree-structure from this data.frame with the edge labels.igraph您只需创建一个data.frame您需要的所有信息的data.frame ,然后从带有边标签的 data.frame 中创建一个具有树结构的图形。 However, it will be necessary to adjust a few more things to make it look nice in comparison to data.tree .但是,与data.tree相比,有必要调整更多内容以使其看起来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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM