A quick introduction to igraph functions. You may have to install igraph
package if you have not done so earliar
install.packages("igraph")
install.packages("igraph")
Making a graph using a vector
library(igraph)
# define the relationships (described by pairs in the vector - delibrately shown with gaps) while defining the nodes.
g <- graph(c(1,2, 2,3, 3,4, 5,6), directed=FALSE)
# plot the graph
plot(g)
# display the structure
str(g)
## IGRAPH U--- 6 4 --
## + edges:
## [1] 1--2 2--3 3--4 5--6
You will observe that the relationships are created between node 1,2; 2,3; 3,4 and so on. You may use plot
to create empty graphs, or graphs with the given edges, adjacency matrices, etc. Specifically, this is an undirected graph.
library(igraph)
# define the relationships (described by pairs in the vector - delibrately shown with gaps) while defining the nodes.
g <- graph(c(1,2, 2,3, 3,4, 5,6), directed=FALSE)
# plot the graph
plot(g)
# display the structure
str(g)
## IGRAPH U--- 6 4 --
## + edges:
## [1] 1--2 2--3 3--4 5--6
You will observe that the relationships are created between node 1,2; 2,3; 3,4 and so on. You may use
plot
to create empty graphs, or graphs with the given edges, adjacency matrices, etc. Specifically, this is an undirected graph.Controlling Layouts
# 50 vertices and 3 children per vertex
g <- graph.tree(50, 3)
# print the structure
str(g)
## IGRAPH D--- 50 49 -- Tree
## + attr: name (g/c), children (g/n), mode (g/c)
## + edges:
## [1] 1-> 2 1-> 3 1-> 4 2-> 5 2-> 6 2-> 7 3-> 8 3-> 9 3->10 4->11
## [11] 4->12 4->13 5->14 5->15 5->16 6->17 6->18 6->19 7->20 7->21
## [21] 7->22 8->23 8->24 8->25 9->26 9->27 9->28 10->29 10->30 10->31
## [31] 11->32 11->33 11->34 12->35 12->36 12->37 13->38 13->39 13->40 14->41
## [41] 14->42 14->43 15->44 15->45 15->46 16->47 16->48 16->49 17->50
# use a circular layout or other layout algorithms to position the vertices
# example http://en.wikipedia.org/wiki/Force-directed_graph_drawing
plot(g, layout=layout.circle)
plot(g, layout=layout.fruchterman.reingold)
# 50 vertices and 3 children per vertex
g <- graph.tree(50, 3)
# print the structure
str(g)
## IGRAPH D--- 50 49 -- Tree
## + attr: name (g/c), children (g/n), mode (g/c)
## + edges:
## [1] 1-> 2 1-> 3 1-> 4 2-> 5 2-> 6 2-> 7 3-> 8 3-> 9 3->10 4->11
## [11] 4->12 4->13 5->14 5->15 5->16 6->17 6->18 6->19 7->20 7->21
## [21] 7->22 8->23 8->24 8->25 9->26 9->27 9->28 10->29 10->30 10->31
## [31] 11->32 11->33 11->34 12->35 12->36 12->37 13->38 13->39 13->40 14->41
## [41] 14->42 14->43 15->44 15->45 15->46 16->47 16->48 16->49 17->50
# use a circular layout or other layout algorithms to position the vertices
# example http://en.wikipedia.org/wiki/Force-directed_graph_drawing
plot(g, layout=layout.circle)
plot(g, layout=layout.fruchterman.reingold)
Interactive Tools
You may have to install rgl
package if you have not done so earliar
install.packages("rgl")
# launch interactive viewer
tkplot(g, layout=layout.kamada.kawai)
## Loading required package: tcltk
## [1] 1
# or an OpenGL graph plotter
library(rgl)
coords <- layout.fruchterman.reingold(g, dim=3)
rglplot(g, layout=coords)
You may have to install
rgl
package if you have not done so earliarinstall.packages("rgl")
# launch interactive viewer
tkplot(g, layout=layout.kamada.kawai)
## Loading required package: tcltk
## [1] 1
# or an OpenGL graph plotter
library(rgl)
coords <- layout.fruchterman.reingold(g, dim=3)
rglplot(g, layout=coords)
few more methods to define and plot graphs
Reading a adjacency matrix from a CSV file
# Change the working directory
setwd("/path/to/folder/where/csv/file/is/saved/");
# read the csv file with adjacency matrix into memory
# ** NOTICE** row.names=1, header=TRUE
# Steps involved
# Read the CSV -> Convert to Matrix -> Force it to be numeric -> Create the graph object -> plot
myData<- read.csv("1.01.RiGraph.csv", row.names=1, header=TRUE)
# The data
myData
## A1 A2 A3 A4 A5
## A1 0 1 1 0 1
## A2 1 0 1 0 0
## A3 1 1 0 1 1
## A4 0 0 1 0 0
## A5 1 0 1 0 0
myMatrix=as.matrix(myData)
mode(myMatrix) <- "numeric"
g=graph.adjacency(adjmatrix=myMatrix,mode="undirected",weighted=TRUE,diag=FALSE)
plot(g)
# Change the working directory
setwd("/path/to/folder/where/csv/file/is/saved/");
# read the csv file with adjacency matrix into memory
# ** NOTICE** row.names=1, header=TRUE
# Steps involved
# Read the CSV -> Convert to Matrix -> Force it to be numeric -> Create the graph object -> plot
myData<- read.csv("1.01.RiGraph.csv", row.names=1, header=TRUE)
# The data
myData
## A1 A2 A3 A4 A5
## A1 0 1 1 0 1
## A2 1 0 1 0 0
## A3 1 1 0 1 1
## A4 0 0 1 0 0
## A5 1 0 1 0 0
myMatrix=as.matrix(myData)
mode(myMatrix) <- "numeric"
g=graph.adjacency(adjmatrix=myMatrix,mode="undirected",weighted=TRUE,diag=FALSE)
plot(g)
Interesting metrics
# calculate the degree
degree(g)
## A1 A2 A3 A4 A5
## 3 2 4 1 2
# lets plot the histogram of degrees
hist(degree(g), breaks=c(0:4))
# calculate degree distribution of individual vertices
degree.distribution(g)
## [1] 0.0 0.2 0.4 0.2 0.2
plot(degree.distribution(g))
lines(degree.distribution(g))
# Calculate graph/edge density
graph.density(g)
## [1] 0.6
# number of vertices
length(V(g))
## [1] 5
# number of edges
length(E(g))
## [1] 6
# diameter
diameter(g)
## [1] 2
# calculate the degree
degree(g)
## A1 A2 A3 A4 A5
## 3 2 4 1 2
# lets plot the histogram of degrees
hist(degree(g), breaks=c(0:4))
# calculate degree distribution of individual vertices
degree.distribution(g)
## [1] 0.0 0.2 0.4 0.2 0.2
plot(degree.distribution(g))
lines(degree.distribution(g))
# Calculate graph/edge density
graph.density(g)
## [1] 0.6
# number of vertices
length(V(g))
## [1] 5
# number of edges
length(E(g))
## [1] 6
# diameter
diameter(g)
## [1] 2
No comments:
Post a Comment