根据此问题Automatically curving an arc when it is overlapping with another one,我尝试在pos
图中设置RGraphviz
属性。
有人可以展示如何正确使用pos
属性或建议更强大的解决方法。感谢。
实施例
为了与上述链接问题保持一致,以下示例从包含网格布局中节点的bnlearn
图开始。
library(bnlearn)
library(Rgraphviz)
library(igraph)
# Create graph
adj <- matrix(0L, ncol = 9, nrow = 9, dimnames = list(LETTERS[1:9], LETTERS[1:9]))
adj[upper.tri(adj)] <- 1
e <- empty.graph(LETTERS[1:9])
amat(e) <- adj
g <- as.graphNEL(e)
# layout in grid
ig <- igraph.from.graphNEL(g)
lay <- layout.grid(ig)
lay <- setNames(data.frame(norm_coords(lay, -100, 100, -100, 100)), c("x", "y"))
帮助页面?GraphvizAttributes
表示pos
应设置为
节点的位置(对于neato布局,这是节点的初始位置)。使用概念val指定val,其中每个val都是double。
再向下,仅neato
属性
pin:如果TRUE且节点在输入上有pos属性,则neato会阻止节点从输入位置移动。此属性的默认值为FALSE。
我无法找到应用此参数的正确方法。
我试过的各种事情没有成功
# Passed named list with `x` and `y` positions
# To see the `pos` attribute has not been added you can use `AgNode(z)[[1]]`
rownames(lay) <- nodes(e)
pos <- lapply(split(lay, rownames(lay)), unlist)
z <- agopen(g, "gg", nodeAttrs=list(pos=pos,
pin=setNames(rep(TRUE, length(nodes(e))), nodes(e))),
layoutType="neato")
# Passed as a named character string with coordinates pasted together
lay1 <- do.call(paste, c(lay, sep=","))
pos <- paste(lay1, '!') # also tried with `pos = paste(lay)`
names(pos) <- nodes(e)
z <- agopen(g, "gg", nodeAttrs=list(pos=pos,
pin=setNames(rep(TRUE, length(nodes(e))), nodes(e))),
layoutType="neato")
我可以通过此解决方法获得预期的结果,但它不会很强大。
# write out dot file
z <- agopen(g, "gg")
agwrite(z, "so.dot")
#positions
lay1 <- do.call(paste, c(lay, sep=","))
pos <- paste('pos = "', lay1, '!"')
#read in dot
rd <- readLines("so.dot")
id <- sapply(paste0("label=", nodes(e)) , grep, rd)
#add in positions
for(i in seq(id)) {
rd[id[i]] <- gsub(names(id)[i], paste(names(id)[i], pos[i], sep="\n"), rd[id[i]])
}
# output and render
cat(rd, file="so1.dot", sep="\n")
system("neato so1.dot -n -Tpdf -o so.pdf")
给出预期结果