Search code examples
rplotdecision-treenetworkd3data.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?

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


Solution

  • 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.

    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. However, it will be necessary to adjust a few more things to make it look nice in comparison to 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))