如何用 igraph 绘制椭圆节点?
How to plot a ellipse node with igraph?
SO里好像没问过这样的问题。但是,igraph
关于自定义节点形状的帮助页面相当模糊。有人可以在 igraph
中提供自定义节点形状的完整示例吗?
你不说你用的是什么语言,所以我准备用R回复。
内置的形状可以用shapes()
列出。不幸的是,椭圆不在其中。帮助页面 ?shapes
提供了几个示例,说明如何添加其他节点形状 - 三角形、星形和添加图像。下面的响应是对添加三角形的示例代码的直接修改。有几个绘制椭圆的函数。我使用了 plotrix
包中的那个。
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)
瞧瞧。
SO里好像没问过这样的问题。但是,igraph
关于自定义节点形状的帮助页面相当模糊。有人可以在 igraph
中提供自定义节点形状的完整示例吗?
你不说你用的是什么语言,所以我准备用R回复。
内置的形状可以用shapes()
列出。不幸的是,椭圆不在其中。帮助页面 ?shapes
提供了几个示例,说明如何添加其他节点形状 - 三角形、星形和添加图像。下面的响应是对添加三角形的示例代码的直接修改。有几个绘制椭圆的函数。我使用了 plotrix
包中的那个。
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)
瞧瞧。