[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[igraph] Question about ploting graph in animation
From: |
Zhu, Hong |
Subject: |
[igraph] Question about ploting graph in animation |
Date: |
Wed, 21 Apr 2010 12:09:00 -0400 |
Hello igraph users,
I am trying to create animation and plot the network after each iteration. It
does show the animation and change of the network.
But the problem is the graph does not have a fixed orientation, while on the
contrast, the nodes move around over time with the
iterations.
I wonder whether there is something wrong with "layout" option. Because if we
use "circle" or "tree" layout, the nodes are fixed but
it is not the graphical presentation we want.
Any comments to help? Thanks a lot in advance!
Best,
Hong
The following is the code.
#
mrule <- function(A, y) {
# Majority rule dynamics
# A = adjacency matrix
# y = state of the vertices at time t
#
apply(cbind(A, y), 1, z=y, function(x, z){
n <- length(x)
if (sum(x[-n]) == 0) return(x[n])
z <- z[x[-n]==1]
zf <- factor(z, levels=c(0,1))
cz <- table(zf)
if (cz[1] != cz[2]) c(0,1)[which.max(cz)] else rbinom(n=1, size=1,
prob=0.5)
})
}
## nt is step, try nt=(25, 50, 100)
nt <- 50
ani.options(interval = 0.5, nmax = nt ,title="Animation for mrule")
ani.start()
par(mar=c(3,2.5,1,0.2),pch=20,mgp=c(1.5,0.5,0))
interval = ani.options("interval")
g1<-ba.game(100,m=2,directed=F)
g1<-add.edges(g1,c(97,98,98,99))
nv<-vcount(g1)
ne<-ecount(g1)
##Assign original status
y0<-V(g1)$status<-rbinom(nv, size=1, prob=0.5) # state of the nodes at time #= 0
V(g1)$color<-ifelse(V(g1)$status==0,"red","blue")
A <- get.adjacency(g1,binary=TRUE)
y_m<-y_p <- y0
s1_m <- rep(NA, nt)
s1_m[1] <- sum(y_m)
plot(g1,layout=layout.fruchterman.reingold,
vertex.size=4,vertex.label=NA,edge.arrow.size=0.5)
for (i in 2:nt) {
y_m <- mrule(A, y=y_m) # State at time = i - 1
V(g1)$color<-ifelse(y_m==0,"red","blue")
plot(g1,layout=layout.fruchterman.reingold,vertex.size=4,vertex.label=NA,edge.arrow.size=0.5)
Sys.sleep(interval)
#dev.off()
}
_______________________________
Hong Zhu
Ph.D. Candidate, Biostatistics
Bloomberg School of Public Health
Johns Hopkins University
- [igraph] Question about ploting graph in animation,
Zhu, Hong <=