更改顶点边框的线型

时间:2018-02-10 21:01:11

标签: r igraph

有没有办法用R改变igraph图中顶点边界的线型?例如,我想用虚线而不是实线制作顶点。

更新:以下是生成带黑色边框的节点的示例代码。我想知道是否有办法让黑色边框成为黑色虚线边框(如标准R线图中的' lty = 2')

library('igraph')
NodeList = data.frame('AA', x = 1 ,y = 1)
EdgeList = data.frame(from = 'AA', to = 'AA')
xgraph = graph_from_data_frame(vertices = NodeList, 
                               d = EdgeList, directed = TRUE)
plot(xgraph, vertex.shape = 'circle',
     vertex.size = 100, rescale = FALSE)

1 个答案:

答案 0 :(得分:4)

哦,哎呀,无论如何我都会发布它。毕竟它是代码。应归功于Gabor Csardi和@ user20650。我认为引文位于一个足够稳定的位置,我们可以期望它在未来几年可以访问。 :

png(width=200,height=200)
mycircle <- function(coords, v=NULL, params) {
  vertex.color <- params("vertex", "color")
  if (length(vertex.color) != 1 && !is.null(v)) {
    vertex.color <- vertex.color[v]
  }
  vertex.frame.lty  <- params("vertex", "lty")
    if (length(vertex.frame.lty) != 1 && !is.null(v)) {
      vertex.frame.lty <- vertex.frame.lty[v]
  }
  vertex.size  <- 1/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }
  vertex.frame.color <- params("vertex", "frame.color")
  if (length(vertex.frame.color) != 1 && !is.null(v)) {
    vertex.frame.color <- vertex.frame.color[v]
  }
  vertex.frame.width <- params("vertex", "frame.width")
  if (length(vertex.frame.width) != 1 && !is.null(v)) {
    vertex.frame.width <- vertex.frame.width[v]
  }
  mapply(coords[,1], coords[,2], vertex.color, vertex.frame.color,
         vertex.size, vertex.frame.width, vertex.frame.lty,
         FUN=function(x, y, bg, fg, size, lwd, lty) {
           symbols(x=x, y=y, bg=bg, fg=fg, lwd=lwd, lty=lty,
                   circles=size, add=TRUE, inches=FALSE)
         })
}
dev.off()

add.vertex.shape("fcircle", clip=igraph.shape.noclip,
         plot=mycircle, parameters=list(vertex.frame.color=1, vertex.frame.lty=1,
                                  vertex.frame.width=1))

plot(xgraph, vertex.shape="fcircle", vertex.frame.color="red", vertex.size=100 ,vertex.lty=2,
     vertex.frame.width=2)

有警告说,如果igraph API发生变化,可能无法保证这一点:

https://github.com/damianavila/RISE