gpt4 book ai didi

r - 如何将不同的图像分配给 igraph 中的不同顶点?

转载 作者:行者123 更新时间:2023-12-04 10:33:29 24 4
gpt4 key购买 nike

我看过 this question这看起来很相似,但我很难让它处理我的数据。

假设我的边缘列表包含以下内容:

P1 P2 weight
a b 1
a c 3
a d 2
b c 8

我用 read.csv收集数据,然后将其转换为矩阵。然后我使用以下图形绘制它:
g=graph.edgelist(x[,1:2],directed=F)
E(g)$weight=as.numeric(x[,3])
tkplot(g,layout=layout.fruchterman.reingold,edge.width=E(g)$weight)

这将返回一个具有顶点和边的网络。我想用一个图像替换顶点 a,用另一个图像替换顶点 b,依此类推。我知道如何将相同的图像应用于所有人,但我想将不同的图像应用于每个顶点。我该怎么做?

编辑 :按照 user20650 的要求在下面添加可重现的代码
# loading libraries
library(igraph)
library(rgdal)

# reading data from edgelist
x <- read.csv('edgelist', colClasses = c("character","character","numeric"), header=T)
# however, to replicate the data, use this line instead (Above line included just to show how I get the data)
x <- data.frame(P1 = c("a","a","a","b"), P2 = c("b","c","d","c"), weight = c(1,3,2,8))

# converting x to a matrix
x = as.matrix(x)

# preparing graph (getting rid of arrows, edge colors)
g = graph.edgelist(x[,1:2], directed=F)
E(g)$weight=as.numeric(x[,3])
E(g)[weight<=1]$color='dodgerblue'
E(g)[weight>=2&weight<=3]$color='dodgerblue1'
E(g)[weight>=4&weight<=7]$color='dodgerblue2'
E(g)[weight>=8&weight<=9]$color='dodgerblue3'
E(g)[weight==10]$color='dodgerblue4'

# plot the graph
# beginning of stuff I do not do anymore - the tkplot and adj lines below here I do not do anymore as they have been replaced with suggestions by user20650
tkplot(g, canvas.width=640, canvas.height=640, layout=layout.fruchterman.reingold, edge.width=E(g)$weight)

# just to make sure everything is correct, I was also verifying with this
adj <- get.adjacency(g, attr='weight')
# end of stuff I do not do anymore and I replaced it with what follows

# this is where I started placing user20650's lines (survcont1.png through survcont13.png are local files - 1 is the image for a, 2 for b, and so on)
url <- paste0("survcont", 1:13, ".png")

# my mapply which I guess I don't need anymore (I'm using rgdal because it is a library I already have that can read the images, am willing to use a better method if one exists)
mapply(readGDAL, url)
img <- lapply(url, png::readPNG)

set.seed(1)
adj <- matrix(sample(0:1,3^2,T,prob=c(0.8,0.8)),13,13)
g <- graph.adjacency(adj)
set.seed(1)

l <- layout.fruchterman.reingold(g)
l[,1]=(l[,1]-min(l[,1]))/(max(l[,1])-min(l[,1]))*2-1
l[,2]=(l[,2]-min(l[,2]))/(max(l[,2])-min(l[,2]))*2-1

# I added in the label so I can verify if the right vertices are showing up in the right places, I will remove in final version, also added in the edge weights
plot(g, layout=l, vertex.size=10, vertex.shape="square", vertex.color="#00000000", vertex.frame.color="#00000000", vertex.label="", edge.width=E(g)$weight)

# and finally plotting of the images
for(i in 1:nrow(l)) {
rasterImage(img[[i]], l[i, 1]-0.2, l[i, 2]-0.2, l[i, 1]+0.2, l[i, 2]+0.2)
}

我猜 adj 行出了点问题,而且我没有将我的数据链接到图像。我也不明白为什么我需要 set.seed。

图像绘图很棒,但我的原始边缘宽度和颜色没有,我不确定图像 1 是否链接到 a、2 到 b,等等。

最佳答案

您可以使用 Sacha's answer在您链接到的问题中,要做到这一点。如果您的图像存储在列表中,只需遍历它即可呈现 png 文件。我不得不调整手动调整(从 0.1 到 0.2)来调整图像大小。

编辑 使用 OP 的数据并添加边缘权重和颜色(删除原始帖子,因为这在很大程度上重复了它)

首先需要一些顶点图像。

# As i dont have access to your images i will download and use the 
# images as before. We need four images as there are four vertices
# You dont need to do this bit exactly, all you need to do is read
# in your images into your R session, in a list called img

url <- paste0("http://pngimg.com/upload/cat_PNG", 1632:1635, ".png")
mapply(download.file, url, basename(url))
img <- lapply( basename(url), png::readPNG)
library(igraph)

# data
x <- data.frame(P1 = c("a","a","a","b"),
P2 = c("b","c","d","c"),
weight = c(1,3,2,8))

# this reads in the third column which you can then assign to be weights
g <- graph.data.frame(x, directed=FALSE)
# check
E(g)$weight

# edge colour - you might need to tweak this depending on your
# data, with the right argument etc
E(g)$colour <- as.character(cut(as.numeric(E(g)$weight),
breaks = c(0, 1, 3, 7, 9, 10),
labels=paste0("dodgerblue", c("", 1:4))))

# you need to set the seed as the layout function is an
# iterative process and not deterministic
set.seed(1)
l <- layout.norm(layout.fruchterman.reingold(g),
xmin=-1, xmax=1, ymin=-1, ymax=1)


par(mar=rep(0,4))
plot(g, layout=l, vertex.size=20, vertex.shape="square",
vertex.color="#00000000", vertex.frame.color="#00000000",
vertex.label="", edge.width=E(g)$weight, edge.color=E(g)$colour)

# and finally plotting of the images
for(i in 1:nrow(l)) {
rasterImage(img[[i]], l[i, 1]-0.2, l[i, 2]-0.2, l[i, 1]+0.2, l[i, 2]+0.2)
}

enter image description here

关于r - 如何将不同的图像分配给 igraph 中的不同顶点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189497/

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