0

I'm trying to set images to vertices by their attribute (type), using this code:

library(igraph)
library(png)

gi <- graph.formula(child -- org -- person)

img1 <- readPNG("baby-boy.png")
img2 <- readPNG("knife.png")
img3 <- readPNG("detective.png")

V(gi)$raster <- ni$type
V(gi)$raster <- gsub("child", "img1", V(gi)$raster)
V(gi)$raster <- gsub("org", "img2", V(gi)$raster)
V(gi)$raster <- gsub("person", "img3", V(gi)$raster)

plot(gi, layout_as_star(gi),
 vertex.label.cex = 0.5,
 edge.arrow.size = 0.1,
 vertex.shape="raster",
 vertex.size = 16, 
 vertex.size2 = 16)

As a result I get the following error message:

"Error in rasterImage(ras, coords[i, 1] - size[i], coords[i, 2] - size2[i],  : 
invalid color name 'img1'" 

and no vertices are plotted. How could I solve this problem?

Vincent Guillemot
  • 3,394
  • 14
  • 21
  • Welcome to SO. It would help to have a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). A.k.a. a small dataset so we can run the code to get to the plotting part. Also have you looked at these 2 examples? [ex1](https://stackoverflow.com/questions/4975681/r-creating-graphs-where-the-nodes-are-images) and [ex2](https://stackoverflow.com/questions/29189497/how-to-assign-different-images-to-different-vertices-in-an-igraph) – phiver Sep 03 '18 at 10:31

1 Answers1

0

You seem to be treating V(gi)$raster as a list of the names of images. It is supposed to contain the actual rasters. Here is a small example. In order to get the list

library(igraph)
library(png)

set.seed(1234)
g = erdos.renyi.game(4, 0.5)

## Sorry. This part is not reproducible.
## You will need to get your own images.
img1 = readPNG("006.png")
img2 = readPNG("040.png")
img3 = readPNG("068.png")
img4 = readPNG("104.png")

V(g)$raster = replicate(vcount(g), img1, simplify=FALSE)
V(g)$raster[[2]] = img2
V(g)$raster[[3]] = img3
V(g)$raster[[4]] = img4

plot(g, vertex.shape="raster", vertex.label=NA)

Graph with images for vertices

G5W
  • 36,531
  • 10
  • 47
  • 80