R:创建节点为图像的图形 [英] R: Creating graphs where the nodes are images

查看:148
本文介绍了R:创建节点为图像的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个图(如图理论中的图,节点和边等),其中每个节点由来自文件的图像(最好是某种光栅格式)表示。我查看了RGraphviz软件包,但不幸的是, shapefill 属性为Currently unsupported。



我也看看iGraph,但浏览文档我似乎无法找到关于在图形中使用图像的任何内容。



有没有人有使用图像文件的经验从R内生成的图?

解决方案

有些方法可以手动执行此操作,因为您可以读取并绘制R这里我使用 rimage ),图形通常也绘制在xy平面上。你可以使用 igraph 几乎任何你想对R中的图形做的任何事情,另一种方法是使用我自己的包 qgraph

在这两个软件包中,节点的位置都是由一个矩阵指定/给出的,每个节点有两列和一行,表示x和y的位置。这两个软件包也绘制-1到1的水平和垂直区域。因此,使用该布局矩阵,我们可以使用 rasterImage 来将图像绘制在正确的位置。

首先我加载一张图片:

 #加载rimage库:
库('rimage')

#阅读图片:
数据(logo)
img< - imagematrix(logo)

并且使用一个图形(使用邻接矩阵):

 #示例一个邻接矩阵:
set.seed(1)
adj < - matrix(sample(0:1,10) ^ 2,T,prob = c(0.8,0.2)),10,10)

qgraph

  library('qgraph')

#运行qgraph(绘制图形)并保存布局:
L < - qgraph(adj,borders = FALSE,vsize = 0,labels = F,directed = F)$ layout

#绘制图像:
apply(L,1,function(x)rasterImage(img,x [1] -0.1,x [2] -0.1,x [1] + 0.1,x [2 ] +0.1))

看起来l ike this:





igraph 中,您首先需要进行布局。这个布局也需要重新调整以适应-1到1的绘图区域(这是通过plot函数中的igraph本身完成的):

 <$ c 


$< - graph.adjacency(adj,mode =undirected)

#创建固定布局:
set.seed(1)
L < - layout.fruchterman.reingold(G)

#将布局重新缩放为-1至1 $ b $ (L [,1]) - (L [,1]) - (L [,1]))/(max(L [,1])) b L [,2] =(L [,2] -min(L [,2]))/(max(L [,2]) - min(L [,2]))* 2-1

#Plot:
plot(G,layout = L,vertex.size = 0,vertex.frame.color =#00000000,vertex.label =)

$设置图像:
apply(L,1,function(x)rasterImage(img,x [1] -0.1,x [2] -0.1,x [1] + 0.1,x [2] + 0.1))



现在,如果您需要有向图,它并不重要,因为箭头需要指向图片。最好的方法是使用与图像大小相近的不可见方形节点。要做到这一点,你需要摆弄 qgraph 顶点中的 vsize 参数。大小参数放在 igraph 中。 (如果你想我可以查找确切的代码,但它不是微不足道的)。



qgraph

  L < -  qgraph(adj,borders = FALSE,vsize = 10,labels = F,shape =square ),color =#00000000)$ layout 

apply(L,1,function(x)rasterImage(img,x [1] -0.1,x [2] -0.1,x [1 ] + 0.1,x [2] +0.1))



igraph :

  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 (G,layout = L,vertex.size = 17,vertex.shape =square,L [,2]) - min(L [,2]))* 2-1

vertex.color =#00000000,vertex.frame.color =#00000000,vertex.label =)

apply(L,1,function(x)rasterImage(img,x [1] -0.1,x [2] -0.1,x [1] + 0.1,x [2] +0.1))


2013更新:



alt =igraph中的定向图请注意, rimage 不再在CRAN上,但您可以使用 png ReadImages 库。我刚刚更新了 qgraph ,以便更轻松地实现此功能。看看这个例子:

 #下载R logo:
download.file(http://cran.r- project.org/Rlogo.jpg,file< - tempfile(fileext =.jpg),
mode =wb)

#对一个邻接矩阵进行采样:
(1)
adj < - 矩阵(样本(0:1,10 ^ 2,TRUE,prob = c(0.8,0.2)),10,10)

#运行qgraph:
qgraph(adj,images = file,labels = FALSE,borders = FALSE)

这需要 qgraph 版本1.2才能使用。


I'm trying to create a graph (graph as in graph theory, nodes and edges, etc.) where each node is represented by an image from file (preferably some raster format). I've looked in the RGraphviz package, but unfortunately the shapefill attribute is "Currently unsupported".

I also had a look at iGraph, but browsing through the documentation I couldn't seem to find anything on using images in the graphs.

Does anyone have experience with using image files in graphs generated from within R?

解决方案

There are some ways to do this manually, since you can read and plot images in R (here I use rimage) and graphs are typically also plotted on a x-y plane. You can use igraph for almost anything you want to do with graphs in R, and an alternative is to use my own package qgraph which can also be used to plot various types of graphs.

In both packages the placement of the nodes is specified/given in a matrix with two columns and a row for each node indicating the x and y location. Both packages also plot on a -1 to 1 horizontal and vertical area. So with that layout-matrix we can plot the images on the right locations using rasterImage.

I will start with undirected graphs (no arrows).

First I load an image:

# Load rimage library:
library('rimage')

# Read the image:
data(logo)
img <- imagematrix(logo)

And sample a graph to be used (using an adjacency matrix):

# Sample an adjacency matrix:
set.seed(1)
adj <- matrix(sample(0:1,10^2,T,prob=c(0.8,0.2)),10,10)

Then in qgraph:

library('qgraph')

# Run qgraph (plot the graph) and save the layout:
L <- qgraph(adj,borders=FALSE,vsize=0,labels=F,directed=F)$layout

# Plot images:
apply(L,1,function(x)rasterImage(img,x[1]-0.1,x[2]-0.1,x[1]+0.1,x[2]+0.1))

Which looks like this:

In igraph you first need to make the layout. This layout also needs to be rescaled to fit the -1 to 1 plotting area (this is done by igraph itself in the plot function):

library('igraph')

# Make the graph
G <- graph.adjacency(adj,mode="undirected")

# Create fixed layout:
set.seed(1)
L <- layout.fruchterman.reingold(G)

# Rescale the layout to -1 to 1
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

# Plot:
plot(G,layout=L,vertex.size=0,vertex.frame.color="#00000000",vertex.label="")

# Set images:
apply(L,1,function(x)rasterImage(img,x[1]-0.1,x[2]-0.1,x[1]+0.1,x[2]+0.1))

Now if you want directed graphs it is less trivial, as the arrows need to be pointing to the edge of the image. Best way to do this is to use invisible square nodes that are about the size of the image. To do this you need to fiddle around with the vsize argument in qgraph or the vertex.size argument in igraph. (if you want I can look up the exact code for this, but it is not trivial).

in qgraph:

L <- qgraph(adj,borders=FALSE,vsize=10,labels=F,shape="square",color="#00000000")$layout

apply(L,1,function(x)rasterImage(img,x[1]-0.1,x[2]-0.1,x[1]+0.1,x[2]+0.1))

in igraph:

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

plot(G,layout=L,vertex.size=17,vertex.shape="square",vertex.color="#00000000",vertex.frame.color="#00000000",vertex.label="")

apply(L,1,function(x)rasterImage(img,x[1]-0.1,x[2]-0.1,x[1]+0.1,x[2]+0.1))

2013 update:

Please mind that rimage is no longer on CRAN but you can use png or the ReadImages library. I have just updated qgraph to include functionality to do this a lot easier. See this example:

# Download R logo:
download.file("http://cran.r-project.org/Rlogo.jpg", file <- tempfile(fileext = ".jpg"), 
    mode = "wb")

# Sample an adjacency matrix:
set.seed(1)
adj <- matrix(sample(0:1, 10^2, TRUE, prob = c(0.8, 0.2)), 10, 10)

# Run qgraph:
qgraph(adj, images = file, labels = FALSE, borders = FALSE)

This requires qgraph version 1.2 to work.

这篇关于R:创建节点为图像的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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