R:创建节点为图像的图表
我正在尝试创建一个图(图论中的图、节点和边等),其中每个节点由文件中的图像(最好是某种光栅格式)表示。我查看了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一些方法可以手动执行此操作,因为您可以在 R 中读取和绘制图像(此处我使用 rimage),并且图形通常也会绘制在 xy 平面上。您可以使用 igraph 来完成几乎任何您想在 R 中使用图形进行的操作,另一种方法是使用我自己的包 qgraph,它也可以用于绘制各种类型的图形图表。
在这两个包中,节点的放置在矩阵中指定/给出,每个节点有两列和一行,指示 x 和 y 位置。这两个包也绘制在 -1 到 1 的水平和垂直区域上。因此,通过该布局矩阵,我们可以使用 rasterImage 将图像绘制在正确的位置。
我将从无向图(没有箭头)开始。
首先,我加载图像:
并对要使用的图形进行采样(使用邻接矩阵):
然后在 qgraph 中:
如下所示:
在
igraph
中,您首先需要进行布局。该布局还需要重新缩放以适应 -1 到 1 的绘图区域(这是由 igraph 本身在绘图函数中完成的):现在,如果您想要有向图,那就不那么简单了,因为箭头需要指向图像的边缘。最好的方法是使用图像大小的不可见方形节点。为此,您需要摆弄
qgraph
中的vsize
参数或igraph
中的vertex.size
参数。 (如果你愿意,我可以查找确切的代码,但这并不简单)。在
qgraph
中:在
igraph
中:2013 年更新:
请注意,
rimage
不再出现在 CRAN 上,但您可以使用 < code>png 或ReadImages
库。我刚刚更新了qgraph
以包含更轻松地完成此操作的功能。请参阅此示例:这需要
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 useigraph
for almost anything you want to do with graphs in R, and an alternative is to use my own packageqgraph
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:
And sample a graph to be used (using an adjacency matrix):
Then in
qgraph
: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):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 inqgraph
or thevertex.size
argument inigraph
. (if you want I can look up the exact code for this, but it is not trivial).in
qgraph
:in
igraph
:2013 update:
Please mind that
rimage
is no longer on CRAN but you can usepng
or theReadImages
library. I have just updatedqgraph
to include functionality to do this a lot easier. See this example:This requires
qgraph
version 1.2 to work.看一下上一个问题:
将自定义图像放入绘图窗口中 - 作为自定义数据标记或注释这些标记
并且,请查看:
http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html
http://www.stat.auckland.ac.nz/~paul/RGraphics/chapter1.html
Take a look at a previous question:
Placing Custom Images in a Plot Window--as custom data markers or to annotate those markers
And, take a look at:
http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html
http://www.stat.auckland.ac.nz/~paul/RGraphics/chapter1.html