R:创建节点为图像的图表

发布于 2024-10-16 23:48:21 字数 200 浏览 1 评论 0原文

我正在尝试创建一个图(图论中的图、节点和边等),其中每个节点由文件中的图像(最好是某种光栅格式)表示。我查看了 RGraphviz 包,但不幸的是 shapefill 属性是“当前不受支持”。

我还查看了 iGraph,但浏览文档时我似乎找不到任何有关在图表中使用图像的信息。

有人有在 R 中生成的图表中使用图像文件的经验吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-10-23 23:48:21

有一些方法可以手动执行此操作,因为您可以在 R 中读取和绘制图像(此处我使用 rimage),并且图形通常也会绘制在 xy 平面上。您可以使用 igraph 来完成几乎任何您想在 R 中使用图形进行的操作,另一种方法是使用我自己的包 qgraph,它也可以用于绘制各种类型的图形图表。

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

我将从无向图(没有箭头)开始。

首先,我加载图像:

# Load rimage library:
library('rimage')

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

并对要使用的图形进行采样(使用邻接矩阵):

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

然后在 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))

如下所示:

qgraph 中制作的网络

igraph 中,您首先需要进行布局。该布局还需要重新缩放以适应 -1 到 1 的绘图区域(这是由 igraph 本身在绘图函数中完成的):

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

“igraph

现在,如果您想要有向图,那就不那么简单了,因为箭头需要指向图像的边缘。最好的方法是使用图像大小的不可见方形节点。为此,您需要摆弄 qgraph 中的 vsize 参数或 igraph 中的 vertex.size 参数。 (如果你愿意,我可以查找确切的代码,但这并不简单)。

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

qgraph 中的有向图

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

igraph 中的有向图

2013 年更新:

请注意,rimage 不再出现在 CRAN 上,但您可以使用 < code>png 或 ReadImages 库。我刚刚更新了 qgraph 以包含更轻松地完成此操作的功能。请参阅此示例:

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

这需要 qgraph 版本 1.2 才能工作。

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:

the network made in qgraph

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

The graph in igraph

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

Directed graph in qgraph

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

Directed graph in igraph

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文