【问题标题】:R igraph, how to plot vertices with mix of shapes and raster?R igraph,如何绘制混合形状和栅格的顶点?
【发布时间】:2016-10-19 02:07:14
【问题描述】:

我正在尝试使用 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)))

【问题讨论】:

    标签: r igraph


    【解决方案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)))
    

    给予

    我敢说有更多的 igraph 方法来解决这个问题

    【讨论】:

    • 这很好用。有没有办法调整函数,使非方形图像在调整大小时保持其纵横比?
    • @skellpco;这与vertex.size2 参数的设置有关:使其小于或大于vertex.size 以拉伸形状(在您想要的方向上。参见?igraph.plotting
    猜你喜欢
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2012-10-19
    • 2018-04-14
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多