# How to plot a network subgraph on a network graph using R 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)

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

# 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?

## 6 thoughts on “How to plot a network subgraph on a network graph using R”

1. ringing in the ears meaning

Asking questjons are in fact pleasant thing if you are not understanding something

• willchernoff

I do this kind of thing all the time. I’ve just made peace with it. Is the code not working?

2. Vinay

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.

• willchernoff

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.

3. Merkur Casino Tricks

Klasse Webseite, ich komme mal wieder vorbei.