If you don’t want to draw by hand (or you have a large graph), then you can use the igraph package. Below are some examples that are covered in lecture There are many options that are not show in these simple examples but is enough to get you started

The graphs look much nicer if you run the R script. They do not render to html very well.

You’ll need

library(igraph)   # Network analysis & visualization
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union

All of the graphs, except the last one are undirected graphs.

A few notes

In these examples, I take as input an adjacency matrix which is a square matrix (has the same rows are columns). The undirected graphs the matrix is symmetric where the (i,j) element equals the (j,i) element. The (i,j) element equals 1 if row variable i is connected to column variable j.

The graphs look much nicer if you run the R script. They do not render to html very well.

Page 6

A path

#                     X  Y  Z
adjmatrix <- matrix(c(0, 1, 0,
                      1, 0, 1,
                      0, 1, 0),
              nrow=3, ncol=3)

colnames(adjmatrix) <- c("X","Y","Z")

path <- graph_from_adjacency_matrix(adjmatrix, mode=c("undirected"))
plot(path )

If you want to change the placement of point/nodes/vertexes, you and give the (x,y) coordinates. In the example below I also change color of vertices and their size. We use the same graph, only the plotting changes.

layout1 <- matrix(c(1,1,
                    2,1,
                    3,1),
            nrow=3, ncol=2, byrow=TRUE)

plot(path, main="Fixing location of nodes \n Changing vertex color \n Larger vertex size",  
           layout=layout1,
           vertex.color=c("orange","lightblue","yellow"),
           vertix.size=10)

Cordless Cycle

# W, X  Y  Z
adjmatrix <- matrix(c(0, 1, 1, 0,
                      1, 0, 0, 1,
                      1, 0, 0, 0,
                      0, 1, 1, 0),
              nrow=4, ncol=4)

rownames(adjmatrix) <- c("W","X","Y","Z")
colnames(adjmatrix) <- c("W","X","Y","Z")

cord <- graph_from_adjacency_matrix(adjmatrix, mode=c("undirected"))
plot(cord)

Page 7: Clique

adjmatrix <- matrix(c(0, 1, 1, 1,
                      1, 0, 1, 1,
                      1, 1, 0, 1,
                      0, 1, 1, 0),
              nrow=4, ncol=4)

rownames(adjmatrix) <- c("W","X","Y","Z")
colnames(adjmatrix) <- c("W","X","Y","Z")

clique <- graph_from_adjacency_matrix(adjmatrix, mode=c("undirected"))
plot(clique)

page 8

Complete independence

#                     X  Y  Z
adjmatrix <- matrix(c(0, 0, 0,
                      0, 0, 0,
                      0, 0, 0),
              nrow=3, ncol=3)

rownames(adjmatrix) <- c("X","Y","Z")
colnames(adjmatrix) <- c("X","Y","Z")

independence <- graph_from_adjacency_matrix(adjmatrix, mode=c("undirected"))
plot(independence  )

Joint independence

I will only do (XY,Z)

#                     X  Y  Z
adjmatrix <- matrix(c(0, 1, 0,
                      1, 0, 0,
                      0, 0, 0),
              nrow=3, ncol=3)

rownames(adjmatrix) <- c("X","Y","Z")
colnames(adjmatrix) <- c("X","Y","Z")

jind <- graph_from_adjacency_matrix(adjmatrix, mode=c("undirected"))
plot(jind, main="Joint Independence: (XY,Z)"   )

Page 9: Conditional Independence

I will only do (XY,XZ)

#                     X  Y  Z
adjmatrix <- matrix(c(0, 1, 1,
                      1, 0, 0,
                      1, 0, 0),
              nrow=3, ncol=3)

rownames(adjmatrix) <- c("X","Y","Z")
colnames(adjmatrix) <- c("X","Y","Z")

cind <- graph_from_adjacency_matrix(adjmatrix, mode=c("undirected"))
plot(cind, main="Conditional Independence: (XY,XZ)", cex=2  )

Page 37: A directed Graph

The adjacency matrix is not longer symmetric. If an arrow should go from row variable (node) i to node j, then enter 1 in the (i,j) cell and 0 otherwise.

This example also sets up a lay out for where the node should be plotted. Also, I used the variable names (or abrv) for the node labels. I had to increase the size of the nodes (i.e. vertex.size) so that the node labels would fit withint the circles.

#                     G  P  E  M
adjmatrix <- matrix(c(0, 1, 0, 0,
                      0, 0, 1, 1,
                      0, 0, 0, 1,
                      0, 0, 0, 0),
              nrow=4, ncol=4, byrow=TRUE)

colnames(adjmatrix) <- c("Gender", "Premartial \n Sex", "Extramatrial \n Sex", "Marital \n Status")

layout <- matrix(c(1, 1,
                   2, 1,
                   3, 1.5,
                   4, 1), ncol=2, nrow=4, byrow=TRUE)

directed <- graph_from_adjacency_matrix(adjmatrix, mode=c("directed"))
plot(directed, main="Directed Graph", 
     vertex.size=c(35,35,36,35),  
     layout=layout)