gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:33:59 25 4
gpt4 key购买 nike

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

给予

enter image description here

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

关于R igraph,如何绘制混合形状和光栅的顶点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40120912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com