如何在igraph中将不同的图像分配给不同的顶点? [英] How to assign different images to different vertices in an igraph?

查看:61
本文介绍了如何在igraph中将不同的图像分配给不同的顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这个问题,类似,但是我很难使它与我的数据一起使用.

I've looked at this question which seems similar but I am having difficulty getting it to work with my data.

比方说,我的边缘列表包含以下内容:

Let's say my edgelist consists of the following:

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

我使用read.csv收集数据,然后将其转换为矩阵.然后使用以下命令对其进行绘制:

I use read.csv to collect the data, and then I convert it to a matrix. Then I graph it using the following:

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,依此类推.我知道如何将相同的图像应用于所有图像,但是我想将不同的图像应用于每个顶点.我该怎么做呢?

And this returns a network with vertices and edges. I would like to replace vertex a with one image, vertex b with another and so on. I know how to apply the same image to all, but I want to apply a different image to every vertex. How do I go about doing this?

编辑:根据

我猜测adj线出了点问题,而且我不知何故没有将我的数据链接到图像.我也不知道为什么需要set.seed.

I am guessing something is going wrong with the adj line and I'm somehow not linking my data to the images. I also don't get why I need to set.seed.

图像绘制很好,但是我的原始边缘宽度和颜色没有,而且我不确定图像1链接到a,2链接到b,依此类推.

The images plot, which is great, but my original edge widths and colors do not and I am not sure image 1 is linking to a, 2 to b, and so on.

推荐答案

您可以使用

You can use Sacha's answer in the question you link to, to do this. If your images are stored in a list, just iterate through it to render the png files. I had to tweak the manual adjustment (from 0.1 to 0.2) to resize the image.

编辑:使用OP的数据并添加边缘粗细和颜色(已删除的原始帖子在很大程度上重复了此内容)

EDIT Using OP's data and adding edge weight and colour (deleted original post as this largely repeats it)

首先需要一些顶点图像.

First need some images for the vertices.

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

这篇关于如何在igraph中将不同的图像分配给不同的顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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