用R绘制树

时间:2015-03-04 15:02:54

标签: r plot tree

来自data.frame(或任何其他R对象类型),包含3列:"节点,父级和文本",我想要绘制一个来自&#34的行的树;节点"到"家长"和"文字"作为标签。

如果可能,任何人都可以建议使用好的库和示例代码。 我一直在看igraph库,但是所有的例子我都可以找到带有序号或字母作为节点的绘图树,并且设置树的布局并不简单。

非常感谢任何帮助

由于

修改

谢谢你们的帮助,我真的很感激。 一些额外的评论,如果你可以进一步帮助

@ md1630,我尝试了你的建议,但那不是我想要的。第一个代码绘制树的根部在顶部,箭头从根到叶,第二个修正箭头但反转树。我喜欢的是从叶子到根的顶部和箭头(我知道每个说法可能不是一棵树 - 但这是要求

enter image description here

@ user20650您的解决方案看起来正确,但随着节点数量的增加,图像开始变得拥挤。如何在它们之间添加更多空间?enter image description here

@math我是否正确使用您提供的功能?我调用了plot(layout.binary(g))并在左边得到了结果。右边的那个是图(g)的输出

enter image description here

3 个答案:

答案 0 :(得分:2)

您可以使用rgraphviz。这是从数据框df中用“Node,Parent和text”列绘制树的代码。我没有在我的电脑上运行它,所以可能有错误。但大致就是这个想法:

source("http://bioconductor.org/biocLite.R")
biocLite("Rgraphviz")
library("Rgraphviz")
#first set up the graph with just the nodes
nodes<- unique(df['Node'])
gR <- new("graphNEL", nodes = nodes, edgemode = "directed")
#add edges for each row in df
    for (j in (1:nrow(df))) {
       gR <- addEdge(df[j,2], df[j,1], gR, 1)
    }
#add text labels
nAttrs <- list()
z <- df['text']
nAttrs$label <- z
#plot
plot(gR, nodeAttrs = nAttrs) #you can specify more attributes here 

答案 1 :(得分:2)

升级评论

library(igraph)

# some example data
dat <- data.frame(parent=rep(letters[1:3], each=2), 
                  node=letters[2:7], 
                  text=paste0("lab", 1:6))
# create graph
g <- graph.data.frame(dat)

# plot
# layout.reingold.tilford gives a tree structure
# edge and vertx labels can be defined in the plot command or alternatively
# you can add them to the graph via V(g)$name and E(g($label assignments

plot(g, layout = layout.reingold.tilford, 
             edge.label=E(g)$text, vertex.label=paste0("v_lab",1:7))

编辑重新评论

如果你想让方向从叶子朝向根;您可以先从更标准的树结构中获取树布局坐标,然后反转边缘。

# get tree layout coords
g <- graph.data.frame(dat)
lay = layout.reingold.tilford(g)

# redraw graph with edges reversed
g2 <- graph.data.frame(dat[2:1], vertices = get.data.frame(g, what="vertices"))

par(mar=rep(0,4), mfrow=c(1,2))
plot(g, layout=lay)
plot(g2, layout=lay)

enter image description here

答案 2 :(得分:0)

您可以使用igraph获取包含数据的网络(假设您的数据框为dd):

g = graph(t(dd[,2:1]))
V(g)$label = as.character(dd$text)
plot(g, layout=layout.binary)

我认为您的根(没有父母)不在数据框中,否则请使用dd[-1,2:1]

如果你想拥有一棵树,你可以轻松地制作一个布局,它只是一个采用图形并返回矩阵的函数。对于二叉树:

layout.binary = function(graph) {
    layout = c()
    r_vertex = length(V(graph))
    depth = ceiling(log2(r_vertex+1))
    for (ii in 0:(depth-1)) {
        for (jj in 1:min(2^ii, r_vertex)) {
            layout = rbind(layout, c(ii, (2*(jj-1)+1)/(2^(ii+1))))
        }
        r_vertex = r_vertex - 2^ii
    }
    return(layout)
}

它将绘制一个水平树,如果您希望它是垂直的,请使用c((2*(jj-1)+1)/(2^(ii+1)), ii)