#### Packages install ##install.packages("igraph") ##install.packages("igraphdata") ##install.packages("network") ##install.packages("networkD3") ##install.packages("circlize") install.packages("sand") ##Load packages library("igraph") library("igraphdata") library("network") library("networkD3") library("circlize") library(sand) ## Set working directory ##setwd("C:/.....) # Reproducible layout set.seed(42) ## At the heart of graph visualization is the graph layout, i.e., the placement of vertices #and edges in space. ### Random dataset # random graph Erdos-Renyi model with nodes = 20 and density = 0.2 --undirected links g <- sample_gnp(50, 0.1, directed=FALSE) ?sample_gnp edge_density(g) ##density of g g # let sample graph from another model preferential attachment model g.pa <- sample_pa(50, 1, directed=FALSE) ?sample_pa edge_density(g.pa) ##density of g g.pa #let use a well-known dataset aidsblog data("aidsblog") summary(aidsblog) # adjacency matrices for graphs g e aidsblog gm <- get.adjacency(g, sparse=F) aidsm <- get.adjacency(aidsblog, sparse=F) ## different layouts for graph 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) 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") plot(g.pa, layout=layout_with_fr, vertex.label = V(g)$name) title("pref. attachment") plot(aidsblog, layout=layout_with_fr) title("Blog Network") # 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") # Kamada-Kawai layout 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(aidsblog, mode = "all") ## Compute node degree and use it to set node size in the graph ## igraph::degree to use the degree fuction in igraph package par(mar=c(0.5,0.5,0.5,0.5)) ### set margins deg_g <- igraph::degree(g, mode = "all", normalized = T) V(g)$size <- deg_g*100 V(g)$name <- 1:vcount(g) 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 degree centrality", cex.main=1) ## Compute node closeness and use it to set node size 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) title(main="Random graph. Node size = node closeness centrality", cex.main=1) ## Compute node betweenness and use it to set node size par(mar=c(0.5,0.5,0.5,0.5)) ### set margins ?betweenness betw_g <- igraph::betweenness(g, directed = F, normalized = T) 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) title(main="Random graph. Node size = node betweenness centrality", cex.main=1) ## Compute node eigenvector and use it to set node 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 ## preferential attachment - undirected graph 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) title(main="Graph layout multidimensional scaling -PA", cex.main=0.8) dev.off() ## 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) title(main="Pref. Attach. graph. Node size = node degree centrality", cex.main=1) #### Compute node closeness and use it to set node size 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) title(main="Pref. Attach. graph. Node size = node closeness centrality", cex.main=1) #### Compute node betweenness and use it to set node size 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) #### Compute node eigenvector and use it to set node size 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) title(main="Pref. Attach. graph. Node size = node eigenvector centrality", cex.main=1) ## random graph - directed graph 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() # if you want to freeze node position... 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) V(AB)$Indegsize <- indeg_AB*50 plot(AB, layout=coord, vertex.size=V(AB)$Indegsize) title(main="Aidsblog. Node size = node indegree centrality", cex.main=1) # with the outdegree outdeg_AB<- igraph::degree(AB, mode = "out", normalized = T) V(AB)$Outdegsize <- outdeg_AB*50 plot(AB, layout=coord, vertex.size=V(AB)$Outdegsize) title(main="Aidsblog. Node size = node outdegree centrality", cex.main=1) #with the overall degree deg_AB<- igraph::degree(AB, mode = "all", normalized = T) V(AB)$degsize <- deg_AB*50 plot(AB, layout=coord, vertex.size=V(AB)$degsize) title(main="Aidsblog. Node size = node overall degree centrality", cex.main=1) # bipartite graphs often are laid out with the two sets of vertices running # across opposing rows (or down opposing columns) plot(g.bip, layout=-layout.bipartite(g.bip)[,2:1], vertex.size=30, vertex.shape=ifelse(V(g.bip)$type,"rectangle", "circle"), vertex.color=ifelse(V(g.bip)$type, "red", "cyan")) ################################################################################# #decorating graphs ### Karate undirected graph data(package="igraphdata") data(karate) class(karate) # graph object table(V(karate)$Faction) karatem <- get.adjacency(karate, sparse=F) dim(karatem) class(karatem) # matrix edge_density (karate) # Plot undecorated first par(mar=c(0.5,0.5,0.5,0.5))### set margins igraph.options(vertex.size=10) l1 <- layout.kamada.kawai(karate) plot(karate, layout=l1, vertex.label=V(karate)) title(main="undecorated plot karate", cex.main=0.8) # Now decorate, starting with labels V(karate)$label <- sub("Actor ", "", V(karate)$name) # Two leaders get shapes different from club members. V(karate)$shape <- "circle" V(karate)[c("Mr Hi", "John A")]$shape <- "rectangle" # Differentiate two factions by color. V(karate)[Faction == 1]$color <- "red" V(karate)[Faction == 2]$color <- "dodgerblue" # Vertex area proportional to vertex strength # (i.e., total weight of incident edges). V(karate)$size <- 4*sqrt(graph.strength(karate)) V(karate)$size2 <- V(karate)$size * .5 # Weight edges by number of common activities among karate club members E(karate)$width <- E(karate)$weight # Color edges by within/between faction. F1 <- V(karate)[Faction==1] F2 <- V(karate)[Faction==2] E(karate)[ F1 %--% F1 ]$color <- "pink" E(karate)[ F2 %--% F2 ]$color <- "lightblue" E(karate)[ F1 %--% F2 ]$color <- "yellow" # Offset vertex labels for smaller points (default=0). V(karate)$label.dist <- ifelse(V(karate)$size >= 10, 0, 0.75) # Plot decorated graph, using same layout. plot(karate, layout=l1) title(main="decorated plot karate", cex.main=0.8) V(karate)$Faction ############################################################# ## one-mode network - undirected graph ## dataset florentine families (Padgett data) --parental relation library(network) data(package="network") data("flo") class(flo) # is a matrix ### trasform matrix in igraph object flo.adiacency <- graph_from_adjacency_matrix(flo, mode="undirected") class(flo.adiacency) # is a graph object edge_density(flo.adiacency) ### node labels V(flo.adiacency)$label <- sub("Actor ", "", V(flo.adiacency)$name) ### all graphs layouts for florentine families par(mar=c(1.5,1.5,1.5,1.5)) ### set margins igraph.options(vertex.size=10, vertex.label=V(flo.adiacency)$name, vertex.label.dist=0.8, vertex.color="orange", vertex.label.cex=0.8) par(mfrow = c(2,3)) plot(flo.adiacency, layout=layout_nicely(flo.adiacency)) title(main="Florentine graph layout nicely", cex.main=0.8) plot(flo.adiacency, layout=layout_randomly(flo.adiacency)) title(main="Florentine graph layout randomly place vertices", cex.main=0.8) plot(flo.adiacency, layout=layout_with_fr(flo.adiacency)) title(main="Florentine graph layout Fruchterman-Reingold", cex.main=0.8) plot(flo.adiacency, layout=layout_with_kk(flo.adiacency)) title(main="Florentine graph layout Kamada-Kawai", cex.main=0.8) plot(flo.adiacency, layout=layout_in_circle(flo.adiacency)) title(main="Florentine graph layout circle", cex.main=0.8) plot(flo.adiacency, layout=layout_with_mds(flo.adiacency)) title(main="Florentine graph layout multidimensional scaling", cex.main=0.8) dev.off() ### to save coordinates and plot nodes on the same coordinates coordfr<-layout_with_fr(flo.adiacency) plot(flo.adiacency, layout=coordfr) #### Compute node degree and use it to set node size ## florentine families - undirected graph par(mar=c(0.5,0.5,0.5,0.5)) ### set margins deg_flo <- igraph::degree(flo.adiacency, mode = "all", normalized = T) V(flo.adiacency)$size <- deg_flo*100 V(flo.adiacency)$label.color <- "blue" plot(flo.adiacency, layout=layout_with_kk(flo.adiacency), vertex.label.dist=0, vertex.label.cex=0.6) title(main="Florentine graph. Node size = node degree centrality", cex.main=1) ####statistics # Degree distribution par(mar=c(4,4,4,4)) ### set margins dd <- degree.distribution(flo.adiacency, cumulative=T, mode="all") plot(dd, pch=19, cex=0.8, col="orange", xlab="Degree", ylab="Cumulative") # Heatmap of the network matrix - sociomatrix representation palf <- colorRampPalette(c("white", "red")) heatmap(flom[, 16:1], Rowv = NA, Colv = NA, col = palf(2), scale="none", margins=c(6,6) ) ##title(main="heatmap plot florentine", cex.main=0.8) # Heatmap of the network matrix - sociomatrix representation #### reordering adiacency matrix according to clustering - geodesic distances geoflo<-distances(flo.adiacency, v = V(flo.adiacency), to = V(flo.adiacency), mode ="all", weights = NULL, algorithm = "automatic") geoflo[geoflo==Inf]<-15 geoflo<-as.dist(geoflo) hclust_geoflo<-hclust(geoflo, method = "complete", members = NULL) heatmap(flom,Rowv=hclust_geoflo$order,Colv="Rowv", scale="none") ###title(main="heatmap plot florentine", cex.main=0.8) ### Directed and weighted graphs - one-mode network data(package="igraphdata") data(UKfaculty) class(UKfaculty) # graph object table(V(UKfaculty)$Group) edge_density (UKfaculty) UKfacultym <- get.adjacency(UKfaculty, sparse=F) ## node color based on attribute data par(mar=c(0.5,0.5,0.5,0.5)) ### set margins igraph.options(vertex.size=6, vertex.label=NA, vertex.label.dist=1, vertex.label.cex=0.8, edge.color="lightblue", edge.arrow.size=0.4) par(mfrow = c(2,3)) E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group plot(UKfaculty, layout=layout_nicely(UKfaculty)) title(main="UKfaculty directed graph layout nicely", cex.main=0.8) plot(UKfaculty, layout=layout_randomly(UKfaculty)) title("UKfaculty directed graph randomly place vertices", cex.main=0.8) plot(UKfaculty, layout=layout_with_fr(UKfaculty)) title(main="UKfaculty directed graph Fruchterman-Reingold layout", cex.main=0.8) plot(UKfaculty, layout=layout_with_kk(UKfaculty)) title(main="UKfaculty directed graph Kamada-Kawai layout", cex.main=0.8) plot(UKfaculty, layout=layout_in_circle(UKfaculty)) title(main="UKfaculty graph layout circle", cex.main=0.8) plot(UKfaculty, layout=layout_with_mds(UKfaculty)) title(main="UKfaculty directed graph layout multidimensional scaling", cex.main=0.8) dev.off() ### separate figures with different layouts ### different shapes and colors for groups par(mar=c(0.5,0.5,0.5,0.5)) ### set margins E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group V(UKfaculty)[V(UKfaculty)$Group=="1"]$shape <- "rectangle" V(UKfaculty)[V(UKfaculty)$Group=="2"]$shape <- "circle" V(UKfaculty)[V(UKfaculty)$Group=="3"]$shape <- "square" V(UKfaculty)[V(UKfaculty)$Group=="4"]$shape <- "sphere" plot(UKfaculty, layout=layout_nicely(UKfaculty), vertex.size=5, vertex.label=NA, edge.color="lightblue", edge.arrow.size=0.6) title(main="UKfaculty directed graph layout nicely", cex.main=0.8) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group V(UKfaculty)[V(UKfaculty)$Group=="1"]$shape <- "rectangle" V(UKfaculty)[V(UKfaculty)$Group=="2"]$shape <- "circle" V(UKfaculty)[V(UKfaculty)$Group=="3"]$shape <- "square" V(UKfaculty)[V(UKfaculty)$Group=="4"]$shape <- "sphere" plot(UKfaculty, layout=layout_randomly(UKfaculty), vertex.size=5, vertex.label=NA, edge.size=15, edge.color="lightblue", edge.arrow.size=0.4) title("UKfaculty directed graph randomly place vertices", cex.main=0.8) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group V(UKfaculty)[V(UKfaculty)$Group=="1"]$shape <- "rectangle" V(UKfaculty)[V(UKfaculty)$Group=="2"]$shape <- "circle" V(UKfaculty)[V(UKfaculty)$Group=="3"]$shape <- "square" V(UKfaculty)[V(UKfaculty)$Group=="4"]$shape <- "sphere" plot(UKfaculty, layout=layout_with_fr(UKfaculty), vertex.size=5, vertex.label=NA, edge.size=15, edge.color="lightblue", edge.arrow.size=0.4) title(main="UKfaculty directed graph Fruchterman-Reingold layout", cex.main=0.8) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group V(UKfaculty)[V(UKfaculty)$Group=="1"]$shape <- "rectangle" V(UKfaculty)[V(UKfaculty)$Group=="2"]$shape <- "circle" V(UKfaculty)[V(UKfaculty)$Group=="3"]$shape <- "square" V(UKfaculty)[V(UKfaculty)$Group=="4"]$shape <- "sphere" plot(UKfaculty, layout=layout_with_kk(UKfaculty), vertex.size=5, vertex.label=NA, edge.size=15, edge.color="lightblue", edge.arrow.size=0.4) title(main="UKfaculty directed graph Kamada-Kawai layout", cex.main=0.8) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group V(UKfaculty)[V(UKfaculty)$Group=="1"]$shape <- "rectangle" V(UKfaculty)[V(UKfaculty)$Group=="2"]$shape <- "circle" V(UKfaculty)[V(UKfaculty)$Group=="3"]$shape <- "square" V(UKfaculty)[V(UKfaculty)$Group=="4"]$shape <- "sphere" plot(UKfaculty, layout=layout_in_circle(UKfaculty), vertex.size=5, vertex.label=NA, edge.size=15, edge.color="lightblue", edge.arrow.size=0.4) title(main="UKfaculty graph layout circle", cex.main=0.8) par(mar=c(0.5,0.5,0.5,0.5)) ### set margins E(UKfaculty)$width <- E(UKfaculty)$weight/3 V(UKfaculty)$color <- V(UKfaculty)$Group V(UKfaculty)[V(UKfaculty)$Group=="1"]$shape <- "rectangle" V(UKfaculty)[V(UKfaculty)$Group=="2"]$shape <- "circle" V(UKfaculty)[V(UKfaculty)$Group=="3"]$shape <- "square" V(UKfaculty)[V(UKfaculty)$Group=="4"]$shape <- "sphere" plot(UKfaculty, layout=layout_with_mds(UKfaculty), vertex.size=5, vertex.label=NA, edge.size=15, edge.color="lightblue", edge.arrow.size=0.4) title(main="UKfaculty directed graph layout multidimensional scaling", cex.main=0.8) #### Compute node indegree and use it to set node size ## UKfaculty friendship relationship directed and weighted graph par(mar=c(0.5,0.5,0.5,0.5)) ## set margins indeg_uk <- igraph::degree(UKfaculty, mode = "in", normalized = T) ##outdeg_uk <- degree(UKfaculty, mode = "out", normalized = T) V(UKfaculty)$size <- indeg_uk*50 ##V(UKfaculty)$size <- out_uk*100 V(UKfaculty)$color <- V(UKfaculty)$Group E(UKfaculty)$width <- E(UKfaculty)$weight/3 plot(UKfaculty, layout=layout_with_fr(UKfaculty), vertex.label.dist=0, vertex.label.cex=0.6, edge.arrow.size=0.4) title(main="UKfaculty graph. Node size = node indegree centrality", cex.main=1) ####statistics # Degree distribution par(mar=c(4,4,4,4)) ## set margins dd <- degree.distribution(UKfaculty, cumulative=T, mode="all") plot(dd, pch=19, cex=0.8, col="orange", xlab="Degree", ylab="Cumulative Frequency") #### Compute node closeness and use it to set node size par(mar=c(0.5,0.5,0.5,0.5)) ## set margins clos_uk <- closeness(flo.adiacency, mode = "all", normalized = T) V(UKfaculty)$size <- clos_uk*10 V(UKfaculty)$color <- V(UKfaculty)$Group E(UKfaculty)$width <- E(UKfaculty)$weight/3 plot(UKfaculty, layout=layout_with_fr(UKfaculty), vertex.label.dist=0, vertex.label.cex=0.6) title(main="UKfaculty graph. Node size = node closeness centrality", cex.main=1) #### Compute node betweenness and use it to set node size par(mar=c(0.5,0.5,0.5,0.5)) ## set margins betw_uk <- betweenness(UKfaculty, directed = T, normalized = T) V(UKfaculty)$size <- betw_uk*100 V(UKfaculty)$color <- V(UKfaculty)$Group E(UKfaculty)$width <- E(UKfaculty)$weight/3 plot(UKfaculty, layout=layout_with_kk(UKfaculty), vertex.label.dist=0, vertex.label.cex=0.6) title(main="UKfaculty graph. Node size = node betweenness centrality", cex.main=1) #### Compute node eigenvector and use it to set node size par(mar=c(0.5,0.5,0.5,0.5)) ## set margins eigen_uk<- eigen_centrality(UKfaculty) V(UKfaculty)$size <- eigen_uk$vector*50 V(UKfaculty)$color <- V(UKfaculty)$Group E(UKfaculty)$width <- E(UKfaculty)$weight/3 plot(UKfaculty, layout=layout_with_kk(UKfaculty), vertex.label.dist=0, vertex.label.cex=0.6) title(main="UKfaculty graph. Node size = node eigenvector centrality", cex.main=1) ####heatmap UKfaculty colnames(UKfacultym) <- V(UKfaculty)$name rownames(UKfacultym) <- V(UKfaculty)$name palf <- colorRampPalette(c("red", "white")) heatmap(UKfacultym[,81:1], Rowv = V(UKfaculty)$Group, Colv = "Rowv", col = palf(20), scale="none", margins=c(6,6)) ##title(sub="heatmap UKfaculty", cex.sub=1) ###heatmap by Group heatmap(UKfacultym[order(V(UKfaculty)$Group), ], col = palf(10), labRow= NA, labCol= NA) ################################################################## ### ego-network visualization #the so-called egocentric network visualizations commonly used in the #social network literature, which show the vertex, its immediate neighbors, and all edges among them. k.nbhds <- graph.neighborhood(karate, order=1) # the neighborhoods pertaining to the head instructor (Mr Hi, vertex 1) and administrator (John A, vertex 34) are the largest. sapply(k.nbhds, vcount) k.1 <- k.nbhds[[1]] k.34 <- k.nbhds[[34]] par(mfrow=c(1,2)) plot(k.1, vertex.label=NA, vertex.color=c("red", rep("lightblue", 16))) plot(k.34, vertex.label=NA, vertex.color=c(rep("lightblue", 17), "red")) dev.off() ################################################################## ####two-mode network # Random bipartite graph inc <- matrix(sample(0:1, 50, replace = TRUE, prob=c(2,1)), 10, 5) gtwo <- graph_from_incidence_matrix(inc) plot(gtwo, layout = layout_as_bipartite, vertex.color=c("blue","red")[V(gtwo)$type+1]) ### two-mode networks Davis Data davis<-read.table("Davis.txt",header = T, row.names = 1) davis<-as.matrix(davis) dim (davis) davis.affiliation <- graph.incidence(davis) is_bipartite(davis.affiliation) par(mar=c(1.5,1.5,1.5,1.5)) ##set margins igraph.options(vertex.size=8, vertex.label.dist=1, vertex.label.cex=0.3) plot(davis.affiliation, layout = layout_as_bipartite, vertex.label=V(davis.affiliation)$name, vertex.color=c("orange","blue")[V(davis.affiliation)$type+1]) title(main="Simple two-row layout for bipartite graphs", cex.main=0.8) ### Circos graphs par(mar=c(0.5,0.5,0.5,0.5)) ##set margins chordDiagram(davis) ### from two-mode network to two one-mode networks ## how to derive two one-mode networks from a two mode network bipart<-bipartite.projection(davis.affiliation) ## one.mode network nodes x nodes donneDavis<-bipart$proj1 class(donneDavis) donneDavis_m <- get.adjacency(donneDavis, sparse=F) class(donneDavis_m) dim(donneDavis_m) ## ## one.mode network events x events eventiDavis<-bipart$proj2 class(eventiDavis) eventiDavis_m <- get.adjacency(eventiDavis, sparse=F) class(eventiDavis_m) dim(eventiDavis_m) ## Interactive plotting with tkplot ## R and igraph offer interactive plotting capabilities ## (mostly helpful for small networks) tkid <- tkplot(flo.adiacency) #tkid is the id of the tkplot l <- tkplot.getcoords(tkid) # grab the coordinates from tkplot plot(flo.adiacency, layout=l) plot(flo.adiacency, layout=layout_with_fr) tkplot(flo.adiacency, layout=layout_with_fr) ################################################################## ### network from package networkd3 ## florentine families example flo_data<-as_data_frame(flo.adiacency, what="edges") flo_data=flo_data[,1:2] simpleNetwork(flo_data, width = 400, height = 250) ?simpleNetwork ## UKfaculty example1 UK_data<-as_data_frame(UKfaculty) simpleNetwork(UK_data, Source = "from", Target = "to", nodeColour = "orange", width = 400, height = 250)