Here is an example of how to highlight the members of a subgraph on a plot of a network graph.
## Load R libraries library(igraph) # Set adjacency matrix g <- matrix(c(0,1,1,1, 1,0,1,0, 1,1,0,1, 0,0,1,0),nrow=4,ncol=4,byrow=TRUE) # Set adjacency matrix to graph object g <- graph.adjacency(g,mode="directed") # Add node attribute label and name values V(g)$name <- c("n1","n2","n3","n4") # Set subgraph members c <- c("n1","n2","n3") # Add edge attribute id values E(g)$id <- seq(ecount(g)) # Extract supgraph ccsg <- induced.subgraph(graph=g,vids=c) # Extract edge attribute id values of subgraph ccsgId <- E(ccsg)$id # Set graph and subgraph edge and node colors and sizes E(g)$color="grey" E(g)$width=2 E(g)$arrow.size=1 E(g)$arrow.width=1 E(g)[ccsgId]$color <- "#DC143C" # Crimson E(g)[ccsgId]$width <- 2 V(g)$size <- 4 V(g)$color="#00FFFF" # Cyan V(g)$label.color="#00FFFF" # Cyan V(g)$label.cex <-1.5 V(g)[c]$label.color <- "#DC143C" # Crimson V(g)[c]$color <- "#DC143C" # Crimson # Set seed value set.seed(40041) # Set layout options l <- layout.fruchterman.reingold(g) # Plot graph and subgraph plot.igraph(x=g,layout=l)
Simple, no?
Asking questjons are in fact pleasant thing if you are not understanding something
completely, however this article offers nie understanding even.
I do this kind of thing all the time. I’ve just made peace with it. Is the code not working?
Dear Will,
The code is well written and only highlights the members of a subgraph. I need to fetch them out from the main graph as a separate subgraph (including nodes and edges). Any suggestions please.
Thanks.
Hey Vinay,
The iGraph function
induced.subgraph()
will let you extract a subgraph from a graph without losing any essential network characteristics. See post: http://bit.ly/SO24vt.Pingback: How to extract a network subgraph using R | Getting Under the Water
Klasse Webseite, ich komme mal wieder vorbei.