|
From: | Walk to Sun |
Subject: | [igraph] Re: Hello |
Date: | Wed, 18 Feb 2009 03:09:27 +0800 |
Hi Dr Gábor Csárdi, Thank you for your help.
The R function is supposed to be data varying widely in structure and content. Could you please help adjusting parameter so that beginners can learn Igraph grammar? Best regards W 1. The Fruchterman-Reingold layout
algorithm library(igraph) library(Cairo) source("frgraphs.R")
g <-
read.graph("test.net",format="pajek") graphs <-
frgraphs() lay <- lapply(graphs,
layout.fruchterman.reingold,
niter=3000) CairoX11() #
CairoPNG(file="frplots.png") par(mai=c(0,0,0,0)) layout(matrix(1:16, nr=4,
byrow=TRUE)) layout.show(16) for (i in seq(along=graphs))
{ plot(graphs[[i]],
layout=lay[[i]],
vertex.label=NA, vertex.size=13,
edge.color="black",
vertex.color="red") } #
dev.off() 2. The tkplot editor of the R
package cs <-
leading.eigenvector.community.step(g) V(g)$color <-
ifelse(cs$membership==0, "lightblue",
"green") scale <- function(v, a, b)
{ v <- v-min(v) ; v <- v/max(v) ; v
<- v * (b-a) ; v+a } V(g)$size <-
scale(abs(cs$eigenvector), 10, 20) E(g)$color <-
"grey" E(g)[ V(g)[ color=="lightblue" ]
%--% V(g)[ color=="green" ] ]$color <-
"red" tkplot(g,
layout=layout.kamada.kawai,
vertex.label.font=2) coords <-
tkplot.getcoords(1) 3. Transparency with the Cairo R
package library(igraph) ;
library(Cairo) g <- barabasi.game(100,
power=0.6, m=10) V(g)$name <-
seq(vcount(g))-1 g <-
simplify(g) l <-
layout.fruchterman.reingold(g) l <- layout.norm(l, -1,1, -1,1)
fcs <-
fastgreedy.community(simplify(as.undirected(g))) Q <- round(max(fcs$modularity),
3) fcs <-
community.to.membership(g, fcs$merges, steps=which.max(fcs$modularity)-1
) CairoX11() # or
CairoPNG(file="fastgreedy.png") plot(g, layout=l, vertex.size=3,
vertex.label=NA,
vertex.color="#ff000033",
vertex.frame.color="#ff000033", edge.color="#55555533",
edge.arrow.size=0.3, rescale=FALSE,
xlim=range(l[,1]), ylim=range(l[,2]), main=paste(sep="",
"Fast greedy community detection,\nQ=",
Q)) g2 <- subgraph(g,
which(fcs$membership==fcs$membership[1])-1) l2 <- l[
which(fcs$membership==fcs$membership[1]),
] plot(g2, layout=l2, vertex.size=3,
vertex.label=V(g2)$name,
vertex.color="#ff0000", vertex.frame.color="#ff0000",
edge.color="#555555", vertex.label.dist=0.5,
vertex.label.cex=0.8,
vertex.label.font=2, edge.arrow.size=0.3,
add=TRUE, rescale=FALSE) nodes <-
which(fcs$membership==fcs$membership[1])-1 nodes <- paste(collapse=", ",
nodes) text(0,-1.3, nodes,
col="blue") #
dev.off() 4. Plotting the
diameter library(igraph) library(Cairo) source("frgraphs.R")
g <-
read.graph("test.net",format="pajek") d <-
get.diameter(g) E(g)$color <-
"grey" E(g)$width <-
1 E(g, path=d)$color <-
"red" E(g, path=d)$width <-
2 V(g)$label.color <-
"blue" V(g)$color <-
"SkyBlue2" V(g)[ d ]$label.color <-
"black" V(g)[ d ]$color <-
"red" plot(g,
layout=layout.fruchterman.reingold, vertex.label.dist=0,
vertex.size=15) title(main="Diameter of the
Zachary Karate Club network", xlab="created by
igraph 0.4") axis(1, labels=FALSE,
tick=TRUE) axis(2, labels=FALSE,
tick=TRUE) 5. Degree distributions for
nonlinear preferential attachment library(igraph) g <-
barabasi.game(100000) d <- degree(g,
mode="in") dd <- degree.distribution(g,
mode="in", cumulative=TRUE) alpha <- power.law.fit(d,
xmin=20) plot(dd, log="xy", xlab="degree",
ylab="cumulative frequency", col=1, main="Nonlinear
preferential attachment") lines(10:500,
10*(10:500)^(-coef(alpha)+1)) powers <- c(0.9, 0.8, 0.7,
0.6) for (p in seq(powers))
{ g <- barabasi.game(100000,
power=powers[p]) dd <- degree.distribution(g,
mode="in", cumulative=TRUE) points(dd, col=p+1,
pch=p+1) } legend(1, 1e-5, c(1,powers),
col=1:5, pch=1:5, ncol=1, yjust=0,
lty=0) 6, Plotting the dendrogram of a
community structure algorithm library(igraph) g <-
read.graph("test.net",format="pajek") wt <- walktrap.community(g,
modularity=TRUE) dend <- as.dendrogram(wt,
use.modularity=TRUE) plot(dend, nodePar=list(pch=c(NA,
20))) 7. Creating simple
animations library(igraph)
;library(Cairo) g <-
read.graph("test.net",format="pajek") l <- layout.kamada.kawai(g,
niter=1000) ebc <-
edge.betweenness.community(g) colbar <-
rainbow(6) colbar2 <- c(rainbow(5),
rep("black",15)) for (i in 1:20)
{ g2 <- delete.edges(g,
ebc$removed.edges[seq(length=i-1)])
eb <-
edge.betweenness(g2) cl <-
clusters(g2)$membership q <- modularity(g,
cl) E(g2)$color <-
"grey" E(g2)[ order(eb, decreasing=TRUE)[1:5]-1
]$color <- colbar2[1:5] E(g2)$width <-
1 E(g2)[ color != "grey" ]$width <-
2 #
CairoPNG(sprintf("eb-community-%04d.png",
i)) plot(g2, layout=l, vertex.size=6,
vertex.label=NA,
edge.label.color="red",
vertex.color=colbar[cl+2],
edge.label.font=2) title(main=paste("Q=", round(q,3)),
font=2) ty <- seq(1,by=-strheight("1")*1.5,
length=20) text(-1.3, ty, adj=c(0,0.5),
round(sort(eb, dec=TRUE)[1:20],2),
col=colbar2,
font=2) #
dev.off()
scan() } |
[Prev in Thread] | Current Thread | [Next in Thread] |