Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a massive tree diagram in RStudio?

Tags:

r

tree

diagram

I am new to R.

I want to create a massive tree diagram that represent a Lotto game in R that looks like Tree Diagram 1 in this picture(I made it via PowerPoint):

enter image description here

The problem is I need to draw 6 balls out of 45 balls. The totally elements in this case will be 127. I tried to create a tree diagram using PowerPoint and it looks like Tree Diagram 2.

Then I gave up. I can't type "match' and "no match" and calculate the probability manually for such a massive diagram.

How can I create a tree diagram that looks like Tree Diagram 2 that has similar labels in Tree Diagram 1?

like image 929
Seng Cheong Song Avatar asked Oct 30 '25 18:10

Seng Cheong Song


1 Answers

The DiagrammeR package should be helpful:

library(DiagrammeR)

nodes <- create_nodes(nodes = 1:7, type = "number")

edges <- create_edges(from = c(1, 1, 2, 2, 3, 3),
                        to = c(2, 3, 4, 5, 6, 7),
                       rel = "leading to")

graph <- create_graph(nodes_df = nodes, 
                      edges_df = edges, 
                      graph_attrs = "layout = dot", 
                      node_attrs = "fontname = Helvetica", 
                      edge_attrs = "color = gray20") 

# View the graph
render_graph(graph)

Diagram

You can get "fancy" with the programming and labels accordingly:

nodes <- create_nodes(nodes = 1:7, type = "number",
                      label = c("Lotto", rep(c("match", "no match"), times = 3)))

### Same Code as Above...

Diagram 2

like image 91
JasonAizkalns Avatar answered Nov 02 '25 07:11

JasonAizkalns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!