# Plot Network Data in R with iGraph

I recently had a conversation on Twitter about a plot I made a while back. Recall, the plot showed my Twitter network, my friends and my friend’s friends.

And here’s the R code:

#### Load R libraries
library("iGraph")

#### Convert to graph object
gr <- graph.data.frame(r,directed=TRUE)

#### gr
# Describe graph
summary(gr)
ecount(gr) # Edge count
vcount(gr) # Node count
diameter(gr) # Network diameter
farthest.nodes(gr) # Nodes furthest apart
V(gr)$indegree = degree(gr,mode="in") # Calculate indegree #### Plot graph E(gr)$color = "gray"
E(gr)$width = .5 E(gr)$arrow.width = .25
V(gr)$label.color = "black" V(gr)$color = "dodgerblue"
V(gr)$size = 4 set.seed(40134541) l <- layout.fruchterman.reingold(gr) pdf("network_friends_plot.pdf") plot(gr,layout=l,rescale=TRUE,axes=FALSE,ylim=c(-1,1),asp=0,vertex.label=NA) dev.off() # How to extract a network subgraph using R In a previous post I wrote about highlighting a subgraph of a larger network graph. In response to this post, I was asked how extract a subgraph from a larger graph while retaining all essential characteristics among the extracted nodes. Vinay wrote: 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. Extract subgraph For a given list of subgraph members, we can extract their essential characteristics (i.e., tie structure and attributes) from a larger graph using the iGraph function induced.subgraph(). For instance, library(igraph) # Load R packages set.seed(654654) # Set seed value, for reproducibility g <- graph.ring(10) # Generate random graph object E(g)$label <- runif(10,0,1)       # Add an edge attribute

# Plot graph
png('graph.png')
par(mar=c(0,0,0,0))
plot.igraph(g)
dev.off()

g2 <- induced.subgraph(g, 1:7)    # Extract subgraph

# Plot subgraph
png('subgraph.png')
par(mar=c(0,0,0,0))
plot.igraph(g2)
dev.off()


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