This is a follow-up question from : Creating treechart from tabbed text in R
I am using following function:
treechart = function(){
library(psych)
fiel
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)
}
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))
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