Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some ways to do this manually, since you can read and plot images in R (here I use <code>rimage</code>) and graphs are typically also plotted on a x-y plane. You can use <code>igraph</code> for almost anything you want to do with graphs in R, and an alternative is to use my own package <code>qgraph</code> which can also be used to plot various types of graphs. </p> <p>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 <code>rasterImage</code>.</p> <p>I will start with undirected graphs (no arrows).</p> <p>First I load an image:</p> <pre><code># Load rimage library: library('rimage') # Read the image: data(logo) img &lt;- imagematrix(logo) </code></pre> <p>And sample a graph to be used (using an adjacency matrix):</p> <pre><code># Sample an adjacency matrix: set.seed(1) adj &lt;- matrix(sample(0:1,10^2,T,prob=c(0.8,0.2)),10,10) </code></pre> <p>Then in <code>qgraph</code>:</p> <pre><code>library('qgraph') # Run qgraph (plot the graph) and save the layout: L &lt;- 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)) </code></pre> <p>Which looks like this:</p> <p><img src="https://i.stack.imgur.com/FwEnV.png" alt="the network made in qgraph"></p> <p>In <code>igraph</code> 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):</p> <pre><code>library('igraph') # Make the graph G &lt;- graph.adjacency(adj,mode="undirected") # Create fixed layout: set.seed(1) L &lt;- 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)) </code></pre> <p><img src="https://i.stack.imgur.com/8jYPA.png" alt="The graph in igraph"></p> <p>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 <code>vsize</code> argument in <code>qgraph</code> or the <code>vertex.size</code> argument in <code>igraph</code>. (if you want I can look up the exact code for this, but it is not trivial).</p> <p>in <code>qgraph</code>:</p> <pre><code>L &lt;- 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)) </code></pre> <p><img src="https://i.stack.imgur.com/D2C3N.png" alt="Directed graph in qgraph"></p> <p>in <code>igraph</code>:</p> <pre><code>G &lt;- graph.adjacency(adj) set.seed(1) L &lt;- 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)) </code></pre> <p><img src="https://i.stack.imgur.com/0a2A5.png" alt="Directed graph in igraph"></p> <h1>2013 update:</h1> <p>Please mind that <code>rimage</code> is no longer on CRAN but you can use <code>png</code> or the <code>ReadImages</code> library. I have just updated <code>qgraph</code> to include functionality to do this a lot easier. See this example:</p> <pre><code># Download R logo: download.file("http://cran.r-project.org/Rlogo.jpg", file &lt;- tempfile(fileext = ".jpg"), mode = "wb") # Sample an adjacency matrix: set.seed(1) adj &lt;- 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) </code></pre> <p>This requires <code>qgraph</code> version 1.2 to work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload