R igraph 手册页

如果您从 R 中使用 igraph,请使用此选项

shapes {igraph}R 文档

绘制 igraph 图形时的各种顶点形状

描述

从 0.5.1 版本开始,igraph 支持绘制图形时的不同顶点形状。

用法

shapes(shape = NULL)

shape_noclip(coords, el, params, end = c("both", "from", "to"))

shape_noplot(coords, v = NULL, params)

add_shape(shape, clip = shape_noclip, plot = shape_noplot, parameters = list())

参数

shape

字符标量,顶点形状的名称。如果对于 shapesNULL,则返回所有已定义的顶点形状的名称。

coords, el, params, end, v

请参阅下面剪切/绘图函数的参数。

clip

一个 R 函数对象,剪切函数。

plot

一个 R 函数对象,绘图函数。

parameters

命名列表,附加的绘图/顶点/边参数。名为 define 的元素定义新参数,元素本身定义它们的默认值。顶点参数应具有前缀 ‘vertex.’,边参数应具有前缀 ‘edge.’。其他常规绘图参数应具有前缀 ‘plot.’。请参阅下面的详细信息。

详细信息

在 igraph 中,顶点形状由两个函数定义:1) 提供关于形状大小的信息以用于剪切边,以及 2) 如果请求,则绘制形状。在本文档页面的其余部分中,这些函数称为 “形状函数”。第一个是剪切函数,第二个是绘图函数。

剪切函数具有以下参数

coords

一个四列矩阵,包含 el 参数中提供的边列表的顶点的坐标。

el

一个两列矩阵,一些端点将被剪切的边。它应该具有与 coords 相同的行数。

params

这是一个函数对象,可以调用它来查询顶点/边/绘图图形参数。该函数的第一个参数是 “vertex”、“edge” 或 “plot”,以决定参数的类型,第二个参数是给出参数名称的字符串。例如:

params("vertex", "size")
    
end

字符串,它给出了将使用哪些端点。可能的值为 “both”、“from” 和 “to”。如果为 “from”,则函数应剪切 el 边列表中的第一列,“to” 选择第二列,“both” 选择两列。

剪切函数应返回一个矩阵,其行数与 el 参数相同。如果 endboth,则矩阵必须具有四列,否则为两列。该矩阵包含修改后的坐标,应用了剪切。

绘图函数具有以下参数

coords

顶点的坐标,一个两列矩阵。

v

要绘制的顶点的 ID。它应该与 coords 参数中的行数匹配。

params

与剪切函数相同,请参阅上面。

不使用绘图函数的返回值。

shapes 可用于列出所有已安装的顶点形状的名称,方法是在没有参数的情况下调用它,或将 shape 参数设置为 NULL。如果给出了形状名称,则该形状的剪切和绘图函数将在一个命名列表中返回。

add_shape 可用于将新的顶点形状添加到 igraph。为此,必须给出新形状的剪切和绘图函数。也可以在 parameters 参数中列出绘图/顶点/边参数,剪切和/或绘图函数可以使用这些参数。一个例子是一个通用的正多边形形状,它可以具有边数的参数。

shape_noclip 是一个非常简单的剪切函数,用户可以在自己的形状定义中使用它。它不进行剪切,边将精确绘制到列出的顶点位置坐标。

shape_noplot 是一个非常简单(并且可能不是很有用)的绘图函数,它不绘制任何内容。

如果 shape 参数为 NULL,则 shapes 返回一个字符向量。它返回一个命名列表,其中包含名为 “clip” 和 “plot” 的条目,它们都是 R 函数。

add_shape 返回 TRUE,不可见。

shape_noclip 返回其 coords 参数的相应列。

示例

# all vertex shapes, minus "raster", that might not be available
shapes <- setdiff(shapes(), "")
g <- make_ring(length(shapes))
set.seed(42)
plot(g, vertex.shape=shapes, vertex.label=shapes, vertex.label.dist=1,
     vertex.size=15, vertex.size2=15,
     vertex.pie=lapply(shapes, function(x) if (x=="pie") 2:6 else 0),
     vertex.pie.color=list(heat.colors(5)))

# add new vertex shape, plot nothing with no clipping
add_shape("nil")
plot(g, vertex.shape="nil")

#################################################################
# triangle vertex shape
mytriangle <- 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/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }

  symbols(x=coords[,1], y=coords[,2], bg=vertex.color,
          stars=cbind(vertex.size, vertex.size, vertex.size),
          add=TRUE, inches=FALSE)
}
# clips as a circle
add_shape("triangle", clip=shapes("circle")$clip,
                 plot=mytriangle)
plot(g, vertex.shape="triangle", vertex.color=rainbow(vcount(g)),
     vertex.size=seq(10,20,length.out=vcount(g)))

#################################################################
# generic star vertex shape, with a parameter for number of rays
mystar <- 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/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }
  norays <- params("vertex", "norays")
  if (length(norays) != 1 && !is.null(v)) {
    norays <- norays[v]
  }

  mapply(coords[,1], coords[,2], vertex.color, vertex.size, norays,
         FUN=function(x, y, bg, size, nor) {
           symbols(x=x, y=y, bg=bg,
                   stars=matrix(c(size,size/2), nrow=1, ncol=nor*2),
                   add=TRUE, inches=FALSE)
         })
}
# no clipping, edges will be below the vertices anyway
add_shape("star", clip=shape_noclip,
                 plot=mystar, parameters=list(vertex.norays=5))
plot(g, vertex.shape="star", vertex.color=rainbow(vcount(g)),
     vertex.size=seq(10,20,length.out=vcount(g)))
plot(g, vertex.shape="star", vertex.color=rainbow(vcount(g)),
     vertex.size=seq(10,20,length.out=vcount(g)),
     vertex.norays=rep(4:8, length.out=vcount(g)))

[包 igraph 版本 1.3.5 索引]