Home > database >  Label Position When Drawing Using data,tree Trees
Label Position When Drawing Using data,tree Trees

Time:09-10

My current code plots a decision tree with labels displayed on the right side of the arrows. However, I want the True label to go on the left side. How does one achieve this?

library(data.tree)

tree <- Node$new("Are you happy?")
  leaf1 <- tree$AddChild("Party")
  leaf2 <- tree$AddChild("Take a nap")
SetEdgeStyle(leaf1, color = "black", label = "True", penwidth = 1, fontcolor = "black")
SetEdgeStyle(leaf2, color = "black", label = "False", penwidth = 1, fontcolor = "black")
plot(tree)

CodePudding user response:

I am not sure if it is possible using data.tree package. An alternative is using the DiagrammeR package with grViz function, which has a lot of option. Here is a reproducible example where the key is to add whitespace in the xlabel to determine set the position of the labels like this:

library(DiagrammeR)
grViz('
digraph {
forcelabels=true;
node [];
    {
        "Are you happy?"  -> Party [xlabel = "True    "];
        "Are you happy?" -> "Take a nap" [xlabel = "       False     "];
    }
}')

Created on 2022-09-09 with reprex v2.0.2

  • Related