R igraph,如何用形状和光栅的混合绘制顶点?

时间:2016-10-19 02:07:14

标签: r 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)))

1 个答案:

答案 0 :(得分:1)

这似乎是一种方式,但它需要一些手动调整以适应栅格。

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)))

给予

enter image description here

我敢说对此有更多的igraph方法