How to plot a ellipse node with igraph?

人走茶凉 提交于 2019-12-11 01:49:48

问题


It seems no such questions has ever been asked in SO. However, the help page of igraph on customizing node shapes is rather vague. Can someone provide a complete example of customizing node shape in igraph?


回答1:


You do not say what language you use, so I am going to respond in R.

The shapes that are built-in can be listed with shapes(). Unfortunately, ellipse is not among them. The help page ?shapes gives a few examples of how to add additional node shapes - a triangle, a star and adding an image. The response below is a straightforward modification of the example code for adding a triangle. There are several functions for drawing ellipses. I used the one from the plotrix package.

library(igraph)
library(plotrix)

## Need a graph as an example
set.seed(1)
N10 = erdos.renyi.game(10, 0.31)

## Function for plotting an elliptical node
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)
}

## Register the shape with igraph
add_shape("ellipse", clip=shapes("circle")$clip,
                 plot=myellipse)

## Plot it, with different sizes and colors to illustrate
plot(N10, vertex.shape="ellipse", vertex.color=rainbow(vcount(N10)),
     vertex.size=(2:11)/2)

Et voila.



来源:https://stackoverflow.com/questions/48457824/how-to-plot-a-ellipse-node-with-igraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!