library("igraph") library("igraphdata") library("network") library("networkD3") library("circlize") library(sand) set.seed(42) g <- sample_gnp(50, 0.1, directed=FALSE) edge_density(g) ##density of g g.pa <- sample_pa(50, 1, directed=FALSE) edge_density(g.pa) ##density of g g g.pa data("aidsblog") summary(aidsblog) gm <- get.adjacency(g, sparse=F) aidsm <- get.adjacency(aidsblog, sparse=F) igraph.options(vertex.size=5, vertex.label=NA, vertex.color="orange", edge.size=15, edge.color="grey", edge.arrow.size=0.2) par(mfrow=c(1, 3)) # random layout plot(g, layout=layout_randomly) # random layout plot(g, layout=layout_randomly) par(mfrow=c(1, 3)) # random layout plot(g, layout=layout_randomly) plot(g.pa, layout=layout_randomly) title("random graph") # random layout plot(g, layout=layout_randomly) title("random graph") plot(g.pa, layout=layout_randomly) title("pref. attachment") plot(aidsblog, layout=layout_randomly) title("Blog Network") # random layout plot(g, layout=layout_randomly) # random layout plot(g, layout=layout_randomly) title("random graph") plot(g.pa, layout=layout_randomly) title("pref. attachment") plot(aidsblog, layout=layout_randomly) title("Blog Network") # circular layout plot(g, layout=layout.circle) title("random graph") plot(g.pa, layout=layout.circle) title("pref. attachment") plot(aidsblog, layout=layout.circle) title("Blog Network") # Fruchterman & Reingold layout plot(g, layout=layout_with_fr) title("random graph") title("random graph") plot(g.pa, layout=layout_with_fr) title("pref. attachment") plot(aidsblog, layout=layout_with_fr) title("Blog Network") plot(g, layout=layout_with_mds) title("random graph") plot(g.pa, layout=layout_with_mds) title("pref. attachment") plot(aidsblog, layout=layout_with_mds) title("Blog Network") plot(g.pa, layout=layout_with_fr, vertex.label = V(g)$name) V(g)$name V(g) g V(g)$name # MDS layout plot(g, layout=layout_with_mds) title("random graph") plot(g.pa, layout=layout_with_mds) title("pref. attachment") plot(aidsblog, layout=layout_with_mds) title("Blog Network") plot(g, layout=layout_with_mds) title("random graph") plot(g.pa, layout=layout_with_mds) title("pref. attachment") plot(aidsblog, layout=layout_with_mds) title("Blog Network") # MDS layout plot(g, layout=layout_with_mds) # MDS layout plot(g, layout=layout_with_mds) plot(g, layout=layout_with_mds) title("random graph") plot(g.pa, layout=layout_with_mds) title("pref. attachment") plot(aidsblog, layout=layout_with_mds) title("Blog Network") plot(g, layout=layout_with_kk) title("random graph") plot(g.pa, layout=layout_with_kk) title("pref. attachment") plot(aidsblog, layout=layout_with_kk) title("Blog Network") dev.off() distances(g, mode = "all") distances(g.pa, mode = "all") distances(aidsblog, mode = "all") par(mar=c(0.5,0.5,0.5,0.5)) deg_g <- igraph::degree(g, mode = "all", normalized = T) deg_g deg_g*100 V(g)$size <- deg_g*100 V(g)$size V(g)$name V(g)$name <- 1:vcount(g) V(g)$label.color <- "blue" V(g)$name plot(g, layout=layout_with_fr, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title(main="Random graph. Node size = node degree centrality", cex.main=1) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins clos_g <- igraph::closeness(g, mode = "all", normalized = T) V(g)$size <- clos_g*30 V(g)$label.color <- "blue" plot(g, layout=layout_with_fr, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins V(g)$size <- betw_g*100 V(g)$label.color <- "blue" plot(g, layout=layout_with_fr, layout=layout_with_fr, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins eigen_g <- eigen_centrality(g) size par(mar=c(0.5,0.5,0.5,0.5)) ### set margins eigen_g <- eigen_centrality(g) V(g)$size <- eigen_g$vector*10 V(g)$label.color <- "blue" plot(g, layout=layout_with_fr, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title(main="Random graph. Node size = node eigenvector centrality", cex.main=1) ?layout_with_fr ?layout_with_kk par(mar=c(0.5,0.5,0.5,0.5)) ### set margins plot(g.pa) title(main="Graph layout default", cex.main=0.8) plot(g.pa, layout=layout_randomly, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title("Randomly place vertices _PA", cex.main=0.8) plot(g.pa, layout=layout_with_fr, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title(main="Fruchterman-Reingold layout -PA", cex.main=0.8) plot(g.pa, layout=layout_with_kk, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title(main="Kamada-Kawai layout -PA", cex.main=0.8) plot(g.pa, layout=layout_in_circle, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title(main="Graph layout circle -PA", cex.main=0.8) plot(g.pa, layout=layout_with_mds, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) plot(g.pa, layout=layout_with_kk, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) plot(g.pa, layout=layout_with_mds, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) ## Compute node degree and use it to set node size par(mar=c(0.5,0.5,0.5,0.5)) ### set margins deg_g.pa <- igraph::degree(g.pa, mode = "all", normalized = T) V(g.pa)$name <- 1:vcount(g.pa) V(g.pa)$label.color <- "blue" V(g.pa)$size <- deg_g.pa*100 plot(g.pa, layout=layout_with_fr, vertex.label = V(g.pa)$name, vertex.label.dist=0, vertex.label.cex=0.6) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins clos_g.pa <- closeness(g.pa, mode = "all", normalized = T) V(g.pa)$size <- clos_g.pa*10 plot(g.pa, layout=layout_with_fr, vertex.label = V(g.pa)$name, vertex.label.dist=0, vertex.label.cex=0.6) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins betw_g.pa <- betweenness(g.pa, directed = F, normalized = T) V(g.pa)$size <- betw_g.pa*10 plot(g.pa, layout=layout_with_fr, vertex.label = V(g)$name, vertex.label.dist=0, vertex.label.cex=0.6) title(main="Pref. Attach. graph. Node size = node betweenness centrality", cex.main=1) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins eigen_g.pa <- eigen_centrality(g.pa) V(g.pa)$size <- eigen_g.pa$vector*10 plot(g.pa, layout=layout_with_fr, vertex.label = V(g.pa)$name, vertex.label.dist=0, vertex.label.cex=0.6) AB <- aidsblog par(mfrow=c(2,3)) plot(AB) title(main="Graph layout default", cex.main=0.8) plot(AB, layout=layout_randomly) title("Randomly place vertices _AB", cex.main=0.8) plot(AB, layout=layout_with_fr) title(main="Fruchterman-Reingold layout -AB", cex.main=0.8) plot(AB, layout=layout_with_kk) title(main="Kamada-Kawai layout -AB", cex.main=0.8) plot(AB, layout=layout_in_circle) title(main="Graph layout circle -AB", cex.main=0.8) plot(AB, layout=layout_with_mds) title(main="Graph layout multidimensional scaling -AB", cex.main=0.8) dev.off() coord <- layout_with_fr(AB) coord coord <- layout_with_fr(AB) ## Compute node indegree and use it to set node size par(mar=c(0.5,0.5,0.5,0.5)) ### set margins indeg_AB<- igraph::degree(AB, mode = "in", normalized = T) plot(AB, layout=coord, vertex.size=V(AB)$Indegsize) title(main="Aidsblog. Node size = node indegree centrality", cex.main=1) palf <- colorRampPalette(c("white", "red")) heatmap(flom[, 16:1], Rowv = NA, Colv = NA, col = palf(2), scale="none", margins=c(6,6) )