1

I have about two dozen networks that I want to plot using the same layout (they all share the same vertices). I'm new to R and igraph, so I have come up with this solution, which probably isn't very elegant. Now I'm stuck though. I'd like to know how I can get object names (in this case: V_turn1 etc.) into my plot titles and, if possible, into the file names.

I have added some random networks to make it easier to make it reproducible. It goes a little something like this:

print("begin")
library("igraph")

V_turn1 <- erdos.renyi.game(n=10,p.or.m=.2,type="gnp",directed=T)
V_turn2 <- erdos.renyi.game(n=10,p.or.m=.1,type="gnp",directed=T)
V_turn3 <- erdos.renyi.game(n=10,p.or.m=.3,type="gnp",directed=T)
V_turn4 <- erdos.renyi.game(n=10,p.or.m=.3,type="gnp",directed=T)

layout.old <- layout.random(V_turn1) 
# I need the same layout for all renderings, because the nodes are all the same across my network data 
list_of_graphs <- c("V_turn1", "V_turn2", "V_turn3", "V_turn4")

png(file="Networks_V3_%03d.png", width=1000,height=1000)

for(i in list_of_graphs){

        plot(get(i), layout=layout.old)  
        title(deparse(list_of_graphs))
        } 
        dev.off()

"deparse(list_of_graphs)" obviously doesn't work...

Actually, I'd be even happier if I could specify real titles for each iteration of the loop, i.e. in a new character vector or something (like "This is Turn 1" for V_turn1). I feel like there must be an obvious solution, but I so far nothing I've tried worked. Thank you for reading.

Markus D
  • 187
  • 1
  • 3
  • 10

2 Answers2

1

You can use main= argument of plot.igraph:

list_of_graphs <- c("V_turn1", "V_turn2", "V_turn3", "V_turn4")
list_of_titles <- c("This is Turn 1","This is Turn 2","This is Turn 3","This is Turn 4")
lapply(seq_along(list_of_graphs),
         function(i) plot(get(list_of_graphs[i]), 
                          layout=layout.old,
                          main=list_of_titles[i]))

Of course if you have a certain pattern for the titles , you can use list_of_graphes to create list_of_titles. For example , here I remove the first 2 letters from graph name to create graph title:

list_of_titles <- paste('This is',gsub('V_','',list_of_graphs))
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thank you for the idea! I'm going to look into the lapply function. As it is, I couldn't get your code to run, though. What should I make of list_along? R returns an error regarding that: `Error in lapply(list_along(list_of_graphs), function(i) plot(get(list_of_graphs[i]), : could not find function "list_along"` I'm not sure what I'm doing wrong here. Still, thank you for the suggestions. – Markus D Jul 10 '13 at 14:28
  • @MarkusD it is `seq_along` ,not `listçalong`. fixed now. Sorry. – agstudy Jul 10 '13 at 14:29
  • Oh, well... I should have figured that out. Thank you, that worked magnificently. – Markus D Jul 10 '13 at 14:34
1

You're almost there with your current solution. At the moment your for loop is iterating over the list_of_graphs so each i will be an element from that list.

If you're happy with using your variable names as the title you can just use i as the title:

plot(...)
title(i)

The title can also simply be passed in as the main argument to the plot function:

plot(..., main=i)

If you don't want to use the variable names as the title, this is going to require more work. First you'll need to iterate over an index instead, and use that to look up both the graph and the title:

titles <- c("Title 1", "Title 2", "Title 3", "Title 4")
for (i in seq_along(list_of_graphs)) {
   old.i <- list_of_graphs[i]  # This is `i` in your current code
   plot(get(old.i), layout=layout.old, main=titles[i])
}

However @agstudy's answer is more elegant.

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • thank you for your answer. I was trying exactly that, and have tried again just now, but although the code runs smoothly without error, I don't actually get any title in the output file. – Markus D Jul 10 '13 at 14:14
  • oh er, my bad, I see what happened. You need to change the for loop as well for it to work. I'll edit my explanation – Scott Ritchie Jul 10 '13 at 14:27
  • Thanks! lapply worked for now, but I'd still be happy to know how to improve my for-loop for future reference. – Markus D Jul 10 '13 at 14:36
  • Of course, JUST `i`... Well, that's a burn. All of this works beautifully and the index solution is pretty much what I've been trying to achieve for the past hour or two. Thanks for the effort. Much appreciated! – Markus D Jul 10 '13 at 14:48