Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i plot binary tree with 13 nodes in R Studio

Tags:

plot

r

I am new in R , i plot graph like ring, star. There are special functions for them, but i dont have any idea how can i plot a binary tree with 13 nodes? I used graph.extended.chordal.ring() function but it didnt help. Is there any good tutorial for R studio and how can i plot a binary tree?

library(igraph)
G <-  graph.extended.chordal.ring(13, matrix(c(2,4,6), nr=1))
L <- layout.fruchterman.reingold(G)
like image 794
Jo_bast Avatar asked Jan 11 '23 15:01

Jo_bast


2 Answers

You can use graph.tree function, e.g. :

library(igraph)
G <- graph.tree(n=13,children=2)

# let's print it using a tree-specific layout 
# (N.B. you must specify the root node)
co <- layout.reingold.tilford(G, params=list(root=1)) 
plot(G, layout=co)

enter image description here


EDIT (as per comment) :

library(igraph)
G <- graph.tree(n=13,children=2)

#add names to vertex (just assign a upper-case letter to each)
V(G)$name <- LETTERS[1:length(V(G))]

# plot (1)
lay <- layout.reingold.tilford(G, params=list(root='A')) 
plot(G, layout=lay, vertex.size=25)

# add a vertex 'O', then a new edge 'G' --> 'O'
G <- G + vertices('O')
G <- G + edge('G', 'O')

# plot again (2)
lay <- layout.reingold.tilford(G, params=list(root='A')) 
plot(G, layout=lay, vertex.size=25)

enter image description here

like image 52
digEmAll Avatar answered Jan 18 '23 17:01

digEmAll


I like @digEmAll's solution, but here is another using my experimental package, btree. It's nice because it represents binary trees as data.table objects (which are essentially data.frames).

library(data.table)
library(ggplot2)
library(btree)

# Make a perfect btree with depth 3 -> 15 total nodes
btree <- make_perfect_btree(depth=3)
btree
    NodeId ParentNodeId LeftChildNodeId RightChildNodeId NodePath Depth
 1:      1           NA               2                3              0
 2:      2            1               4                5        L     1
 3:      3            1               6                7        R     1
 4:      4            2               8                9       LL     2
 5:      5            2              10               11       LR     2
 6:      6            3              12               13       RL     2
 7:      7            3              14               15       RR     2
 8:      8            4              NA               NA      LLL     3
 9:      9            4              NA               NA      LLR     3
10:     10            5              NA               NA      LRL     3
11:     11            5              NA               NA      LRR     3
12:     12            6              NA               NA      RLL     3
13:     13            6              NA               NA      RLR     3
14:     14            7              NA               NA      RRL     3
15:     15            7              NA               NA      RRR     3

# Remove nodes 14 & 15 to get a tree with 13 nodes. (We're simply removing rows from a data.frame)
btree <- btree[!NodeId %in% c(14, 15)]

# Plot the tree
plot_btree(btree, labelCol = "NodeId")

enter image description here

like image 27
Ben Avatar answered Jan 18 '23 17:01

Ben