R igraph程序包中树形图条目的顺序不正确 [英] Order of treechart entries not correct in R igraph package

查看:92
本文介绍了R igraph程序包中树形图条目的顺序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自以下问题的后续问题:

This is a follow-up question from : Creating treechart from tabbed text in R

我正在使用以下功能:

treechart = function(){
library(psych)
fields <- max(count.fields(textConnection(readClipboard()), sep = "\t"))
dat = read.table(text = readClipboard(), sep="\t",col.names = paste0("V", sequence(fields)), header=FALSE, fill=TRUE, strip.white=TRUE, stringsAsFactors=FALSE, na.strings="")

library(zoo)
library(igraph)
# To prepare the data
# carry forward the last value in columns if lower level (col to the right)
# is non-missing
dat[1] <- na.locf(dat[1], na.rm=FALSE)
for(i in ncol(dat):2)  {
  dat[[i-1]] <-  ifelse(!is.na(dat[[i]]), na.locf(dat[[i-1]], na.rm=F), dat[[i-1]])
}            

# get edges for graph
edges <- rbind(na.omit(dat[1:2]),
            do.call('rbind',
                    lapply(1:(ncol(dat)-2), function(i) 
                    na.omit(setNames(dat[(1+i):(2+i)],
                    names(dat[1:2])))))
                       )

# create graph
g <- graph.data.frame(edges)
# Plot graph
E(g)$curved <- 0
plot.igraph(g, vertex.size=0, edge.arrow.size=0 , layout=-layout.reingold.tilford(g)[,2:1])
}

我正在使用以下示例数据(由文本编辑器中的标签或电子表格中的标签分隔),这些示例数据是通过control-C选择并复制的:

I am using following example data (separated by tabs in text editor or from spreadsheet) which I select and copy with control-C:

AAA 
    BBB
    CCC
    DDD
        III
        JJJ
            LLL
    EEE
        KKK
    FFF
    GGG

然后在运行命令"treechart()"时,我得到以下图表:

Then on running the command 'treechart()' I get following chart:

此处DDD和EEE高于BBB,CCC.同样,JJJ排在III之前.如何才能使函数treechart()始终保持正确的顺序?感谢您的帮助.

Here DDD and EEE are coming higher than BBB, CCC. Similarly, JJJ is coming before III. How can I correct the function treechart() for this order to be always correct? Thanks for your help.

推荐答案

并不是布局不正确,而是您要求的是layout.reingold.tilford布局,这就是您所得到的.如您所见,它喜欢将更复杂的分支移到一侧.它不考虑顶点的指定顺序.我尝试编写一个新的布局功能来保留订单

It's not that the layout is incorrect, it's just that you asked for the layout.reingold.tilford layout and that's what you got. As you can see, it likes to move more complex branches to one side. It does not consider the order that the vertices were specified. I tried writing a new layout function that would preserve order

layout.tree.order <- function(g, vseq=V(g)$name, root=vseq[1]) {
    leaves <- vseq[sapply(V(g)[vseq], function(x) 
        length(unique(neighbors(g, x, mode="out"))))==0]
    ypos <- rep(NA, vcount(g))
    ypos[match(leaves, V(g)$name)]<-rev(seq(0,1,length.out=length(leaves)))

    calcypos<-function(g, vx) {
        if (!is.na(ypos[vx])) {
            p <- ypos[vx]
        } else {
            nb <- unique(neighbors(g, V(g)[vx]))
            p <- mean(sapply(nb, function(x) calcypos(g,x)))
        }
        ypos[vx] <<- p
        return(invisible(p))
    }
    calcypos(g, which(V(g)$name == root))
    xpos <- c(shortest.paths(g, V(g)[which(vseq == root)], V(g), mode="out"))

    cbind(xpos, ypos)
}

然后,您只想在treemap函数中更改绘图线以添加另一条线并更改布局

Then you just want to change the plot line in your treemap function to add one additional line and change the layout

vseq <- apply(dat, 1, function(x) na.omit(rev(x))[1])
plot.igraph(g, vertex.size=0, edge.arrow.size=0, 
    layout=layout.tree.order(g, vseq))

因此,这里的vseq是指定自顶向下顺序的内容.在这里,我们按照它们在dat数据框中出现的顺序使用这些值.这将产生以下情节

So the vseq here is what specifies the top-down ordering. Here we use the values in the order that they appear in your dat data frame. This will produce the following plot

这篇关于R igraph程序包中树形图条目的顺序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆