3

I'm trying to reproduce the probability tree diagram as shown below from wikipedia using igraph and ggnet2. The following is my start,

library(igraph)
g <- graph.tree(2^4-1, 2)

library(GGally)
ggnet2(g, label = TRUE, label.size = 3)

which randomly places the nodes, labels them numerically, and has no labels on the edges:

enter image description here

Instead, I need to reorganize and label the edges and nodes, like so, except place the node labels inside the circles:

enter image description here

ssp3nc3r
  • 3,662
  • 2
  • 13
  • 23
  • 1
    maybe start with `g <- graph.tree(7, 2) plot(g, layout = layout.reingold.tilford(g, root=1))` – DJack Jan 21 '18 at 09:48

1 Answers1

8

Here was my solution:

library(igraph)

g <- graph.tree(n = 2 ^ 3 - 1, children = 2)
node_labels <- c("", "P(A)", "P(A')", "P(AB)", "P(AB')", "P(A'B)", "P(A'B')")
edge_labels <- c("P(A)", "P(A')", "P(B|A)", "P(B'|A)", "P(B|A')", "P(B'|A')")

layout <- layout.reingold.tilford(g)
layout <- -layout[,2:1]                    # rotate layout using negative and 
                                        # reverse columns of default
plot(g,
     layout = layout,                   # draw graph as tree
     vertex.size = 25,                  # node size
     vertex.color = '#C4D8E2',          # node color
     vertex.label = node_labels,        # node labels
     vertex.label.cex = .5,             # node label size
     vertex.label.family = "Helvetica", # node label family
     vertex.label.font = 2,             # node label type (bold)
     vertex.label.color = '#000000',    # node label color
     edge.label = edge_labels,          # edge labels
     edge.label.cex = .7,               # edge label size
     edge.label.family = "Helvetica",   # edge label family
     edge.label.font = 2,               # edge label font type (bold)
     edge.label.color = '#000000',      # edge label color
     edge.arrow.size = .5,              # arrow size
     edge.arrow.width = .5              # arrow width
) 

Which gives me this,

Horizontal Probability tree

dzeltzer
  • 990
  • 8
  • 28
ssp3nc3r
  • 3,662
  • 2
  • 13
  • 23