title: How do you assign images conditionally to vertices in iGraph?
body: <p>I am having difficulties with assigning images (png) conditionally to vertices in iGraph. For example, I want to plot so that if col.2=='a', then image=='1.png'. I have been able to plot the vertices as pngs, but the images do not correspond correctly with the vertex IDs. Any help or advice would be very gratefully received! Here's a minimal example:</p>
  
  <pre><code>#minimal example
  
  #(1)plot graph.data frame from three unqiue columns ('col.1', 'col.2', 'col.3')
  #(2)create two new columns to include png files ('nodes1.tmp', 'nodes2.tmp')
  #(3)assign images to vertices conditional on categories ('Groups' and 'Members')
  
  
  #required
  library(igraph)
  library(png)
  
  
  #import data from files
  my_data &lt;- read.csv(file="my_data.csv")
  
  #load two different sets of images
  images_1 &lt;- list.files("dir/folder1", pattern=".png$") #loads .pngs for groups
  images_2 &lt;- list.files("dir/folder2", pattern=".png$") #loads .pngs for members
  
  #assign the vectors
  col.1 &lt;- my_data$[,1] #nodes (groups)
  col.2 &lt;- my_data$[,2] #nodes (members)
  col.3 &lt;- my_data$[,3] #weights (gender)
  col.4 &lt;- my_data$[,4] #subets (age)
  
  #bind vectors to data.frame (two new cols for images will be added)
  df1 &lt;- data.frame(cbind(col.1, col.2, col.3, col.4))
  
  #create image vectors
  all_images &lt;- cbind(images_1, images_2)
  nodes1.tmp &lt;- as.character(my_data$[,5]) # new col for groups images
  nodes2.tmp &lt;- as.character(my_data$[,6]) # new col for members images
  
  #populate nodes1.tmp column with .pngs, conditional on col.1 (groups)
  nodes1.tmp &lt;- ifelse(col.1 %in% "Name1", images_1[1], 
                       ifelse (col.1 %in% "Name2", images_1[2], NA))
  
  #populate nodes2.tmp column with .pngs, conditional on col.2 (members)
  nodes2.tmp &lt;- ifelse(col.2=='a', images_1[1],
                       ifelse(col.2=='b', images_2[2],
                       ifelse(col.2=='c', images_2[3],
                       ifelse(col.2=='d', images_2[4],
                       ifelse(col.2=='e', images_2[5],
                       ifelse(col.2=='f', images_2[6], NA))))))
  
  
  
  #read all pngs
  all_rasters &lt;- cbind(nodes1.tmp, nodes2.tmp)
  rasters &lt;- lapply(all_rasters, readPNG)
  unlist(rasters)
  
  #Subset df1 based on "Group1"
  df2 &lt;- subset(df1, df1$col.1 %in% "Group1") 
  
  #create graph data frame
  df2.m &lt;- as.matrix(df2)
  #edge list from col.5 and col.6 (images columns)
  g_edglst.1 &lt;- graph.edgelist(df2.m[,c(5,6)], directed = F)
  #weights from col.3 (gender)
  E(g_edglst.1)$weight=as.character(g_edglst.1[,3])
  V(g_edglst.1)$raster &lt;- rasters[1:length(V(g_edglst.1))]
  E(g_edglst.1)$color &lt;- ifelse((g_edglst.1)$list.3 %in% "m", "red",      "darkslategrey") #colour-code edges based on gender m/f
  
  plot(g_edglst.1, vertex.label = V(g_edglst.1)$name, layout=layout.auto, 
       vertex.shape="raster")
  </code></pre>