R igraph,如何绘制混合形状和光栅的顶点?
R igraph, how to plot vertices with mix of shapes and raster?
我正在尝试使用 R 和 igraph 绘制图形,混合使用形状和光栅图像作为顶点。我修改了下面的 igraph 示例以重现我的问题。有人能看出哪里出了问题吗?您需要一个 png 文件来测试脚本。
library(png)
library(igraph)
img.1 <- readPNG(system.file("img", "Rlogo.png", package="png"))
shapes <- setdiff(shapes(), "")
g <- make_ring(length(shapes))
V(g)$shape <- shapes
#change the rectangle variants to raster
V(g)$shape[grepl("rect",V(g)$shape)] <- "raster"
#give every vertex the same image, regardless of shape
V(g)$raster <- replicate(vcount(g), img.1, simplify=FALSE)
plot(g,
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)))
这似乎是一种方法,但需要进行一些手动调整以适应栅格。
library(png)
library(igraph)
# Your code
img.1 <- readPNG(system.file("img", "Rlogo.png", package="png"))
shapes <- setdiff(shapes(), "")
g <- make_ring(length(shapes))
V(g)$shape <- shapes
# Change some shapes to user defined
V(g)$shape[grepl("rect",V(g)$shape)] <- "myimg"
# Using idea from http://igraph.org/r/doc/shapes.html
# define function for image
# manually tweaked the x any y to increase size of image
myimg <- function(coords, v=NULL, params) {
vertex.size <- 1/200 * params("vertex", "size")
if (length(vertex.size) != 1 && !is.null(v)) {
vertex.size <- vertex.size[v]
}
rasterImage(img.1,
coords[,1]-vertex.size, coords[,2]-vertex.size,
coords[,1]+vertex.size, coords[,2]+vertex.size)
}
# add shape
add_shape("myimg", plot=myimg)
# plot
plot(g, vertex.size=seq(5, 5*length(shapes), 5), vertex.size2=seq(5, 5*length(shapes), 5)
vertex.pie=lapply(shapes, function(x) if (x=="pie") 2:6 else 0),
vertex.pie.color=list(heat.colors(5)))
给予
我敢说对此有更多的 igraph 方法
我正在尝试使用 R 和 igraph 绘制图形,混合使用形状和光栅图像作为顶点。我修改了下面的 igraph 示例以重现我的问题。有人能看出哪里出了问题吗?您需要一个 png 文件来测试脚本。
library(png)
library(igraph)
img.1 <- readPNG(system.file("img", "Rlogo.png", package="png"))
shapes <- setdiff(shapes(), "")
g <- make_ring(length(shapes))
V(g)$shape <- shapes
#change the rectangle variants to raster
V(g)$shape[grepl("rect",V(g)$shape)] <- "raster"
#give every vertex the same image, regardless of shape
V(g)$raster <- replicate(vcount(g), img.1, simplify=FALSE)
plot(g,
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)))
这似乎是一种方法,但需要进行一些手动调整以适应栅格。
library(png)
library(igraph)
# Your code
img.1 <- readPNG(system.file("img", "Rlogo.png", package="png"))
shapes <- setdiff(shapes(), "")
g <- make_ring(length(shapes))
V(g)$shape <- shapes
# Change some shapes to user defined
V(g)$shape[grepl("rect",V(g)$shape)] <- "myimg"
# Using idea from http://igraph.org/r/doc/shapes.html
# define function for image
# manually tweaked the x any y to increase size of image
myimg <- function(coords, v=NULL, params) {
vertex.size <- 1/200 * params("vertex", "size")
if (length(vertex.size) != 1 && !is.null(v)) {
vertex.size <- vertex.size[v]
}
rasterImage(img.1,
coords[,1]-vertex.size, coords[,2]-vertex.size,
coords[,1]+vertex.size, coords[,2]+vertex.size)
}
# add shape
add_shape("myimg", plot=myimg)
# plot
plot(g, vertex.size=seq(5, 5*length(shapes), 5), vertex.size2=seq(5, 5*length(shapes), 5)
vertex.pie=lapply(shapes, function(x) if (x=="pie") 2:6 else 0),
vertex.pie.color=list(heat.colors(5)))
给予
我敢说对此有更多的 igraph 方法