Thanks for the Aspirin Guys
| Gabriel |
In a recent post, I lamented that I couldn’t figure out how to do loops and a few other things in R. The script at issue was intended to create a slideshow of network graphs which I’d then use Image Magick to convert to an animated gif. With some help from Brian and Kieran, I ultimately got it to work and the results are in this post (as well as a presentation at a mini-conference I made a few days later).
Kieran very generously further improved on the code at his own blog using simulated data. Kieran’s code has a certain elegance to it, but it’s an R style of elegance based on objects and lists and the like so it’s not completely intuitive to me, despite his thorough commenting. Anyway, I worked his suggestions into my code (see below) and it works great.
Since Kieran was “interested to see whether this approach was at all faster,” I ran the old version and the new version with timestamps. (To make it fair, I commented out the “pdf” version of the flipbook, which only appears in the “old” code — this saves about one second). Turns out they’re almost exactly the same speed — about 26 or 27 seconds (it varies about a second or two every time I do it, sometimes new is faster, sometimes old). The vast majority of that half a minute is taken up by Image Magick. The actual R code only takes about 7 seconds to execute in either version. I think that’s amazingly fast to import 60 or so datasets (albeit small datasets), generate two different FR layouts, apply the layouts to 60 or so graphs (albeit graphs that are written directly to disk rather than to screen), and do miscellaneous housekeeping. I haven’t run a timestamped comparison, but my impression is that this is much faster than comparable operations in Pajek or NWB and appreciably faster than doing scatterplots in Stata.
# File-Name: chrnetwork.R # Date: 2010-03-11 # Created Date: 2009-11-24 # Author: Gabriel Rossman (w a fair amount of help from BR and KJH) # Purpose: graph CHR station network # Data Used: ties_bounded.net # Packages Used: igraph plyr timestamp() library(igraph) library(plyr) setwd("~/Documents/Sjt/radio/survey") #ties -- including ties to non-top40 (who can't reciprocate) chrnet <- read.graph("ties.net", c("pajek")) pdf("~/Documents/book/images/chrnetwork.pdf") plot.igraph(chrnet, layout=layout.fruchterman.reingold, vertex.size=4, vertex.label=NA, vertex.color="red", edge.color="gray20", edge.arrow.size=0.3, margin=0) dev.off() #ties bounded to only top 40, includes adoption time color-codes, but use is optional chrnetbounded <- read.graph("ties_bounded_humpcolor.net", c("pajek")) la = layout.fruchterman.reingold(chrnetbounded) #create layout for use on several related graphs #graph structure only pdf("~/Documents/book/images/chrnetworkbounded.pdf") plot.igraph(chrnetbounded, layout=la, vertex.size=4, vertex.label=NA, vertex.color="red", edge.color="gray20", edge.arrow.size=0.3, margin=0) dev.off() #graph color coded diffusion pdf("~/Documents/book/images/chrnetworkboundedcolor.pdf") plot.igraph(chrnetbounded, layout=la, vertex.size=4, vertex.label=NA, edge.color="gray80", edge.arrow.size=0.3, margin=0) dev.off() #flipbook timestamp() setwd("~/Documents/Sjt/radio/survey/flipbook") filenames <- list.files(getwd(), pattern="\\.net$") ind <- order(as.numeric(gsub("[^[:digit:]]", "", filenames))) filenames <- filenames[ind] g.list <- llply(filenames, read.graph, format="pajek") png(file="~/Documents/book/images/flipbook/chrnet_hc%d.png", width=600, height=600, units="px") l_ply(g.list, plot, layout=la, vertex.size=4, vertex.label=NA, edge.color="gray60", edge.arrow.size=0.3, margin=0) dev.off() timestamp() ### Finally, generate the gif without having to renumber the files ### individually. png.filenames <- list.files("~/Documents/book/images/flipbook/", pattern="\\.png$") timestamp() ## Resort properly again ind <- order(as.numeric(gsub("[^[:digit:]]", "", png.filenames))) png.filenames <- png.filenames[ind] png.filenames <- paste("~/Documents/book/images/flipbook/", png.filenames, sep="") # add the path back png.string <- capture.output(cat(png.filenames)) ## Open a pipe to the shell to execute the convert command ## directly. timestamp() gifpipe <- pipe(paste("convert", png.string, "~/Documents/book/images/flipbook/chrnet_humps.gif", sep=" "), "w") close(gifpipe) timestamp()