如何在R Studio中绘制具有13个节点的二叉树

如何在R Studio中绘制具有13个节点的二叉树,r,plot,R,Plot,我是R的新手,我画的图像环,星。它们有一些特殊的函数,但我不知道如何绘制一个有13个节点的二叉树? 我使用了graph.extended.chordal.ring()函数,但它没有帮助。 有没有关于R studio的好教程,以及如何绘制二叉树 library(igraph) G <- graph.extended.chordal.ring(13, matrix(c(2,4,6), nr=1)) L <- layout.fruchterman.reingold(G) 库(igrap

我是R的新手,我画的图像环,星。它们有一些特殊的函数,但我不知道如何绘制一个有13个节点的二叉树? 我使用了graph.extended.chordal.ring()函数,但它没有帮助。 有没有关于R studio的好教程,以及如何绘制二叉树

library(igraph)
G <-  graph.extended.chordal.ring(13, matrix(c(2,4,6), nr=1))
L <- layout.fruchterman.reingold(G)
库(igraph)

G您可以使用
graph.tree
函数,例如:

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)
库(igraph)

G我喜欢@digEmAll的解决方案,但这里有另一个使用我的实验包。这很好,因为它将二叉树表示为data.table对象(本质上是data.frames)

库(data.table)
图书馆(GG2)
图书馆(btree)
#制作一个深度为3->15个节点的完美btree

btree R studio只是R的一个奇特GUI。您找到的任何R教程都应该在Rstudio中工作。查看您找到的
graph.extended.chordal.ring()
包中的
igraph
您可以尝试
graph.tree()
。如果这对您不起作用,您可以使用
grid
包手动构建树,但这将是一个难题。我相信Graphviz程序可以很容易地创建一个二叉树。可能值得研究。谢谢DigeMail,我尝试了上百种不同的方法,但你的解决方案解决了我的问题:)非常感谢:)这是一个非常好的解决方案DigeMail,但如何给节点命名以及如何在特定节点添加边。非常感谢DigeMail,这对我帮助很大。如果我需要,我希望你能再次帮助我
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)
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")