箭头躲在顶点后面

时间:2018-02-10 21:07:59

标签: r igraph

示例:

library(igraph)
library(plotrix)

##############
## Generate an `ellipse` node type as per
##  https://stackoverflow.com/a/48469289/2232265
myellipse <- function(coords, v=NULL, params) {
    vertex.color <- params("vertex", "color")
    if (length(vertex.color) != 1 && !is.null(v)) {
        vertex.color <- vertex.color[v]
    }
    vertex.size <- 1/30 * params("vertex", "size")
    if (length(vertex.size) != 1 && !is.null(v)) {
        vertex.size <- vertex.size[v]
    }
    draw.ellipse(x=coords[,1], y=coords[,2],
                 a = vertex.size, b=vertex.size/2, col=vertex.color)
}

add_shape("ellipse", clip=shapes("circle")$clip,
          plot=myellipse)

## Set up graph
nodes = c('AA','BB','CC')
x = c(1,3,8)
y = c(1,10,1)
from = c('AA', 'AA', 'AA', 'BB')
to = c('CC','CC', 'BB', 'CC')
NodeList = data.frame(nodes, x ,y)
EdgeList = data.frame(from, to, arrow.mode = rep(2, length(from)))
xgraph = graph_from_data_frame(vertices = NodeList, 
                               d = EdgeList, directed = TRUE)
V(xgraph)$color = c('gray', "lightblue", 'gray')
plot(xgraph, vertex.shape = 'ellipse', 
     vertex.size = 100,
     xlim = c(0, 10),
     ylim = c(0, 10),
     edge.arrow.mode=c(0, 0, 2, 2),
     edge.arrow.size = 2*c(1,1,1,1),
     rescale = FALSE)

enter image description here

我该如何解决这个问题?理想情况下,我希望边缘稍微缩短一点,这样箭头就不会与节点重叠。

1 个答案:

答案 0 :(得分:0)

一个简单的选项是缩小顶点大小,使用vertex.size = 30而不是vertex.size = 100绘图。

另一种选择是更改函数vertex.size的{​​{1}}输出:使用myellipse代替vertex.size <- 1/60 * params("vertex", "size")然后绘图,这也会减少顶点。