Home > Net >  How can I plot a collapsible tree with various levels in R?
How can I plot a collapsible tree with various levels in R?

Time:03-31

I have been trying to plot a collapsible tree with "collapsibletree" package but I don't get what I need. Also, this plot should be uploaded to power BI using the R script visual feature.

Imagine that I have a dataframe like this:

parent <- c("", "Carlos", "Carlos", "María", "María", "Paula", "Alex")
child <- c("Carlos", "María", "Alex", "Javier", "Paula", "Pablo", "Pepe")

df <- data.frame(parent, child)

And then, I want to plot a collapsible tree:

collapsibleTree(df = df,
                hierarchy = c("parent", "child"),
                root = "Carlos")

But I get this result:

enter image description here

And the result I want is this: enter image description here

Does anyone knows how to get that solution?

Thanks in advance and have a great day!!

CodePudding user response:

You should have one value as NA in your data, because you need to have a beginning point. That's why I changed your "" in the parent vector to NA. After that you can use the following code:

parent <- c(NA, "Carlos", "Carlos", "María", "María", "Paula", "Alex")
child <- c("Carlos", "María", "Alex", "Javier", "Paula", "Pablo", "Pepe")

df <- data.frame(parent, child)

library(collapsibleTree)
collapsibleTreeNetwork(df, collapsed = FALSE)

Output:

enter image description here

  • Related