Rigograph,如何绘制形状和栅格混合的顶点? [英] R igraph, how to plot vertices with mix of shapes and raster?

查看:73
本文介绍了Rigograph,如何绘制形状和栅格混合的顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用形状和栅格图像的混合来绘制带有R和igraph的图形.我修改了下面的igraph示例以重现我的问题.有人看到错了吗?您需要一个png文件来测试脚本.

I'm trying to plot a graph with R and igraph, using a mix of shapes and raster images for the vertices. I've modified the igraph example below to reproduce my problem. Can someone see what is wrong? You'll need a png file to test the script.

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

推荐答案

这似乎是一种方法,但是需要一些手动调整才能适合栅格.

This seems to be one way, but it needs a bit of manual tweaking to fit the rasters.

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

给予

我敢说有一种更多的图形方法

I dare say there is a more igraph approach to this

这篇关于Rigograph,如何绘制形状和栅格混合的顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆