Question

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.

Was it helpful?

Solution

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))

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top