我在R和igraph中遇到自定义顶点形状的问题。我可以使用下面的示例来定义带参数的自定义形状函数,但是当我尝试添加具有不同参数的多个形状时,它们都会从最后添加的形状中获取参数。
例如,下面的代码来自igraph手册,附加两行。它将绘制7射线星,即使我告诉情节使用“星”,它有vertex.norays = 3
有人看到问题吗?
library(igraph)
# all vertex shapes, minus "raster", that might not be available
shapes <- setdiff(shapes(), "")
g <- make_ring(length(shapes))
set.seed(42)
#################################################################
# 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=3))
# I ADDED THIS ################################################
add_shape("star7", clip=shape_noclip,
plot=mystar, parameters=list(vertex.norays=7))
plot(g, vertex.shape="star", vertex.color=rainbow(vcount(g)),
vertex.size=seq(10,20,length=vcount(g)))